//創(chuàng)建模型文件
php artisan make:model Article
//Laravel會在app目錄下生成一個Article.php的模型文件皇耗。但是我們?yōu)榱朔奖愣嗤В话銜⒛P臀募旁贛odel目錄下出革,
//所以需要在生成文件的時候指定命名空間
php artisan make:model Models/Article
//Laravel會自動生成Models目錄和Article.php文件燕耿,如果你想在生成模型文件的同時生成遷移文件,可以在后面加上-m
php artisan make:model Models/Article -m
//參數(shù)配置
//模型文件采用單數(shù)形式命名漱牵,而數(shù)據(jù)表采用復(fù)數(shù)形式命名生音。所以一個Article模型默認對應(yīng)Articles 數(shù)據(jù)表脉让,如果我們在
//開發(fā)中需要指定表的話。
//指定表名
protected $table = 'article2';
//指定主鍵
protected $primaryKey = 'article_id';
//是否開啟時間戳
protected $timestamps = false;
//設(shè)置時間戳格式為Unix
protected $dateFormat = 'U';
//過濾字段跷睦,只有包含的字段才能被更新
protected $fillable = ['title','content'];
//隱藏字段
protected $hidden = ['password'];
以上信息來自:https://blog.csdn.net/qq_30202073/article/details/84835473
laravel分為三大數(shù)據(jù)庫操作(DB facade[原始查找]筷弦,查詢構(gòu)造器[Query Builder],Eloquent ORM):
use Illuminate\Support\Facades\DB;
1.DB facade[原始查找]
$results = DB::select('select * from users where id = :id', ['id' => 1]);
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
不返回值:
DB::statement('drop table users');
返回自增id:
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
$affected = DB::update('update users set votes = 100 where name = ?', ['John']);
更詳細可跳轉(zhuǎn)至https://www.cnblogs.com/fps2tao/p/7859322.html