LoginSignup
2
1

More than 5 years have passed since last update.

Laravel Route パラメータのバインディング

Posted at
Route::bind("company_code", function ($value) {
    $comp = CompanyEloquent::where("code",$value)->first();
    return $comp;
});

Route::bind("staff_code", function ($value) {
    $comp = StaffEloquent::where([
        "company_id" => ????,
        "staff_code" => $value // 複合ユニークのため
    ])->first();
    return $comp;
});

こんな感じのアレが、

use \Illuminate\Routing\Route;

Route::bind("company_code", function ($value) {
    $comp = CompanyEloquent::where("code",$value)->first();
    return $comp;
});

Route::bind("staff_code", function ($value,Route $route) {
    $comp = StaffEloquent::where([
        "company_id" => $route->parameter("company_code")->id,
        "staff_code" => $value // 複合ユニークのため
    ])->first();
    return $comp;
});

みたいに書けたりする。

便利。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1