laravel 命令行创建controller 创建数据库表迁移 创建module

1、php artisan 查看命令列表

 

2、php artisan make:controller ArticleController 命令 创建控制器

 

3、创建数据库迁移表

创建文章表

php artisan make:migration create_articles_table

Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title',100)->default('');
$table->text('content');
$table->integer('user_id')->default(0);
$table->timestamps();
});
 
开始迁移 如果migration表里面有 就执行迁移没有的 
 php artisan migrate  
如果报错 去config/database  utf8mb4改为utf8
 
4、php artisan make:model Article
 
Article 模块
// 默认对应articles表
class Article extends Model
{
//
protected $table = "article";
}
 
5、tinker  php artisan tinker
设置时区
config app.php
'timezone' => 'UTC'
添加
$art = new \App\Article()
$art->title = "this is title"
$art->content = "this is content"
$art->save()
查看 通过id where
\App\Article::find(2)
\App\Article::where('title','gg')->first()
/*先查找 在修改*/
$art->title = 'gg2'
$art->save()
 
$art = \App\Article::find(2)
$art->delete()
 
\App\Article::where('title','gg')->get()
 
 
 
public function list() {
//模型查找
$art = \App\Article::orderBy('created_at', 'desc')->get();

return response()->json(['status'=>1,'msg'=>'success','data'=>$art]);
}
 
  
 
 
posted @ 2018-11-27 11:13  suanmei  阅读(995)  评论(0编辑  收藏  举报