Wednesday, March 1, 2017

Trying to understand table relationship in Laravel

I just started studying Laravel and as my starter I am following this tutorial video series. https://youtu.be/Y1TkLkGhHwA

I am trying to understand how these two table relation works at Model level.

Order.php

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

I kind of understand this is how Laravel makes relationship between two tables by using belongsTo method. (I just accept it as it is)

But when I use this model in other model, customer() function defined in Order.php becomes a property(?) of object instance($order->customer->name). This is the part I don't get or just lack of OOP knowledge...

Routes\web.php

Route::get('orders',function(){
    $orders = App\Order::all();
    foreach($orders as $order){
        echo $order->name . 'belongs to' . $order->customer->name;




via norixxx

Advertisement