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

shahr's avatar
Level 10

Trying to get property of non-object

I have three tables users, soldiers, resumes

users table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

soldiers table

public function up()
{
    Schema::create('soldiers', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });
}

resumes table

public function up()
{
    Schema::create('resumes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('image')->nullable();
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('job_title')->nullable();
        $table->bigInteger('soldier_id')->unsigned();
        $table->foreign('soldier_id')->references('id')->on('soldiers')->onDelete('cascade');
        $table->timestamps();
}

HomeController.php

public function index()
{
    $soldiers = Soldier::all();
    return view('Home.index', compact('soldiers'));
}

User.php

public function resume()
{
    return $this->belongsTo(Resume::class);
}

Resume.php

public function soldier()
{
    return $this->belongsTo(Soldier::class);
}

index.blade.php

<div class="col-md-3">
    <div class="form-group">
        <label for="soldier_id">Solider Status</label>
        <select id="soldier_id" name="soldier_id" class="form-control">
            @foreach($soldiers as $soldier)
                <option value="{{ $soldier->id }}" {{ auth()->check() && auth()->user()->resume->soldier->id == $soldier->id ? 'selected' : '' }}>
                    {{ $soldier->name }}
                </option>
            @endforeach
        </select>
    </div>
</div>

I get this error

Trying to get property of non-object

0 likes
4 replies
shahr's avatar
Level 10

Yes only users and soldiers tables.

ChinaEmporer's avatar

<option value="{{ $soldier->id }}>

Remove the auth stuff and see if you get a return from that

<option value="{{ $soldier->id }} {{ $soldier->name }}>
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

User.php

public function resume()
{
    return $this->hasOne(Resume::class);
}

or

public function resumes()
{
    return $this->hasMany(Resume::class);
}

depending on which it is. (note that here it is plural)

Always test your relationships with tinker before building into view code.

1 like

Please or to participate in this conversation.