我在Pivot模型中使用。在以前,我分配的user_id
是routekey,但现在我也想匹配模型绑定平面(flat_id
)。我也附上了输出屏幕截图。
我的网址是http://127.0.0.1:8000/admin/apartments/1/flats/2/flat-members/3
namespace App\pivotes;
use Illuminate\Database\Eloquent\Relations\Pivot;
use App\models\RoleUser;
use App\models\Flat;
use App\models\Association;
use App\models\Role;
use App\User;
class FlatUser extends Pivot
{
protected $table = 'flat_user';
public $timestamps = false;
public function getRouteKeyName()
{
return 'user_id';
}
public function flat()
{
return $this->belongsTo(Flat::class, 'flat_id', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function role()
{
return $this->belongsTo(Role::class, 'role_id', 'id');
}
}
更新,我对此感到厌倦
在RouteServiceProvider
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
dd(\App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first());
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}
对于dd()
,我得到了正确的数据,但是如果我返回它,它似乎将变成空的。
答案 0 :(得分:0)
I am using RouteServiceProvider.php. I check flat_id and route parameter flats whether it matches, giving particular pivot model data. In below added my codes.
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}