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

Randy_Johnson's avatar

Laravel Eloquent Relationship Return Null

User

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'full_name',
        'game_name',
        '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 soldier()
    {
        return $this->belongsTo(Soldier::class);
    }
}

Soldier

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Soldier extends Model
{
    use HasFactory;

    protected $fillable = [
        'squad_id',
        'user_id',
        'game_name', 
        'game_name',
        'steam_id',
        'instagram',
        'twitter',
        'facebook',
        'twitch',
    ];

    public function user()
    {
        return $this->hasOne(User::class);
    }

    public function squad()
    {
        return $this->belongsTo(Squad::class);
    }
}

Squad

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Squad extends Model
{
    use HasFactory;

    public function soldiers()
    {
        return $this->hasMany(Soldier::class);
    }

    public function platoon()
    {
        return $this->belongsTo(Platoon::class);
    }
}

DB Tables

mysql> select * from users;
+----+-----------+-----------+-------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| id | full_name | game_name | email       | email_verified_at | password                                                     | remember_token | created_at          | updated_at          |
+----+-----------+-----------+-------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
|  1 | 1         | 1         | [email protected] | NULL              | y$SfDSjE0Ukpv8130kxSxx3.vC.NQQ5rWicdqsGHpg7qrs.1XmX.kWq | NULL           | 2022-01-24 22:49:13 | 2022-01-24 22:49:13 |
|  2 | 2         | 2         | [email protected] | NULL              | yuQH3/OBMZyczfuNl7Dp8Onic5r73rXIhsAp6BcTSSrVCFUq9wv7C | NULL           | 2022-01-24 22:58:40 | 2022-01-24 22:58:40 |
|  3 | 3         | 3         | [email protected] | NULL              | y$zqp6pTnzjF0./I6r8OIMAesw8U6oPoaYpvSS7keVbeJ1gwbeqD.oe | NULL           | 2022-01-24 22:58:59 | 2022-01-24 22:58:59 |
+----+-----------+-----------+-------------+-------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
3 rows in set (0.01 sec)

mysql> select * from soldiers;
+----+----------+---------+-----------+----------+-----------+-----------+----------+--------+---------------------+---------------------+
| id | squad_id | user_id | game_name | steam_id | instagram | twitter   | facebook | twitch | created_at          | updated_at          |
+----+----------+---------+-----------+----------+-----------+-----------+----------+--------+---------------------+---------------------+
|  1 |        1 |       1 | 1         | 2345234  | Jimmy     | 876756543 | Josh     | Josh   | 2022-01-24 22:49:18 | 2022-01-26 20:58:22 |
|  2 |        2 |       2 | 2         | NULL     | NULL      | NULL      | NULL     | NULL   | 2022-01-24 22:58:46 | 2022-01-28 13:47:48 |
|  3 |        1 |       3 | 3         | NULL     | NULL      | NULL      | NULL     | NULL   | 2022-01-24 22:59:05 | 2022-01-26 23:23:07 |
+----+----------+---------+-----------+----------+-----------+-----------+----------+--------+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql>
mysql> select * from squads;
+----+------------+------------+-------------+---------------------+---------------------+
| id | platoon_id | soldier_id | name        | created_at          | updated_at          |
+----+------------+------------+-------------+---------------------+---------------------+
|  1 |          1 |          1 | Armour      | 2022-01-24 22:49:01 | 2022-01-28 20:12:06 |
|  2 |          1 |          2 | Artillery   | 2022-01-24 22:49:01 | 2022-01-28 13:47:56 |
|  3 |          1 |          1 | Engineering | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
|  4 |          1 |          1 | Infantry    | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
|  5 |          1 |          1 | Logistics   | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
|  6 |          1 |          1 | Medical     | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
|  7 |          1 |          3 | Commando    | 2022-01-24 22:49:01 | 2022-01-26 23:19:40 |
|  8 |          2 |          1 | Armour      | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
|  9 |          2 |          1 | Artillery   | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
| 10 |          2 |          1 | Engineering | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |
| 11 |          2 |          1 | Infantry    | 2022-01-24 22:49:01 | 2022-01-24 22:49:01 |

Command used

$soldier = User::find(1);

        dd($soldier->user); 

Return null. 🙃

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

On the soldier it should be

public function user()
    {
        return $this->belongsTo(User::class);
    }

And hasOne on the user model

Tray2's avatar

You are trying to get the user from the user.

Shouldn't it be

$soldier = Solidier::find(1);

        dd($soldier->user); 

?

1 like

Please or to participate in this conversation.