morganchorlton3's avatar

Model Relationships

Hi all I'm trying to set up multiple relationships on my models, Im trying to display my orders in a table along with a users address. ( i have it working for the users model).

(background) I'm creating an e-commerce platform in short

Users model

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'first_name', 'last_name', 'phone_number', '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 Address(){
        return $this->hasOne('App\Address');
    }


}


My Orders Model


class Orders extends Model
{
    protected $primaryKey = 'user_id';

    protected $fillable = [
        'user_id', 'slot_id', 'note', 'total_weight', 'item_count', 'total', 'status'
    ];

    public function Address(){
        return $this->hasOne('App\Address');
    }
}

My Address Model

class Address extends Model
{
    protected $table = 'address';

    protected $primaryKey = 'user_id';
    
    protected $fillable = [
        'post_code', 'address_line_1', 'address_line_2', 'address_line_3', 'city'
    ];

    public function User()
    {
        return $this->hasOne('App\User');
    }

    public function Orders()
    {
        return $this->hasOne('App\Orders');
    }
}

When i try to pull the data in my view

@foreach($orders as $order)
                        <tr>
                            <th>{{ $order->id }}</th>
                            <th>{{ formatPrice($order->total) }}</th>
                            <th>{{ $order->address->post_code }}</th>
                        </tr>
  @endforeach

i Get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'address.orders_user_id' in 'where clause' (SQL: select * from address where address.orders_user_id in (1))

Im not 100% sure what is going on but I dont think it is matching the user_id in the orders table to the user_id in the address table

Thanks in advance for any help

0 likes
8 replies
Sinnbeck's avatar

You must have a query in a controller some where?

Address::where('orders_user_id', $var)->get()
Sti3bas's avatar

@morganchorlton3 why your Address and Orders (stick to conventions and rename it to Order) models has user_id as a primary key?

1 like
Sinnbeck's avatar

Also consider using proper casing (user(), address() and order())

1 like
morganchorlton3's avatar

when i pull my users to display the users and the related address i just use this

$users = User::with('address')->get();
Sinnbeck's avatar

But in the posted code your are iterating over $orders not $users ?

morganchorlton3's avatar

yeah I have basically setup the same relationships as the user model has but it's not working

so in my orders controller im using

$orders = Order::with('address')->get();
Sinnbeck's avatar

Ok. Are you sure this is correct. Normally it is id. If it indeed is true you need to fix both relationships as it tries to find the column based on this key.. So Order table name and primary key added together = orders_user_id (instead of order_id

protected $primaryKey = 'user_id';
1 like
morganchorlton3's avatar

Im, not 100% certain but I'm storing the user_id in the orders table so I can track whos the order is and also each user has an address entry. so im then trying to pull the address matching the user_id not the order id

Please or to participate in this conversation.