jeudi 19 avril 2018

eloquent one-To-Many-To-Many

Hi everyone I start with laravel and eloquent and already I have a problem Here is my problem

I have users, each user can have multiple vehicles and a vehicle can have multiple rental.

I want to list all rentals of each vehicle belonging to the current user

user model

class User extends Authenticatable { use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];


public function vehicule()
{
    return $this->hasMany('App\Models\Vehicule');
}
vehicule model :

use Illuminate\Database\Eloquent\Model;

class Vehicule extends Model { protected $fillable = [

'user_id',
'published',
'rented'

];

public function user()
{
    return $this->belongsTo('App\User');
}

public function rental()
{
    return $this->hasMany('App\Models\Rental');
}

}

Rental model :

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Rental extends Model {

protected $fillable = [
    'from_date',
    'to_date',
    'amount_total',
    'vehicule_id'
];


public function vehicule()
{
    return $this->belongsTo('App\Models\Vehicule');
}

}

Dear expert i need your help



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2F1mpWb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire