#laravel #eloquent #pivot-table
#laravel #красноречивый #сводная таблица
Вопрос:
Я использую в сводной модели. Ранее я назначил user_id
is routekey, но теперь я хочу также сопоставить привязку модели flats ( flat_id
). Я также приложил скриншот вывода.
Мой URL-адрес таков http://127.0.0.1:8000/admin/apartments/1/flats/2/flat-members/3
namespace Apppivotes;
use IlluminateDatabaseEloquentRelationsPivot;
use AppmodelsRoleUser;
use AppmodelsFlat;
use AppmodelsAssociation;
use AppmodelsRole;
use AppUser;
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(ApppivotesFlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first());
return ApppivotesFlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}
потому dd()
что я получаю правильные данные, но если я возвращаю их, кажется, что они становятся пустыми.
Ответ №1:
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 ApppivotesFlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}