# 2020-12-31 Model
###### tags: `Laravel`
### 建立Model
1. php artisan make:model 表格名稱
* model命名規則
* 1. 大寫開頭
* 2. 與表格名稱一致,但字尾不用+s
* 程式會自行判斷命名,去選擇對應的表格
2. 建立位置 app->表格名稱

3.
```php=
class Products extends Model
{
//
protected $table = 'products';
}
```
---
命名規則:
| Model: | Migration: | Controller: |Viwes:(不強制) |
| ------------------- | ---------------------- | ----------- | --- |
| 1. 大駝峰 | 1. 底線連接2個以上單字 | 1.___Controller(大) | 1. 小寫 |
| 2. 與表格名稱一致 | 2. 複數 | 2. 單數 | 2. 底線連接 |
| 3. 單數|| 3. 複數|
| EX: History | EX: histories | EX: HistoryController | EX: history |
| 命名方式: | |
| --------- | ------------ |
| 大駝峰 | ProductList |
| 小駝峰 | productList |
| 底線 | product_list |
| 建立一格聯絡我們(contact us): | migration | Model | Controller | view |
| ----------------------------- | -------------------------------------------------- | -------------------------------- | ----------------------------------------------- | -------------------------------------------------------- |
| 建立表格:命名 | php artisan make:migration create_contact_us_table | php artisan make:model ContactUs | php artisan make:controller ContactUsController | contact_us.blade.php資料夾名稱contact_us/index.blade.php |
| 路徑 | database/migrations/contact_us.php | app/ContactUs.php | app/Http/Controllers/ContactUsController | resources/views/contact_us.blade.php|
|功能描述|將資料使用程式碼與資料庫連結,執行表格CRUD相關操作|負責與資料庫建立溝通,限制資料的存取,建立資料表之間的關聯|與該網址相關之業務邏輯,負責透過Model與資料庫溝通存取資料,返回資料或頁面/route透過controller將資料回傳至前端頁面|畫面的模板
---
## 欲操作事項
> C 利用Model 新增資料
> R Product::get();
> U 更新ID=1的資料.name->xyz
> D 刪除資料
#### 建立新增產品步驟
>1. 建立新增產品資料Route
>
>`Route::get('products_create',"ProductController@create");`
>
>
>2. Controller 建立新增產品的 function
>3. 撰寫利用model新增資料的功能
>4. 返回目前所有產品資料
方法1
```php=
public function create()
{
Product::insert([
'name'=>'123',
'type'=>'456',
'img'=>'789',
'description'=>'456',
'price'=>'456',
'url'=>'123',
]);
$products = Product::get();
return $products;
}
```
> 方法2
```php=
public function create()
{
Product::create([
'name'=>'123',
'type'=>'456',
'img'=>'789',
'description'=>'456',
'price'=>'456',
'url'=>'123',
]);
$products = Product::get();
return $products;
}
使用create語法新增資料時,Model須設定fillable 或 guarded
class Product extends Model
{
//
protected $table = 'products';
protected $fillable = ['name','type','img','url','description','price'];
// 白名單
寫在上面的資料才可以被插入資料庫
}
```
> UPDATE
> 更新ID=1的資料.name->xyz
```php=
public function update()
{
方法1.
$product = Product::find(4);
// find(函數) 函數=id
$product->name="xyz";
$product->save();
// 抓取到符合鍵值,foreach
// $變數 = Model::where('欄位名稱',值)
$product =Product::where('price','50')->get();
$product->name="123";
$product->save();
$product為陣列無法更改,需透過foreach遍歷出資料。
// $products = Product::where('name','123')->get();
// foreach ($products as $products) {
// $products->name="999";
// $products->save();
// }
// 找到符合key value的第一筆資料
// $product = Product::where('name','456')->first();
// $product->name="123";
// $product->save();
//刪除
// $product = Product::find(2)->delete();
// $product->delete();
return $product ;
}
```
>刪除資料
```php=
$product = Product::find(2)->delete();
or
$product->delete();
return $product ;
```
拍攝白板推薦的app
office lens
- [ ] 代辦事項
- [ ] 123
```mermaid
graph TD
a(開始)-->b[下一步]-->c{判斷式}
c--no-->D
c--yes-->E
```
[流程圖](https://mermaid-js.github.io/mermaid/#/)
長網址使用方式
[貓]
[cat]
[cat]:(https://obs.line-scdn.net/0hgMPYYgdGOGJtIBL-_2VHNVd2Ow1eTCthCRZpYS5OZlYSRyw8BkZ0DE4kYwYQFn88AxFzDE4hI1MQEnlmVkd0/w644)