2017年9月27日 星期三

Laravel,新增model,ORM方式操作資料庫


//檔案:指令
php artisan make:model Post


資料夾的app出現一個post.php



內容如下



<?php

//檔案:app/post.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
//
}


 





 



<?php
//檔案:app/post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
//parents
{
protected $table = 'posts';
//若沒有此行,定義的table會是posts(class名稱的小寫複數型)

protected $primaryKey = 'id';
//預設值,其實也可以不用打

}


//檔案:routes/web.php

use App\Post;

Route::get('/find', function () {
$posts = Post::all();
foreach ($posts as $post) {
return $post->title;
}
});


瀏覽器輸入:public/find



呈現出所有文章的標題





 



//檔案:routes/web.php

use App\Post;

Route::get('/find', function () {
$post = Post::find(2);

return $post->title;
});


瀏覽器輸入:public/find



呈現出編號2的文章標題





 



//檔案:routes/web.php

use App\Post;

Route::get('/findwhere', function () {
$post = Post::where('id',2)->get();
//->orderBy('id','desc')->take(1)
return $post;
});


瀏覽器輸入:public/findwhere



呈現出編號2的文章物件





 



//檔案:routes/web.php

use App\Post;

Route::get('/findmore', function () {
$post = Post::findOrFail(1);
return $post;
});


瀏覽器輸入:public/findmore



呈現錯誤,因為編號一被我刪掉了



若不希望沒文章而出現空畫面時使用





 



//檔案:routes/web.php

use App\Post;

Route::get('/basicinsert', function () {
$post = new Post;
$post->title = '又有新的標題惹';
$post->content = '又有新的內容惹';
$post->save();
});


瀏覽器輸入:public/basicinsert



呈現空白,資料庫多一筆資料





 



//檔案:routes/web.php

use App\Post;

Route::get('/basicinsert2', function () {
$post = Post::find(2);
//static method
$post->title = '標題要被改掉囉';
$post->content = '內容要被改掉囉';
$post->save();
});


瀏覽器輸入:public/basicinsert2



呈現空白,資料庫編號二的資料標題與內容被改掉





 



<?php
//檔案:app/post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
//parents
{
protected $fillable = ['title','content'];
}


//檔案:routes/web.php

use App\Post;

Route::get('/create', function () {
Post::create(['title'=>'更更新的標題','content'=>'更更新的內容']);
});


瀏覽器輸入:public/create



呈現空白,資料庫新增一筆資料





 



//檔案:routes/web.php

use App\Post;

Route::get('/update', function () {
Post::where('id',2)->where('is_admin',0)->update(['title'=>'我來改標題囉','content'=>'我來改內容囉']);
});


瀏覽器輸入:public/update



呈現空白,資料庫編號二資料被改掉





 



//檔案:routes/web.php

use App\Post;

Route::get('/delete', function () {
$post=Post::find(2);
$post->delete();
});


瀏覽器輸入:public/delete



呈現空白,資料庫編號二資料被刪掉





 



//檔案:routes/web.php

use App\Post;

Route::get('/delete2', function () {
Post::destroy(3);
});


瀏覽器輸入:public/delete2



呈現空白,資料庫編號三資料被刪掉





 



//檔案:routes/web.php

use App\Post;

Route::get('/delete2', function () {
Post::destroy([4,5]);
// Post::where('is_admin',0)->delete();
});


瀏覽器輸入:public/delete2



呈現空白,資料庫編號4.5資料被刪掉





 



//檔案:app/post.php

protected $table ='posts';


多一層保障





 



//檔案:app/user.php

protected $hidden = [
'password', 'remember_token',
];


不讓user的密碼被洩漏





 



//下指令

php artisan tinker


//tinker

$post= new App\Post; //定義變數
$post->create(['user_id'=>'1','title'=>'test','content'=>'hello']); //增加資料
//註:app/post.php裡面,fillable的東西才可以寫入


沒有留言:

張貼留言