【问题标题】:display specific data from function (laravel)显示来自函数的特定数据(laravel)
【发布时间】:2017-05-03 18:32:16
【问题描述】:

我将首先解释我的结构,以便您了解我想要得到的内容

1 - 汽车 = 名称 - 型号 - 年龄

2 - 用户 = 姓名 - 城市 - 电话

3 - 预订 = from_date - to_date

4- reservation_user = user_id - reservation_id

5 - car_user = user_id - car_id

在 view.blade 中,我尝试在表格中显示用户的信息,在表格下方显示用户请求汽车的描述(他想购买),在下方显示他想要租的汽车(只租)

表用户信息

他要求的所有汽车的桌子(租和买)

带有汽车 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);

问题

出租汽车显示在两个表中,因为在函数中我拉出了所有汽车。

问题

我怎样才能在一张桌子(第三张桌子)中只获得预订的汽车?没有在第二个表中显示它们

【问题讨论】:

    标签: php mysql laravel model laravel-5.3


    【解决方案1】:

    首先,您没有正确设计您的car_user 数据透视表。您将User and Car 的关系存储在该表中,但是您使用相同的属性存储两种类型的关系数据,用于出售和出租,并且无法区分哪个是出售的,哪个是出租的。因此,首先,您必须使用该数据透视表中的另一个字段来区分两种类型的关系,因此让我们在表中添加另一个字段,以便您找出关系类型,例如:

    表car_user:

    user_id | car_id | type
    ------------------------
    1       |    1   | buy
    1       |    2   | rent
    

    在这里,类型将用于标识关系的类型,无论是出租还是出售。因此,当您附加关系时,请添加类型字段(租/买),但在此之前,您必须为它们建立关系。因此,您可以在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
    

    【讨论】:

    • 非常感谢你,伙计:)我学到的东西比我看过的任何视频都多
    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2018-10-30
    • 2023-03-03
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多