vendredi 26 juin 2020

how to get data from eager laravel?

i want to get logged in user history order, this my controller :

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

i get this error alert( Illuminate\Database\Eloquent\Collection::products does not exist.)

this is my user model :

class User extends \TCG\Voyager\Models\User
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function orders() 
{
    return $this->hasMany('App\Order');
}
}

and this is my order model :

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

}

the question is , how can i get the product information and store it to array ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire