samedi 27 juin 2020

how to check relation value laravel?

im new using laravel and i get some problem.

i have function in my controller :

$user = auth()->user()->orders()->with('products')->get();
    //dd($user);
    if(!empty($user)){
        $order = $user->last()->products->pluck('details')->toArray();
        $details = implode(" ", $order);
        // dd($details);

        return $details;
    }else{
        //nothing
    }

i want check, if logged in user have relations product run the code and return value, if empty do nothing.

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 to check logged in user has relation product or not, if yes execute code and return value, if no do nothing. sorry for my broken english ,thanks.



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

Aucun commentaire:

Enregistrer un commentaire