為一種純粹使用URL網址來傳遞資訊的機制,比如/photos/1/edit
//routes/web.php
Route::resource('photos','App/Http/Controllers/PhotoController');
php artisan make:controller PhotoController --resource
//使用Item物件的屬性來新增
$item = new Item;
$item->title = '手機一台';
$item->price = '15000';
$item->pic = 'https://member.ithome.com.tw/avatars/88232?s=ithelp';
$item->enabled = 1;
$item->size = 'L';
$item->email = 'admin@demo.com';
$item->save();
//使用Item建構子來新增
$item = new Item([
'title' => '手機2台',
'price' => '15000',
'pic' => 'https://member.ithome.com.tw/avatars/88232?s=ithelp',
'enabled' => 1,
'size' => 'L',
'email' => 'admin222@demo.com'
]);
$item->save();
//使用Item的create()來新增
$data = [
'title' => '手機4台',
'price' => '15000',
'pic' => 'https://member.ithome.com.tw/avatars/88232?s=ithelp',
'enabled' => 1,
'size' => 'L',
'email' => 'admin444@demo.com'
];
$item = Item::create($data);//要注意Mass Assignment保護機制
當鍵入資料是以陣列的方式,而且是使用模型的create()的話,通常都會觸發Mass Assignment防護機制,預設所有的欄位都會被保護
如要關閉欄位的保護,進到Model檔案內,如App\Models\Item.php,加入以下屬性
protected $fillable = ['白名單欄位名'];
protected $guarded = ['黑名單欄位名'];
//用流水號進行查詢,找不到就算了
$item = Item::find($id);
//用流水號進行查詢,找不到會報錯
$item = Item::findOrFail($id);
//查詢有啟用的商品,取出第一個
$item = Item::where('enabled',true)->first();
//取得items表格的所有資料
$items = Item::get();
//取得items表格的所有資料,並依created_at欄位由大至小排序
$items = Item::orderBy('created_at','desc')->get();
//取得items表格的所有enabled欄位為true的資料
$items = Item::where('enabled',true)->get();
得到資料參考後,設定欄位新屬性值,最後呼叫save()更新永續層(資料庫)
第一種作法
$item = Item::find($id);
$item->price = 0;
$item->save();
第二種作法
$song = Song::updateOrCreate(['id'=>$song->id],['name'=>'Song99']);
//使用資料參考來刪除
$item->delete();
//使用主鍵來刪除
Item::destroy($item->id);
為你省去下達find(),用主鍵找資料的步驟
Step 1.將路徑參數改為Model名稱
//routes/web.php
Route::get('/itemdelete/{item}','App\Http\Controllers\ItemController@destroy');
Step 2.變更Action的參數列,補上模型類別與名稱
//app/Http/Controllers/ItemController.php
public function destroy(Item $item)
Step 3.為模型類別加入白/黑名單
//app/Models/Item.php
//設定黑名單為空
protected $guarded = [];
//設定白名單欄位
protected $fillable = ['title','price'...];
Q1.如何判斷查詢完所給的變數是集合還是物件?
A.問自己所使用的查詢方法有無可能拿到多筆資料,只要有可能,一定是集合,比如all().get()屬之,否則則必然為物件
用Token取代Git帳密的做法
https://你的帳號:你的TOKEN@github.com/javck/voyage.git
關於Restful的說明
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing