从昨天起我就面临着这个问题。我有一个名为resources的db表,它有一个外键链接到另一个名为category的表。

我试图检索刀片视图中的description字段,但是我得到了以下错误:
试图获取非对象的属性“描述”。
我的刀片视图:
@extends('templates.header')
@section('section')
<div class="p-10 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5">
@foreach($resources as $resource)
<div class="max-w-sm rounded overflow-hidden shadow-lg">
{{-- <img class="w-full" src="#" alt="Mountain"> --}}
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">
{{ $resource->name }}
</div>
<p class="text-gray-700 text-base">
{{ $resource->description }}
</p>
</div>
<div class="px-6 pt-4 pb-2">
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">{{ $resource->categor->description }}</span>
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">{{ $resource->status }}</span>
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">{{ $resource->centerId }}</span>
<button type="submit" class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Prenota
</button>
</div>
</div>
@endforeach
</div>
@endsection我的资源模型:
namespace App\Models;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Resource extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'category',
'inventoryN',
'status',
'centerId',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
];
public function category()
{
return $this->hasOne(Category::class, 'id', 'category');
}
}我的分类模型:
namespace App\Models;
use App\Models\Resource;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $table = 'categories';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'description',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
];
public function resource()
{
return $this->belongsTo(Resource::class, 'category');
}
}最后,我的ResourceController:
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Resource;
use Illuminate\Http\Request;
class ResourceController extends Controller
{
public function index()
{
$resources = Resource::with('category')->get();
return view('resources', compact('resources'));
}
}这是“$resources”的dd:$resources的dd
发布于 2021-05-24 14:26:42
你在这里有几个错误。
第一个是在刀刃上。你需要修正一个错误
$resource->categor->description
// should be
$resource->category->description然后,我建议通过将资源列从category更改为category_id来更改模式。
这将有助于Laravel自动填充以下代码段中的值。
接下来,你需要修复你们的关系。
在参考资料模型中,您需要
public function category()
{
return $this->hasOne(Category::class);
}我已经删除了第二个和第三个参数,这些参数是由Laravel自动填充的;由于您使用的是Laravel的命名方案,所以您不需要它。
你之前说的是表是类别的单数变体,但事实并非如此。
然后,您需要将分类模型更改为
public function resource()
{
return $this->belongsTo(Resource::class);
}之所以失败,是因为Laravel返回了null,因为列名不太正确。
只要在数据库中有一个更标准的命名结构就更容易了,因为它可以帮助其他开发人员,并且在使用Laravel时可以使您的生活更轻松。
发布于 2021-05-24 09:36:00
您的类别查看中有一个错误。我想这就是问题所在。
{{ $resource->categor->description }}
vs.
{{ $resource->category->description }}https://stackoverflow.com/questions/67669627
复制相似问题