Laravel5.8入门与实战-安全与优化
数据库database迁移文件字段设置
$table->bigIncrements('id');
$table->string('title');
$table->string('keyword');
$table->string('pic',150);
$table->longText('desc');
$table->longText('remark');
$table->integer('views')->default(0);
$table->text('content');
$table->timestamps();
命令 php artisan migrate:list生产数据库迁移文件
填充数据
public function run()
{
//构造器方式操作
$data = [];
for ($i=0; $i < 30; $i++) { //生产30条数据
$data[] = [
'title'=> Str::random(20),
'keyword'=> Str::random(30),
'desc'=> Str::random(150),
'remark'=> Str::random(300),
'content'=> Str::random(500),
'created_at'=> date('Y/m/d H:i:s'),
'updated_at'=> date('Y/m/d H:i:s'),
];
}
DB::table('news')->insert($data);
}
命令 php artisan db:seed
控制器设置
文章列表首页
public function index()
{
//通过创建时间和id倒序获取文章 分页 每页10条 然后return数据给视图模板文件
$list = AppNews::orderby('created_at','Desc')->orderby('id','Desc')->paginate(10);
return view('admin.news.index')->with('list',$list);
}
}文章添加页面
public function store(Request $request)
{
//根据字段创建模型
$NewsModel = new News;
$NewsModel->title = $request->title;
$NewsModel->keyword = $request->keyword;
$NewsModel->desc = $request->desc;
$NewsModel->remark = $request->remark;
$NewsModel->views = $request->views;
$NewsModel->content = $request->content;
//判断是否有文件上传(文件上传模板里面要设置enctype="multipart/form-data") if ($request->file('file')) {
$NewsModel->pic = $request->file('file')->store('news');
}
// 设置添加成功失败信息
if($NewsModel->save()){
session()->flash('data',['class'=>'success','msg'=>'添加成功']);
}else{
session()->flash('data',['class'=>'danger','msg'=>'添加失败']);
}
return redirect(route('admin.news.index'));
}