Laravel中9個(gè)不經(jīng)常用的小技巧匯總
前言
眾所周知Laravel是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架(PHP Web Framework)。下面這篇文章主要給大家總結(jié)了一些Laravel不經(jīng)常用的小技巧,下面話不多說了,來一起看看詳細(xì)的介紹吧
1. 更新父表的timestamps
如果你想在更新關(guān)聯(lián)表的同時(shí),更新父表的timestamps,你只需要在關(guān)聯(lián)表的model中添加touches屬性。
比如我們有Post和Comment兩個(gè)關(guān)聯(lián)模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { /** * 要更新的所有關(guān)聯(lián)表 * * @var array */ protected $touches = ['post']; /** * Get the post that the comment belongs to. */ public function post() { return $this->belongsTo('App\Post'); } }
2. 懶加載指定字段
$posts = App\Post::with('comment:id,name')->get();
3. 跳轉(zhuǎn)指定控制器并附帶參數(shù)
return redirect()->action('SomeController@method', ['param' => $value]);
4. 關(guān)聯(lián)時(shí)使用withDefault()
在調(diào)用關(guān)聯(lián)時(shí),如果另一個(gè)模型不存在,系統(tǒng)會拋出一個(gè)致命錯(cuò)誤,例如 $comment->post->title,那么我們就需要使用withDefault()
... public function post() { return $this->belongsTo(App\Post::class)->withDefault(); }
5. 兩層循環(huán)中使用$loop
在blade的foreach中,如果你想獲取外層循環(huán)的變量
@foreach ($users as $user) @foreach ($user->posts as $post) @if ($loop->parent->first) This is first iteration of the parent loop. @endif @endforeach @endforeach
6. 瀏覽郵件而不發(fā)送
如果你使用的是mailables來發(fā)送郵件,你可以只展示而不發(fā)送郵件
Route::get('/mailable', function () { $invoice = App\Invoice::find(1); return new App\Mail\InvoicePaid($invoice); });
7. 通過關(guān)聯(lián)查詢記錄
在hasMany關(guān)聯(lián)關(guān)系中,你可以查詢出關(guān)聯(lián)記錄必須大于5的記錄
$posts = Post::has('comment', '>', 5)->get();
8. 軟刪除
查看包含軟刪除的記錄
$posts = Post::withTrashed()->get();
查看僅被軟刪除的記錄
$posts = Post::onlyTrashed()->get();
恢復(fù)軟刪除的模型
Post::withTrashed()->restore();
9. Eloquent時(shí)間方法
$posts = Post::whereDate('created_at', '2018-01-31')->get(); $posts = Post::whereMonth('created_at', '12')->get(); $posts = Post::whereDay('created_at', '31')->get(); $posts = Post::whereYear('created_at', date('Y'))->get(); $posts = Post::whereTime('created_at', '=', '14:13:58')->get();
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
相關(guān)文章
關(guān)于laravel-admin ueditor 集成并解決刷新的問題
今天小編就為大家分享一篇關(guān)于laravel-admin ueditor 集成并解決刷新的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10關(guān)于Laravel-admin的基礎(chǔ)用法總結(jié)和自定義model詳解
今天小編就為大家分享一篇關(guān)于Laravel-admin的基礎(chǔ)用法總結(jié)和自定義model詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能示例
這篇文章主要介紹了thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能,結(jié)合實(shí)例形式分析了thinkPHP5框架上進(jìn)行ajax分頁操作的具體步驟、實(shí)現(xiàn)代碼與相關(guān)操作方法,需要的朋友可以參考下2018-06-06Laravel實(shí)現(xiàn)自定義錯(cuò)誤輸出內(nèi)容的方法
這篇文章主要介紹了Laravel實(shí)現(xiàn)自定義錯(cuò)誤輸出內(nèi)容的方法,結(jié)合實(shí)例形式分析了Laravel自定義錯(cuò)誤輸出信息的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Codeigniter里的無刷新上傳的實(shí)現(xiàn)代碼
這篇文章主要介紹了Codeigniter里的無刷新上傳的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04windows server 2008/2012安裝php iis7 mysql環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了windows server 2008/2012安裝php iis7 mysql環(huán)境搭建教程 ,需要的朋友可以參考下2016-06-06