You must have a query in a controller some where?
Address::where('orders_user_id', $var)->get()
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
Please or to participate in this conversation.