【问题标题】:Laravel Relationship: Trying to get property 'description' of non-objectLaravel 关系:试图获取非对象的属性“描述”
【发布时间】:2021-08-12 14:45:06
【问题描述】:

我从昨天开始就面临这个问题。 我有一个名为resources 的数据库表,有一个外键链接到另一个名为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: dd of $resources

【问题讨论】:

  • 因为$resource-&gt;category返回null所以$resource-&gt;category-&gt;description抛出这个错误
  • 如果我使用 "$resource->category" 我可以看到类别的 id
  • 我认为任何一个表中都需要有一个默认值。
  • 另外,最好更改架构,使category(外来ID)实际上是category_id...

标签: php laravel eloquent foreign-keys relational-database


【解决方案1】:

您的类别查看中有错字。我认为这就是问题所在。

{{ $resource->categor->description }}

vs. 

{{ $resource->category->description }}

【讨论】:

  • 我错过了一个 y,但仍然得到同样的错误
  • 你能添加整个错误吗?通常,您可以查看行号以查明问题。可能没有类别关系,所以$resource-&gt;category结果为null或为空。
【解决方案2】:

这里有一些错误。

第一个是在刀片中。你需要修正一个错字

$resource->categor->description
// should be
$resource->category->description

然后我建议通过将资源列从 category 更改为 category_id 来更改架构。
这将有助于 Laravel 自动填充以下 sn-ps 中的值。

接下来,你需要修复你们的关系。

在资源模型中,您需要

public function category()
{
    return $this->hasOne(Category::class);
}

我删除了第二个和第三个参数,这些是 Laravel 自动填充的;由于您使用的是 Laravel 的命名方案,因此您不需要它。
您之前所说的是该表是类别的单数变体,但事实并非如此。

然后您需要将您的类别模型更改为

public function resource()
{
    return $this->belongsTo(Resource::class);
}

失败的原因是 Laravel 返回 null,因为列名不太正确。


在您的数据库中拥有更标准的命名结构会更容易,因为它可以帮助其他开发人员,并让您在使用 Laravel 时更轻松。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 2020-09-28
    • 2018-02-10
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多