mardi 16 octobre 2018

Laravel rental car search

I'm trying to make a rent-a-car website and i'm stucked at the search functionality.

The input will be a pick up date and drop off date and the result should be a list of cars that are available in that period.

This are my tables:

Cars: id, name, category etc.

Placement: id, client_name, client_second_name etc.

Invoice: id(irelevant), placement_id, car_id, pick_up_date, drop_off_date.

This are my relationships:

For Car model:

public function invoices(){
        return $this->belongsTo('App\Invoice');
    }
    public function placements(){
        return $this->belongsTo('App\Placement');
    }

For Placement model:

public function invoice(){
        return $this->belongsTo('App\Invoice');
    }
    public function car(){
        return $this->hasOne('App\Car');
    }

For Invoice model:

public function cars(){
        return $this->hasMany('App\Car');
    }

    public function placements(){
        return $this->hasMany('App\Placement');
    }

I saw a question on this problem, but i don't really know how to apply that response:

$date1 = Carbon::parse('20.02.2018')->startOfDay();
$date2 = Carbon::parse('23.02.2018')->endOfDay();
$available = Car::whereDoesntHave('rentals', function($q) use($date1, $date2) {
    $q->whereBetween('starting_date', [$date1, $date2])
      ->orWhereBetween('ending_date', [$date1, $date2])
      ->orWhere(function($q) use($date1, $date2) {
          $q->where('starting_date', '<', $date1)
            ->where('ending_date', '>', $date2);
      });
})
->get();

If i try to run this: $available = Car::doesntHave('placements')->get();, just for test purpose, i get this error:

Column not found: 1054 Unknown column 'cars.placements_id' in 'where clause' (SQL: select * from `cars` where not exists (select * from `placements` where `cars`.`placements_id` = `placements`.`id`)) 

Can you help me,please? If not with an exact response, maybe with an explanation?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire