前言
众所周知Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。下面这篇文章主要给大家总结了一些Laravel不经常用的小技巧,下面话不多说了,来一起看看详细的介绍吧
1. 更新父表的timestamps
如果你想在更新关联表的同时,更新父表的timestamps,你只需要在关联表的model中添加touches属性。
比如我们有Post和Comment两个关联模型
<"color: #ff0000">2. 懒加载指定字段
$posts = App\Post::with('comment:id,name')->get();3. 跳转指定控制器并附带参数
return redirect()->action('SomeController@method', ['param' => $value]);4. 关联时使用withDefault()
在调用关联时,如果另一个模型不存在,系统会抛出一个致命错误,例如 $comment->post->title,那么我们就需要使用withDefault()
... public function post() { return $this->belongsTo(App\Post::class)->withDefault(); }5. 两层循环中使用$loop
在blade的foreach中,如果你想获取外层循环的变量
@foreach ($users as $user) @foreach ($user->posts as $post) @if ($loop->parent->first) This is first iteration of the parent loop. @endif @endforeach @endforeach6. 浏览邮件而不发送
如果你使用的是mailables来发送邮件,你可以只展示而不发送邮件
Route::get('/mailable', function () { $invoice = App\Invoice::find(1); return new App\Mail\InvoicePaid($invoice); });7. 通过关联查询记录
在hasMany关联关系中,你可以查询出关联记录必须大于5的记录
$posts = Post::has('comment', '>', 5)->get();8. 软删除
查看包含软删除的记录
$posts = Post::withTrashed()->get();查看仅被软删除的记录
$posts = Post::onlyTrashed()->get();恢复软删除的模型
Post::withTrashed()->restore();9. Eloquent时间方法
$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();总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 赞多《序章》[FLAC/分轨][129.13MB]
- 群星《完美的她 影视原声带》[320K/MP3][51.98MB]