一点一点地,但现在我遇到了这个错误“尝试读取空值上的属性“名称””,并且在我的数据库中我有信息,因此它必须出现在我的视图中,请帮助我的数据库数据已满,我的城市国家/地区都组织得很好,但在从 cars_id 源表调用数据时仍然给出错误
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css\app.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<title>prueba</title>
</head>
<body>
<div class="tittle">
<h1>hola</h1>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">nombre</th>
<th scope="col">correo</th>
<th scope="col">ciudad</th>
<th scope="col">pais</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->city->name}}</td>
<td>{{$user->city && $user->city->country ? $user->city->country->name : 'N/A'}}</td>
</tr>
@endforeach
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
景色
型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\Country;
class City extends Model
{
use HasFactory;
public function User()
{
return $this->hasMany(User::class);
}
public function country()
{
return $this->belongsTo(country::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\City;
class Country extends Model
{
use HasFactory;
public function city()
{
return $this->hasMany(city::class);
}
}
?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\City;
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<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* 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',
'password' => 'hashed',
];
public function city(){
return $this->belongsTo(city::class);
}
}
您没有正确处理
null
链接。你已经得到了这个:
{{ $user->city && $user->city->country ? $user->city->country->name : 'N/A' }}
这没问题,但是在它的正上方,你有这个:
{{ $user->city->name }}
没有检查...如果任何用户的城市是
null
,那么 null->name
将触发,这是无效的。
由于您已将其标记为
laravel-10
,我认为这需要 PHP 8
,因此您可以使用 ?->
运算符:
{{ $user?->city?->name ?? 'N/A' }}
{{ $user?->city?->country?->name ?? 'N/A' }}
如果你不能使用该运算符,那么你需要链接它:
{{ $user->city ? $user->city->name : 'N/A' }}
{{ $user->city && $user->city->country ? $user->city->country->name : 'N/A' }}