I have the following models in my application:
User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function customer_details()
{
return $this->hasOne(CustomerDetails::class);
}
}
CustomerDetails.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CustomerDetails extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
Now I am trying to run the following query:
$user = User::select('email', 'phone')
->where('id', Auth::user()->id)
->with('customer_details')
->first();
Now what I want it to do is to select only the email and phone number from my users table and first_name, last_name from my customer_details table but whenever I try this it always returns customer_details as null
.
return $this->hasOne('App\Models\CustomerDetails', 'user_id');
It should be :return $this->hasOne('App\Models\CustomerDetails', 'user_id','id');
$user = Auth::user()->load("customer_details");