0
I am hitting myself here with a simple query on Laravel, I want to make a query on the client table and bring the customer’s address together.
Note: Whenever I try it brings the address id and not the client id
class Address extends Model
{
protected $fillable = [
'street',
'number',
'neighborhood',
'complement',
'reference',
'zip_code',
'phone',
'cel_phone',
'city',
'state',
];
public function client()
{
return $this->hasMany(Client::class);
}
}
class Client extends Model
{
protected $fillable = [
'name',
'email',
'cpf',
'rg',
'address_id',
];
public function address()
{
return $this->belongsTo(Address::class);
}
}
Example from SQL:
SELECT * FROM 'clients' as A
INNER JOIN 'addresses' as B on a.address_id = b.id
WHERE a.id =" $id;
How are you doing? If you did so
Cliente::with('address')->get()
? Or didn’t try anything?– novic
What is the type of relationship between
Client
andAddress
? is 1 client for many addresses? I did not understand well which relation you want to use, first defines which relationship exists between these two tables !– novic
Each customer has only one address, but there is the possibility of more than one customer living at the same address! You would be right this way?
– Moacyr Santana