
01: 触发父级的时候戳
如题目所示,在子模子更新时,能够触发父模子的时候戳。比方 Comment
属于 Post
,偶然更新子模子致使更新父模子时候戳异常有效。比方,当 Comment
模子被更新时,您要自动触发父级 Post
模子的 updated_at
时候戳的更新。Eloquent
让它变得简朴,只需增加一个包括子模子关联称号的 touch
属性。
<?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 或许路由中,还能够用于控制器中带有参数的要领中。
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时的一些小技能的细致内容,更多请关注ki4网别的相干文章!