<?phpnamespace 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 或者路由中,还可以用于控制器中带有参数的方法中。
return redirect()->action('somecontroller@method', ['param' => $value]);
05: 如何使用 withdefault() 避免在关系中出现的错误当一个关系被调用时,如果它不存在,则会出现致命的错误,例如 $post->user->name ,可以使用 withdefault() 来避免。
/** 获取文章作者 */ public function user() { return $this->belongsto('app\user')->withdefault(); }
06: 在模版中两个平级的 $loop 变量在 blade 的 foreach 中,即使在两次循环中,依然可以通过使用 $loop 变量来获取父级变量。
@foreach ($users as $user) @foreach ($user->posts as $post) @if ($loop->parent->first) this is first iteration of the parent loop. @endif @endforeach @endforeach
07: 修改查询结果在执行 eloqument 查询后,你可以使用 map() 来修改行。
$users = user::where('role_id', 1)->get()->map(function (user $user) { $user->some_column = some_function($user); return $user;});
08: 轻松的使用 dd()在 eloqument 的最后加上 $test->dd(),来代替 dd($result)。
// 优化前$users = user::where('name', 'taylor')->get();dd($users);// 优化后$users = user::where('name', 'taylor')->get()->dd();
09: use hasmany to savemany.如果有 hasmany() 关联关系,和想要从父类对象中保存许多子类对象,可以使用 savemany() 来达到你想要的效果。
$post = post::find(1);$post->comments()->savemany([ new comment(['message' => 'first comment']), new comment(['message' => 'second comment']),]);
10: 在 model::all() 中指定列当你使用 eloqument 的 model::all() 时,你可以指定要返回的列。
$users = user::all(['id', 'name', 'email']);
11: blade 中的 @auth你可以使用 @auth 指令来代替 if 语句来检查用户是否经过身份验证。
典型的方法:@if(auth()->user()) // the user is authenticated. @endif
简短的方法:@auth // the user is authenticated. @endauth
12: 预览邮件而不发送如果你使用 mailables 来发送你的邮件,你可以预览它们而不发送出去。
route::get('/mailable', function () { $invoice = app\invoice::find(1); return new app\mail\invoicepaid($invoice);});
13: hasmany 的特定检查在 eloquent 的 hasmany() 关系中,你可以筛选出具有 n 个子记录数量的记录。
// author -> hasmany(book::class) $authors = author::has('books', '>', 5)->get();
14: 恢复多个软删除如果记录使用了软删除,那么你就可以一次恢复多条软删除记录。
post::withtrashed()->where('author_id', 1)->restore();
15: 带时区的迁移列迁移文件不仅有 timestamps() 时间戳,还有 timestampstz() 带有时区的时间戳。
schema::create('employees', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->timestampstz();});
16: 视图文件是否存在?你知道还可以检查视图文件是否存在吗?
if (view()->exists('custom.page')) { // load the view}
17: 组中的路由组在路由文件中,你可以为一个路由组创造一个组,还可以为其指定特定的中间件。
route::group(['prefix' => 'account', 'as' => 'account.'], function() { route::get('login', 'accountcontroller@login'); route::get('register', 'accountcontroller@register'); route::group(['middleware' => 'auth'], function() { route::get('edit', 'accountcontroller@edit'); });});
18: eloquent 中的日期时间方法whereday() , wheremonth() , whereyear() , wheredate() , wheretime() 这些方法皆为 eloquent 中检查日期的方法。
$products = product::wheredate('created_at', '2018-01-31')->get(); $products = product::wheremonth('created_at', '12')->get(); $products = product::whereday('created_at', '31')->get(); $products = product::whereyear('created_at', date('y'))->get(); $products = product::wheretime('created_at', '=', '14:13:58')->get();
19: 在 eloquent 关系中使用 orderby()你可以在 eloquent 关系中直接指定 orderby() 。
public function products(){ return $this->hasmany(product::class);}public function productsbyname(){ return $this->hasmany(product::class)->orderby('name');}
20: 无符号整型对于迁移的外键,不要使用 integer() , 而是使用 unsignedinteger() 或者是 integer()->unsigned() ,否则将会出现一系列的错误。
schema::create('employees', function (blueprint $table) { $table->unsignedinteger('company_id'); $table->foreign('company_id')->references('id')->on('companies'); });
更多laravel相关技术文章,请访问laravel教程栏目进行学习!
以上就是使用laravel时的一些小技巧的详细内容。
