生成Model
php artisan make:model Article
生成在app目錄下
上面注意規范:通常model類用單數,controller類用復數
app\Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
//
}
使用tinker 命令行交互界面
$article = new App\Article;
$article->title = 'first title';
$article->body= 'content';
// 保存數據到數據庫
$article->save();
//查找一條數據
$firstArticle = App\Article::find(1); //方法1
$firstArticle = App\Article::first(); //方法2
//where查詢
// 注意最后的get(),在get()之前都是處理數據,并沒有請求數據庫
$firstArticle = App\Article::where('body', '=', 'content')->get();
// 批量插入
$article = App\Article::create([
'title' => 'second title',
'body' => 'content'
]);
//批量更新
$article->update(['title' => 'update the first title']);
批量插入和批量更新需要注意對應的model層的$fillable
Paste_Image.png