AlmoDev
AlmoDev

Reputation: 969

display specific data from function (laravel)

I'll explain my structure first so you can understand what I'm trying to get

1 - Car = name - model - age

2 - User = name - city - phone

3 - Reservation = from_date - to_date

4- reservation_user = user_id - reservation_id

5 - car_user = user_id - car_id

In the view.blade I tried to show the user's info in a table and below that the user request car's descriptions ( he wants to buy) and below that if he has a car he wants to rent (ONLY rent)

table user's info

table with all the cars he request ( both rent and buy)

table with car id and the dates the user want the car from-to

 public function show($id)
{
    $users = User::with('cars')->where( 'id',$id)->get();


    return view('show')->withusers($users);
}

the way I saved the cars for sales

$id = $new_cars->id;

    $user->cars()->attach($id);

for rent

    $id = $new_car->id;
    $R_id = $time->id;
    $user->cars()->attach($id);


    $user->reservations()->attach($R_id);

problem

the cars for rent is displaying in both tables because in the function I pulled all the cars.

Question

how can I get only the cars with reservation in one table(third table)? Without displaying them in the second table

Upvotes: 1

Views: 1221

Answers (1)

The Alpha
The Alpha

Reputation: 146191

First of all, you didn't design your car_user pivot table properly. You are storing the relationship for User and Car in that table but you are storing both types of relationship data using same properties, for sell and rent and there's no way to distinguish the difference as which one is for sell and which one is for rent. So, at first, you've to make a difference between both types of relationships using another field in that pivot table, so let's add another field in the table which will allow you to find out the relationship type, for example:

Table car_user:

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

Here, the type will be used to identify the type of the relationship whether it's a rent or sell. So, when you attaching the relationship, add the type field (rent/buy) but before that, you've to make the relationship for them. So, you may use two separate relationship methods in User model, for example:

// 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');
}

Now, you can query for certain types of car using something like this:

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();

}

Now, when you attaching the cars to User model, you have to pass the value for the type field as well:

// For rent
$user->cars()->attach($car_id, ['type' => 'rent']);

// For buy
$user->cars()->attach($car_id, ['type' => 'buy']);

Also, when you do something like this:

$user = User::with('cars')->find(1);

You can check wheather a car is for rent or buy using something like this:

@foreach($user->cars as $car)

    {{ $car->name }}

    // Either buy or rent
    {{ $car->pivot->type }}

@endforeach

Upvotes: 1

Related Questions