vendredi 22 septembre 2017

¿Create model with multiple relationships?

Basically I have the following models

  • Applications
  • Products
  • Users

These models are related in the following way

A request contains a type of product and this request is made by a user

Then the relationships for model requests are

//Usuario que hace la solicitud
public function user(){
  return $this->belongsTo(User::class);
}


public function product(){
      return $this->belongsTo(Product::class);
    }

Relationships for the user model

public function solicitudes(){
  return $this->hasMany(Solicitud::class);
}

Relationships for the model Product

  public function solicitudes(){
  return $this->hasMany(Solicitud::class);
}

The doubt arises since the request model has two relations with user and product then what would be the best way to create the request model since now as I am doing it is as follows.

$user->solicitudes()->create(['product_id'=>1,'size_dd'=>'22','snapshot'=>1]);

as it is basically observed I am passing the product id to the * create * method so that it does not generate errors, but I want to know what would be the best way to do it considering that it has double relation.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire