mercredi 21 août 2019

Can I add multiple foreign key to the same table in one to many relationship in Laravel?

I have two tables:

orders
-----------------
id
client_id
manager_id
team_id 

And

users
---------------------
id
name 
email 

The order table's client_id, manager_id, team_id are the id of the users from user table.

Can I create one to many relationships for all?

If yes, what will be the code?

In Order Model I have this for client

  public function orders (){
  return $this->hasMany('App\Model\Order','client_id','id');

}

And in User Table

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

I get the following result on tinker:

 >>> $order = Order::find(1);
 => App\Models\Order {#3037
 id: 1,
 client_id: 1,
 service_id: 4,
 service: "SEO Technical",
 service_des: "Eum iusto veritatis et optio quia recusandae odit.",
 qty: 2,
 price: "3.00",
 ord_no: null,
 ord_note: null,
 manager_id: null,
 payment_method: "Paypal",
 team_id: 20,
 transaction_id: "5170150321041242",
 coupon: null,
 discount: null,
 invoice_id: "6354469541913",
 payment_status: "approved",
 total: "1149.00",
 status: "schedule",
 schedule_date: "2019-08-26",
 reporting_date: "2019-09-11",
 refund_amt: null,
 created_at: "2019-08-18 05:02:46",
 updated_at: "2019-08-21 06:29:18",
 }
 >>> $order->client;
 => null

I will highly appreciate your help. Thanks in advance.



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

Aucun commentaire:

Enregistrer un commentaire