vendredi 26 juin 2020

how to get data from relation laravel?

i have function ini my controller like this :

public function get_user_order() 
{
    $user = auth()->user()->orders()->with('products')->get();
    $order = $user->products->name;
    dd($order);
}

i want to get logged in user order history.

this is my user model:

class User extends \TCG\Voyager\Models\User
{
public function orders() 
{
    return $this->hasMany('App\Order');
}
}

this is my order model :

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

public function products()
{
    return $this->belongsToMany('App\Product')->withPivot('quantity');
}
}

the problem is how can u get the product name from this relation ? please help me im newb, sorry for my broken english.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire