Laravel中都有哪些实用的小技巧可以分享?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1346个文字,预计阅读时间需要6分钟。
相关专题
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { /** * 涉及到的所有关联关系。 * * @var array */ protected $touches = ['post']; /** * 获取评论所属的文章。 */ public function post() { return $this->belongsTo('App\Post'); } }
02: 预加载精确的列
在使用预加载时,可以从关系中获取指定的列。
$users = App\Book::with('author:id,name')->get();
03: 为单个请求验证用户身份
你可以使用 Auth::once() 来为单个请求验证用户的身份,此方法不会使用 Cookie 会话。这意味着此方法可能有助于构建无状态 API 。
if (Auth::once($credentials)) { // }
04: 重定向到带有参数的控制器方法中
你不仅可以将 redirect() 方法用于用户特定的 URL 或者路由中,还可以用于控制器中带有参数的方法中。
本文共计1346个文字,预计阅读时间需要6分钟。
相关专题
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { /** * 涉及到的所有关联关系。 * * @var array */ protected $touches = ['post']; /** * 获取评论所属的文章。 */ public function post() { return $this->belongsTo('App\Post'); } }
02: 预加载精确的列
在使用预加载时,可以从关系中获取指定的列。
$users = App\Book::with('author:id,name')->get();
03: 为单个请求验证用户身份
你可以使用 Auth::once() 来为单个请求验证用户的身份,此方法不会使用 Cookie 会话。这意味着此方法可能有助于构建无状态 API 。
if (Auth::once($credentials)) { // }
04: 重定向到带有参数的控制器方法中
你不仅可以将 redirect() 方法用于用户特定的 URL 或者路由中,还可以用于控制器中带有参数的方法中。

