Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

how do i get the user_id properties attached to the order

My DB

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('users_id');
	    $table->string('seller_id');
            $table->string('users_email',100);
            $table->string('name',100);
            $table->string('address');
            $table->timestamps();
        });

my controller

 public function vendor_order()
    {
        $user = auth()->user()->id;
        $cart = User::findorfail(auth()->user()->id)->cart;
        $messages = User::findorfail($user)->latestmessaging->count();
        $slug = User::where('slug', auth()->user()->slug)->firstOrFail();
        $subcategory = SubCategory::with(['productCategory']);
        $order = User::findorfail($user)->orders;
        dd($order);
        // $cat = $subcategory->productCategory;
        return view('user.vendor_order', compact('slug', 'cart', 'messages', 'subcategory', 'order'));
    }

user model

public function orders() {
        return $this->hasMany(Orders_model::class, 'seller_id', 'id');
    }

This relationship gives me

Illuminate\Database\Eloquent\Collection {#1532 ▼
  #items: array:5 [▼
    0 => App\Orders_model {#1533 ▼
      #table: "orders"
      #primaryKey: "id"
      #fillable: array:12 [▶]
      #connection: "mysql"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:21 [▼
        "id" => 70
        "users_id" => 4
        "seller_id" => "5"
        "product_id" => "4"
        "address" => "Abuja"
        "name" => "Manager"
        "mobile" => "0sss"
        "email" => "[email protected]"
        "check" => null
        "fee" => null
        "deliver_method" => null
        "delivery_fee" => null
        "delivery_pick_up_station" => null
        "shipping_charges" => 0.0
        "coupon_code" => "NO Coupon"
        "coupon_amount" => "0"
        "order_status" => "success"
        "payment_method" => "Paystack"
        "grand_total" => "400"
        "created_at" => "2020-10-23 06:10:16"
        "updated_at" => "2020-10-23 06:10:16"
      ]
      #original: array:21 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => App\Orders_model {#1534 ▶}
    2 => App\Orders_model {#1535 ▶}
    3 => App\Orders_model {#1536 ▶}
    4 => App\Orders_model {#1537 ▶}
  ]
}
0 likes
1 reply
tykus's avatar

If you want only the user_id then it is a property on each Order_model instance in the Collection. If you want the User instance, then you need a second relationship on the Order_model model. It is difficult to understand really what you want/need however.

Your code is needlessly verbose, and not expressive; it is difficult to understand exactly what you are trying to retrieve because your variable names are inconsistent, and you are making multiple queries unnecessarily for the same User instance as auth()->user() already is; and you have an uncompleted Eloquent Query for $subcategory. For example, a simple refactor removes 4 such queries:

public function vendor_order()
{
        $cart = auth()->user()->cart;
        $messagesCount = auth()->user()->latestmessaging->count();
        // $slug = User::where('slug', auth()->user()->slug)->firstOrFail(); // this is auth()->user()
        $subcategory = SubCategory::with(['productCategory']); // this is not completed
        $order = auth()->user()->orders;

        return view('user.vendor_order', compact('cart', 'messagesCount', 'subcategory', 'order'));
 }

Please or to participate in this conversation.