显示函数的特定数据 (laravel)
display specific data from function (laravel)
我会先解释一下我的结构,这样你就能明白我想要得到什么
1 - 车 = 姓名 - 型号 - 年龄
2 - 用户 = 姓名 - 城市 - phone
3 - 预订 = from_date - to_date
4- reservation_user = user_id - reservation_id
5 - car_user = user_id - car_id
在 view.blade 中,我尝试在 table 中显示用户的信息,在该用户请求汽车描述(他想购买)的下方,如果他有他想要的汽车,则在该下方出租(仅出租)
table 用户信息
table 以及他要求的所有汽车(包括租赁和购买)
table 带有汽车 ID 和用户想要汽车的日期
public function show($id)
{
$users = User::with('cars')->where( 'id',$id)->get();
return view('show')->withusers($users);
}
我为销售节省汽车的方式
$id = $new_cars->id;
$user->cars()->attach($id);
出租
$id = $new_car->id;
$R_id = $time->id;
$user->cars()->attach($id);
$user->reservations()->attach($R_id);
问题
两个 table 中都显示了出租汽车,因为在函数中我拉了所有汽车。
问题
如何才能在一个table(第三个table)中只得到预订的车?没有在第二个中显示它们 table
首先,您没有正确设计 car_user
枢轴 table。您将 User and Car
的关系存储在 table 中,但您使用相同的属性存储两种类型的关系数据,用于出售和出租,并且无法区分出售和出租的区别哪一个是出租的。因此,首先,您必须使用该枢轴 table 中的另一个字段来区分两种类型的关系,因此让我们在 table 中添加另一个字段,这将使您能够找出关系输入,例如:
Table car_user
:
user_id | car_id | type
------------------------
1 | 1 | buy
1 | 2 | rent
在这里,类型将用于标识关系的类型,是出租还是出售。因此,当您附加关系时,添加类型字段 (rent/buy) 但在此之前,您必须为它们建立关系。因此,您可以在 User
模型中使用两个单独的关系方法,例如:
// For Rent
public function rentingCars()
{
return $this->belongsToMany(Car::class)
->wherePivot('type', 'rent')
->withPivot('type');
}
// For Buy
public function buyingCars()
{
return $this->belongsToMany(Car::class)
->wherePivot('type', 'buy')
->withPivot('type');
}
// For both types
public function buyingCars()
{
return $this->belongsToMany(Car::class)->withPivot('type');
}
现在,您可以使用如下方式查询特定类型的汽车:
public function show($id)
{
// Load all cars the user wants to buy
$users = User::with('buyingCars')->where( 'id', $id)->get();
// Notice "withUsers" not "withusers" (you've used "withusers")
// Or: view('show')->with('users', $users);
return view('show')->withUsers($users);
// More examples:
// Load all cars the user wants to rent
// $users = User::with('rentingCars')->where( 'id', $id)->get();
// Load all cars the user wants to buy or sell
// $users = User::with('cars')->where( 'id', $id)->get();
}
现在,当您将汽车附加到 User
模型时,您还必须传递 type
字段的值:
// For rent
$user->cars()->attach($car_id, ['type' => 'rent']);
// For buy
$user->cars()->attach($car_id, ['type' => 'buy']);
此外,当您执行以下操作时:
$user = User::with('cars')->find(1);
您可以使用如下方式查看汽车是出租还是购买:
@foreach($user->cars as $car)
{{ $car->name }}
// Either buy or rent
{{ $car->pivot->type }}
@endforeach
我会先解释一下我的结构,这样你就能明白我想要得到什么
1 - 车 = 姓名 - 型号 - 年龄
2 - 用户 = 姓名 - 城市 - phone
3 - 预订 = from_date - to_date
4- reservation_user = user_id - reservation_id
5 - car_user = user_id - car_id
在 view.blade 中,我尝试在 table 中显示用户的信息,在该用户请求汽车描述(他想购买)的下方,如果他有他想要的汽车,则在该下方出租(仅出租)
table 用户信息
table 以及他要求的所有汽车(包括租赁和购买)
table 带有汽车 ID 和用户想要汽车的日期
public function show($id)
{
$users = User::with('cars')->where( 'id',$id)->get();
return view('show')->withusers($users);
}
我为销售节省汽车的方式
$id = $new_cars->id;
$user->cars()->attach($id);
出租
$id = $new_car->id;
$R_id = $time->id;
$user->cars()->attach($id);
$user->reservations()->attach($R_id);
问题
两个 table 中都显示了出租汽车,因为在函数中我拉了所有汽车。
问题
如何才能在一个table(第三个table)中只得到预订的车?没有在第二个中显示它们 table
首先,您没有正确设计 car_user
枢轴 table。您将 User and Car
的关系存储在 table 中,但您使用相同的属性存储两种类型的关系数据,用于出售和出租,并且无法区分出售和出租的区别哪一个是出租的。因此,首先,您必须使用该枢轴 table 中的另一个字段来区分两种类型的关系,因此让我们在 table 中添加另一个字段,这将使您能够找出关系输入,例如:
Table car_user
:
user_id | car_id | type
------------------------
1 | 1 | buy
1 | 2 | rent
在这里,类型将用于标识关系的类型,是出租还是出售。因此,当您附加关系时,添加类型字段 (rent/buy) 但在此之前,您必须为它们建立关系。因此,您可以在 User
模型中使用两个单独的关系方法,例如:
// For Rent
public function rentingCars()
{
return $this->belongsToMany(Car::class)
->wherePivot('type', 'rent')
->withPivot('type');
}
// For Buy
public function buyingCars()
{
return $this->belongsToMany(Car::class)
->wherePivot('type', 'buy')
->withPivot('type');
}
// For both types
public function buyingCars()
{
return $this->belongsToMany(Car::class)->withPivot('type');
}
现在,您可以使用如下方式查询特定类型的汽车:
public function show($id)
{
// Load all cars the user wants to buy
$users = User::with('buyingCars')->where( 'id', $id)->get();
// Notice "withUsers" not "withusers" (you've used "withusers")
// Or: view('show')->with('users', $users);
return view('show')->withUsers($users);
// More examples:
// Load all cars the user wants to rent
// $users = User::with('rentingCars')->where( 'id', $id)->get();
// Load all cars the user wants to buy or sell
// $users = User::with('cars')->where( 'id', $id)->get();
}
现在,当您将汽车附加到 User
模型时,您还必须传递 type
字段的值:
// For rent
$user->cars()->attach($car_id, ['type' => 'rent']);
// For buy
$user->cars()->attach($car_id, ['type' => 'buy']);
此外,当您执行以下操作时:
$user = User::with('cars')->find(1);
您可以使用如下方式查看汽车是出租还是购买:
@foreach($user->cars as $car)
{{ $car->name }}
// Either buy or rent
{{ $car->pivot->type }}
@endforeach