数据库
- 连接到数据库 CLI
//进入 Homestead目录
vagrant up //启动homestead虚拟机
vagrant ssh //进入虚拟机
cd code //进入项目
php artisan db //链接mysql
show databases; //查看数据库
exit; //退出数据库
- 数据表创建
//快捷键
Linux命令行下如何终止当前程序快捷键: Ctrl+c
phpstorm 自动加入类 alt + enter +enter
php artisan migrate //运行迁移
php artisan make:migration create_goods_table //创建一个新的表
$table->id();
$table->integer('user_id')->comment('创建者');
$table->integer('category_id')->comment('分类');
$table->string('title')->comment('标题');
$table->string('description')->comment('描述');
$table->integer('price')->comment('价格');
$table->integer('stock')->comment('库存');
$table->integer('sales')->default(0)->comment('销量');
$table->string('cover')->comment('封面图');
$table->json('pics')->nullable()->comment('小图集');
$table->tinyInteger('is_on')->default(0)->comment('是否上架 0不上架 1上架');
$table->tinyInteger('is_recommend')->default(0)->comment('是否推荐 0不推荐 1推荐');
$table->text('details')->comment('详情');
$table->timestamps();
$table->softDeletes();
$table->index('category_id');
$table->index('title');
$table->index('is_on');
$table->index('is_recommend');
php artisan make:migration add_hot_to_goods //添加字段
$table->tinyInteger('hot')->default(0)->comment('是否热门');
php artisan migrate:rollback //回滚迁移
查询构造器
//资源路由
Route::resource('/goods',GoodsController::class);
//GoodsController
$goods= DB::select('SELECT * FROM goods');
$goods= DB::select('SELECT * FROM goods WHERE `id`=?',[1]);// 不常用
$goods= DB::table('goods')->get(); //获取多条
$goods= DB::table('goods')->first(); //获取单条
//where 条件
$goods= DB::table('goods')->where('id',1)->first();
//只要商品的名称
$goods= DB::table('goods')->where('id',1)->value('title');
//取一列
$goods= DB::table('goods')->pluck('title');
$goods= DB::table('goods')->where('id',1)->first();
dd($goods->title);
$good= DB::table('goods')->find($id);
$goods= DB::table('goods')->count();
$goods= DB::table('goods')
->select('title','user_id as uid')
->get();
//where
$goods= DB::table('goods')
->where('price','>',100)
->get();
$goods= DB::table('goods')
->where([
['price','>',100],
['stock','>',10],
])
->get();
//orwhere
$goods= DB::table('goods')
->where([
['price','>',100],
])
->orWhere('stock','>',10)
->get();
//insert
$result= DB::table('goods')->insert(
[
'user_id'=>1,
'category_id'=>1,
'title'=>'小米8 pro',
'description'=>'小米',
'price'=>1499,
'stock'=>99,
'sales'=>1,
'cover'=>'0',
'pics'=>'0',
'is_on'=>0,
'is_recommend'=>0,
'details'=>'0'
]
);
insertGetid //获取id
// update
$result=DB::table('goods')->where('id',1)->update(
[
'price'=>6888,
'stock'=>998
]
);
//调试 dd结束运行,dump不结束运行
DB::table('goods')->where('id',1)->dump();
$data= $request->except('_token');
$result= DB::table('goods')->insertGetid($data);
dd($result);
// create.blade.php
<x-layout>
<x-slot name="title">商品添加</x-slot>
<div>
<form method="post" action="{{route('goods.store')}}" enctype="multipart/form-data" >
@csrf
{{-- <input type="file" name="photo">--}}
<div class="form-group">
<span>标题</span>
<input type="text" name="title">
</div>
<div class="form-group">
<span>user_id</span>
<input type="text" name="user_id">
</div>
<div class="form-group">
<span>category_id</span>
<input type="text" name="category_id">
</div>
<div class="form-group">
<span>description</span>
<input type="text" name="description">
</div>
<div class="form-group">
<span>price</span>
<input type="text" name="price">
</div>
<div class="form-group">
<span>stock</span>
<input type="text" name="stock">
</div>
<div class="form-group">
<span>sales</span>
<input type="text" name="sales">
</div>
<div class="form-group">
<span>cover</span>
<input type="text" name="cover">
</div>
<div class="form-group">
<span>is_on</span>
<input type="text" name="is_on">
</div>
<div class="form-group">
<span>is_recommend</span>
<input type="text" name="is_recommend">
</div>
<div class="form-group">
<span>details</span>
<input type="text" name="details">
</div>
<div class="form-group">
<input type="submit">
</div>
</form>
</div>
</x-layout>
模型
php artisan make:model Good //生成model
//引入use App\Models\Good;
$data=Good::get();
$data=Good::find(1);
dd($data->title);
$data=Good::count();
//Good
protected $table='goods';// 对应可以省略
protected $fillable = [
'title',
'user_id',
'category_id',
'description',
'price',
'stock',
'cover',
'is_on',
'is_recommend',
'details',
];
$result= Good::create($request->all());
dd($result);
//修改
$good=Good::find(6);
$good->title='我是第6条';
$good->save();
dd($good);
请求
//获取所有值
dd($request->all());
//获取单个值
$a= $request->input('a');
dd($a);
$request->path();
$request->ip();
$request->method();
$title = $request->input('title','default');
$title = $request->only(['title','hot']);
$title = $request->except('hot');
if($request->has(['title','hot'])){
}
// 图片上传
enctype="multipart/form-data"
<input type="file" name="photo">
$file=$request->photo->store('images');
php artisan storage:link
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('images') => storage_path('app/images'),
],
- 解决方案
首先先把你已经开启的 homestead 虚拟机关闭掉,关闭语句
vagrant halt
我的系统是 Windows 10 。找到系统里的 cmd.exe 文件,文件路径
C:\Windows\System32\cmd.exe
鼠标右键点击“以管理员身份运行”。
进入 Homestead 目录
我的目录是 “C:\Users\mm\Homestead\”。
执行
vagrant up # 启动虚拟机
vagrant ssh # 进入虚拟机
最后进入对应的项目目录再执行
php artisan storage:link
图片上传保存数据
// GoodsController
public function store(Request $request)
{
$file=$request->photo->store('images');
$img=['cover'=>$file];
$data=$request->all();
//把图片压入提交的其他数据的数组中
$dataAll= array_merge($img,$data);
$result= Good::create($dataAll);
dd($result,$file);
}