0%

Attempt to read property "name" on null or Undefined property Illuminate\Database\Eloquent\Relations\BelongsTo::$name

We have two model class, one is User the other one is Division, user belongs to one division and division has many users.
Users table only has one column named division_id, the other division information like “division name” we can get from Divisions table.

1
2
3
4
5
6
7
8
9
10
11
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->unsignedBigInteger('division_id')->nullable();
$table->timestamps();
});

1
2
3
4
5
6
Schema::create('divisions', function (Blueprint $table) {
$table->id();
$table->string('name', 128);
$table->timestamps();
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?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;
use Illuminate\Database\Eloquent\Model; //by default this line is not here, you need to add it manually

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

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'division_id',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];

public function division()
{
return $this->belongsTo(Division::class);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace App\Models;

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

class Division extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];


public function users()
{
return $this->hasMany(User::class);
}
}

In the blade file if you try to use user->division()->name it will throw an error.

1
#message: "Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name"

Instead you should use user->division->name to get the name of the division, but if you directly use the property name name it will throw an other error, beacuse the default Laravel user model do not import Eloquent Model.

1
Attempt to read property "name" on null

To solve this problem we need to add a this line to your User model file.

 use Illuminate\Database\Eloquent\Model;