101.設(shè)置表主鍵
protected $primaryKey='art_id';
102.設(shè)置默認(rèn)時間戳
public $timestamps=false;
103.批量賦值保護(hù)
protected $guarded=[];
104.為模型指定不同的鏈接
protected $connection = 'connection-name';
105.取回多個模型
$flights = Flight::all();
106.增加額外限制
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
107.通過主鍵取回一個模型
$data=Navs::find($id);
108.取回符合限制條件的第一個模型
$flight = App\Flight::where('active', 1)->first();
109.拋出未找到異常
$model = App\Flight::findOrFail(1);
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
在找不到模型時拋出一個異常纺荧,如果捕捉到異常占贫,則自動送回http 404響應(yīng)給用戶
109.取回集合
$count = App\Flight::where('active', 1)->count();
$max = App\Flight::where('active', 1)->max('price');
110.基本添加
public function store(Request $request)
{
// 驗證請求...
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
}
111.基本更新
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save()
112.批量賦值白名單
protected $fillable = ['name'];
113.刪除模型
$flight = App\Flight::find(1);
$flight->delete();
114.通過鍵刪除模型
App\Flight::destroy(1);
App\Flight::destroy([1, 2, 3]);
App\Flight::destroy(1, 2, 3);
115.通過查找刪除模型
$re=Navs::where('nav_id',$id)->delete();
116.Eloquent集合
$users = App\User::where('active', 1)->get();
117.Eloquent集合所有方法###
- all
- avg
- chunk
- collapse
- combine
- contains
- count
- diff
- diffKeys
- each
- every
- except
- filter
- first
- flatMap
- flatten
- flip
- forget
- forPage
- get
- groupBy
- has
- implode
- intersect
- isEmpty
- keyBy
- keys
- last
- map
- max
- merge
- min
- only
- pluck
- pop
- prepend
- pull
- push
- put
- random
- reduce
- reject
- reverse
- search
- shift
- shuffle
- slice
- sort
- sortBy
- sortByDesc
- splice
- sum
- take
- toArray
- toJson
- transform
- union
- unique
- values
- where
- whereLoose
- whereIn
- whereInLoose
- zip
118.集合對象-map
$data=Links::orderBy('link_order','asc')->get();
$names=$data->map(function($name){
return $name->link_name;
});
dd($names);
輸出
Collection {#245 ▼
#items: array:3 [▼
0 => "百度"
1 => "雅虎"
2 => "google"
]
}
119.集合對象-all(所有)
$data=Links::orderBy('link_order','asc')->get();
$names=$data->all(function($name){
return $name->link_name;
});
dd($names);
輸出
array:3 [▼
0 => Links {#258 ?}
1 => Links {#259 ?}
2 => Links {#260 ?}
]
120.集合對象-avg(平均值)
$data=Links::orderBy('link_order','asc')->get();
$names=$data->avg(function($name){
return $name->link_order;
});
dd($names);
121.集合對象-chunk(將集合拆成多個指定大小的較小集合)
$names=$data->chunk(3);
dd($names->toArray());
輸出
array:5 [▼
0 => array:3 [?]
1 => array:3 [?]
2 => array:3 [?]
3 => array:3 [?]
4 => array:1 [?]
]
這個方法適用于網(wǎng)格系統(tǒng)如Bootstrap的視圖
@foreach ($products->chunk(3) as $chunk)
<div class="row">
@foreach ($chunk as $product)
<div class="col-xs-4">{{ $product->name }}</div>
@endforeach
</div>
@endforeach
122.集合對象-collapse(將多個數(shù)組組成的集合合成單個數(shù)組集合)
$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
$collapsed = $collection->collapse();
dd($collapsed->all());
輸出
array:9 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
6 => 7
7 => 8
8 => 9
]
123.集合對象-combine(將集合的值作為鍵厘灼,合并另一個數(shù)組或者集合作為鍵對應(yīng)的值)
對應(yīng)英文:The combine method combines the keys of the collection with the values of another array or collection:
$collection = collect(['name', 'age']);
$combined = $collection->combine(['George', 29]);
$combined->all();
輸出
array:2 [▼
"name" => "George"
"age" => 29
]
124.集合對象-contains(判斷集合是否含有指定項目)
$collection = collect(['name' => 'Desk', 'price' => 100]);
$collection->contains('Desk');
輸出
true
125.集合對象count(項目總數(shù))
$data=Links::orderBy('link_order','asc')->get();
$contains=$data->count();
dd($contains);
126.集合對象-diff(將集合與其他集合或者php數(shù)組比較)
$collection = collect([1, 2, 3, 4, 5]);
$diff = $collection->diff([2, 4, 6, 8]);
$diff->all();
127.集合對象-each(遍歷集合中的項目夹纫,并將之傳入回調(diào)函數(shù))
$collection = $collection->each(function ($item, $key) {
//
});
128.集合對象-every(創(chuàng)建每包含第n個元素的新集合)
$collection = collect(['a', 'b', 'c', 'd', 'e', 'f']);
$collection->every(4);
輸出
Collection {#245 ▼
#items: array:2 [▼
0 => "a"
1 => "e"
]
}
129.集合對象-except(返回集合中除了指定鍵的所有項目)
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->except(['price', 'discount']);
$filtered->all();
輸出
['product_id' => 1, 'name' => 'Desk']
130.集合對象-filter(使用回調(diào)函數(shù)篩選集合,只留下通過判斷測試的項目)
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($item) {
return $item > 2;
});
$filtered->all();
131.集合對象first(返回集合第一個通過制定測試的元素)
$data=Links::orderBy('link_order','asc')->first();
dd($data);
輸出
Links {#256 ▼
#table: "links"
#primaryKey: "link_id"
+timestamps: false
#guarded: []
#connection: null
#keyType: "int"
#perPage: 15
+incrementing: true
#attributes: array:5 [?]
#original: array:5 [?]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
132.集合對象-flatten(將多維集合轉(zhuǎn)為一維集合)
$collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]);
$flattened = $collection->flatten();
$flattened->all();
133.集合對象-flip(將集合中的鍵和對應(yīng)的值進(jìn)行互換)
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$flipped = $collection->flip();
dd($flipped->all());
輸出
array:2 [▼
"taylor" => "name"
"laravel" => "framework"
]
134.通過集合的鍵來移除集合中的一個項目
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$collection->forget('name');
$collection->all();
輸出
[framework' => 'laravel']
注意:與大多數(shù)其它集合的方法不同设凹,forget 不會返回修改過后的新集合舰讹;它會直接修改調(diào)用它的集合。
135.集合對象-has(檢查集合中是否含有指定的鍵)
$collection = collect(['account_id' => 1, 'product' => 'Desk']);
$collection->has('email');
136.集合對象-implode(合并集合中的項目)
$collection = collect([
['account_id' => 1, 'product' => 'Desk'],
['account_id' => 2, 'product' => 'Chair'],
]);
$collection->implode('product', ', ');
輸出
Desk, Chair
或者
collect([1, 2, 3, 4, 5])->implode('-');
輸出
'1-2-3-4-5'
137.集合對象-intersect(移除任何指定數(shù)組或者集合內(nèi)沒有的數(shù)值)
$collection = collect(['Desk', 'Sofa', 'Chair']);
$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);
$intersect->all();
輸出
[0 => 'Desk', 2 => 'Chair']
相當(dāng)于去交集
138.集合對象-last(返回集合中闪朱,最后一個通過指定測試的元素)
$data=Links::orderBy('link_order','asc')->get();
$keys=$data->last(function($key,$value){
return $key<4;
});
dd($keys);
139.集合對象-max(計算指定鍵的最大值)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->max();
dd($max);
輸出最大鍵對應(yīng)的結(jié)果
140.集合對象-only(返回集合中指定鍵的所有項目)
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->only(['product_id', 'name']);
$filtered->all();
輸出
['product_id' => 1, 'name' => 'Desk']
141.集合對象-pluck(獲取所有集合中指定鍵對應(yīng)的值)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->pluck('link_name','link_id');
dd($max);
輸出
Collection {#245 ▼
#items: array:13 [▼
6 => "百度"
8 => "百度1"
10 => "百度2"
12 => "百度3"
14 => "百度4"
1 => "百度"
4 => "雅虎"
7 => "google"
9 => "google1"
11 => "google2"
13 => "google3"
15 => "google4"
2 => "google"
]
}
142.集合對象-pop(移除并返回集合最后一個項目)
$collection = collect([1, 2, 3, 4, 5]);
$collection->pop();
$collection->all();
輸出
[1, 2, 3, 4]
143.集合對象-prepend(集合前面添加一個項目)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->prepend(0);
dd($data);
輸出
Collection {#267 ▼
#items: array:14 [▼
0 => 0
1 => Links {#268 ?}
2 => Links {#269 ?}
3 => Links {#270 ?}
4 => Links {#271 ?}
5 => Links {#272 ?}
6 => Links {#273 ?}
7 => Links {#274 ?}
8 => Links {#275 ?}
9 => Links {#276 ?}
10 => Links {#277 ?}
11 => Links {#278 ?}
12 => Links {#279 ?}
13 => Links {#280 ?}
]
}
也可以傳遞第二個參數(shù)月匣,指定這個值對應(yīng)的鍵
$data=Links::orderBy('link_order','asc')->get();
$max=$data->prepend(0,'aaa');
dd($data);
輸出
Collection {#267 ▼
#items: array:14 [▼
"aaa" => 0
0 => Links {#268 ?}
1 => Links {#269 ?}
2 => Links {#270 ?}
3 => Links {#271 ?}
4 => Links {#272 ?}
5 => Links {#273 ?}
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
]
}
144.集合對象-pull(把鍵對應(yīng)的值從集合中移除并返回)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->pull(5);
dd($data);
輸出
Collection {#267 ▼
#items: array:12 [▼
0 => Links {#268 ?}
1 => Links {#269 ?}
2 => Links {#270 ?}
3 => Links {#271 ?}
4 => Links {#272 ?}
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
]
}
145.集合對象push(在集合后面添加一個元素)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->push(5);
dd($data);
輸出
Collection {#267 ▼
#items: array:14 [▼
0 => Links {#268 ?}
1 => Links {#269 ?}
2 => Links {#270 ?}
3 => Links {#271 ?}
4 => Links {#272 ?}
5 => Links {#273 ?}
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
13 => 5
]
}
146.集合對象-put(在集合內(nèi)設(shè)置一個鍵值)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->put('aa','bbb');
dd($data);
輸出
Collection {#267 ▼
#items: array:14 [▼
0 => Links {#268 ?}
1 => Links {#269 ?}
2 => Links {#270 ?}
3 => Links {#271 ?}
4 => Links {#272 ?}
5 => Links {#273 ?}
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
"aa" => "bbb"
]
}
也可以更換已有鍵的值
$data=Links::orderBy('link_order','asc')->get();
$max=$data->put(5,'bbb');
dd($data);
輸出
Collection {#267 ▼
#items: array:13 [▼
0 => Links {#268 ?}
1 => Links {#269 ?}
2 => Links {#270 ?}
3 => Links {#271 ?}
4 => Links {#272 ?}
5 => "bbb"
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
]
}
147.集合對象-random(從集合中隨機(jī)取出一個項目)
$data=Links::orderBy('link_order','asc')->get();
$max=$data->random();
dd($max);
輸出
Links {#275 ▼
#table: "links"
#primaryKey: "link_id"
+timestamps: false
#guarded: []
#connection: null
#keyType: "int"
#perPage: 15
+incrementing: true
#attributes: array:5 [?]
#original: array:5 [?]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
147.集合對象-reduce(將集合縮縮減到單個數(shù)值,該方法將每次迭代的結(jié)果傳入到下一次迭代)
$collection = collect([1, 2, 3]);
$total = $collection->reduce(function ($carry, $item) {
return $carry + $item;
});
輸出
6
148.集合對象-reject(以指定的回調(diào)函數(shù)篩選集合奋姿,該回調(diào)函數(shù)應(yīng)該對希望從最終集合移除掉的項目返回true)
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->reject(function ($item) {
return $item > 2;
});
$filtered->all();
輸出
[1, 2]
148.集合對象-reverse(倒轉(zhuǎn)集合內(nèi)項目的順序)
$collection = collect([1, 2, 3, 4, 5]);
$reversed = $collection->reverse();
$reversed->all();
輸出
[5, 4, 3, 2, 1]
149.集合對象-shift(移除并返回集合的第一個項目)
$data=Links::orderBy('link_order','asc')->get();
$total = $data->shift();
dd($data);
輸出
Collection {#267 ▼
#items: array:12 [▼
0 => Links {#269 ?}
1 => Links {#270 ?}
2 => Links {#271 ?}
3 => Links {#272 ?}
4 => Links {#273 ?}
5 => Links {#274 ?}
6 => Links {#275 ?}
7 => Links {#276 ?}
8 => Links {#277 ?}
9 => Links {#278 ?}
10 => Links {#279 ?}
11 => Links {#280 ?}
]
}
150.集合對象-slice(返回集合從指定索引開始的一部分切片)
$data=Links::orderBy('link_order','asc')->get();
$total = $data->slice(4);
dd($total);
輸出
Collection {#252 ▼
#items: array:9 [▼
4 => Links {#272 ?}
5 => Links {#273 ?}
6 => Links {#274 ?}
7 => Links {#275 ?}
8 => Links {#276 ?}
9 => Links {#277 ?}
10 => Links {#278 ?}
11 => Links {#279 ?}
12 => Links {#280 ?}
]
}
151.集合對象-splice(返回指定的索引開始的一小切片項目锄开,原本的集合也會被刪除)
$collection = collect([1, 2, 3, 4, 5]);
$chunk = $collection->splice(2);
$chunk->all();
輸出
// [3, 4, 5]
原來的集合被切除
$collection = collect([1, 2, 3, 4, 5]);
$chunk = $collection->splice(2);
$collection->all();
輸出
// [1, 2]
152.集合對象-sum(返回集合內(nèi)所有項目的總和)
collect([1, 2, 3, 4, 5])->sum();
輸出
15
153.集合對象-take(返回有著指定數(shù)量項目的集合)
$collection = collect([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(3);
$chunk->all();
輸出
[0, 1, 2]
也可以傳入負(fù)數(shù)獲取從集合后面來算指定數(shù)量的項目
$collection = collect([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(-2);
$chunk->all();
輸出
// [4, 5]
154.集合對象-toArray(將集合轉(zhuǎn)換成純php數(shù)組)
$data=Links::orderBy('link_order','asc')->get();
$total = $data->toArray();
dd($total);
輸出
array:13 [▼
0 => array:5 [▼
"link_id" => 6
"link_name" => "百度"
"link_title" => "全球最大的中文搜索引擎"
"link_url" => "https://www.baidu.com"
"link_order" => 1
]
1 => array:5 [?]
2 => array:5 [?]
3 => array:5 [?]
4 => array:5 [?]
5 => array:5 [?]
6 => array:5 [?]
7 => array:5 [?]
8 => array:5 [?]
9 => array:5 [?]
10 => array:5 [?]
11 => array:5 [?]
12 => array:5 [?]
]
155.集合對象-toJSON(將集合轉(zhuǎn)換成json)
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toJson();
輸出
'{"name":"Desk","price":200}'
156.集合對象-zip(將集合與指定數(shù)組同樣索引的值合并在一起)
$collection = collect(['Chair', 'Desk']);
$zipped = $collection->zip([100, 200]);
$zipped->all();
輸出
[['Chair', 100], ['Desk', 200]]
157.設(shè)置session函數(shù)
session(['user'=>$user]);
158.銷毀這個session
session(['user'=>'']);
159.模板引入css和js
<script type="text/javascript" src="{{asset('resources/views/admin/lib/html5.js')}}"></script>
160.模板頁面書寫url
{{url('admin/code')}}