【问题标题】:Problems accessing model relationship Eloquent Model Laravel 5.8访问模型关系的问题 Eloquent Model Laravel 5.8
【发布时间】:2019-08-11 19:00:06
【问题描述】:

我是早期的 Laravel 开发人员(学习),在处理 Eloquent 模型之间的关系时遇到了一些麻烦。我将不胜感激一些帮助。

创建一个包含一堆食谱的网站,我目前正致力于根据类别(早餐、午餐、晚餐等)分离不同的视图。在视图中,每个菜谱都可以显示菜谱标题和菜谱的创建者。我有一个用户模型、配方模型和类别模型。在我的早餐.blade.php 视图中,我试图在 @if 语句中访问 $recipe->category->name 以整理出相关的食谱。它给我一个错误,并说“此集合实例上不存在属性 [类别]”。 我来自控制器、视图和模型的代码发布在下面:

配方模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Recipe extends Model
{
//Table Name
protected $table = 'recipes';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;

public function user(){
    return $this->belongsTo('App\User', 'id');
}

public function category(){
    return $this->belongsTo('App\Category');
}
}

分类模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $table = 'categories';

public function recipes(){
    return $this->hasMany('App\Recipe');
}
}

CategoryController 的顶部

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Recipe;
use App\Category;
use App\User;
use App\Auth;

class CategoryController extends Controller
{
public function showBreakfast(){
    $recipes = Recipe::all();
    $users = User::all();
    return view('recipes.categories.breakfast')->with('recipes', 
 $recipes);
}

还有我的@if 语句和@foreach 循环的开头(这是引发错误的地方)

@section('content')
<section>
<div class="container">
<div class="row">
@if(count($recipes) > 0 && $recipes->category->name == 'Breakfast') 
@foreach($recipes as $recipe) 

如果有任何帮助,我将不胜感激!我一直在搞乱控制器、Eloquent 模型,并将变量传递给视图,有时我让它工作,有时我没有。我似乎不了解如何有效使用它的基本规则。再次,我非常感谢任何帮助! :) 如果我需要澄清这个问题,请告诉我!

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    当您使用all() 方法时,您会收到来自该模型的整个集合。 $recipes 变量是一组对象(配方),而不仅仅是一个对象(配方),因此您应该使用 foreach 来访问每个配方中的类别名称:

    @if(count($recipes) > 0)
        @foreach($recipes as $recipe) 
            @if($recipe->category->name == 'Breakfast') 
            // do something
            @endif
        @endforeach
    @endif
    

    你在人际关系中犯了一个错误

    public function user(){
        return $this->belongsTo('App\User', 'user_id');
    }
    
    public function category(){
        return $this->belongsTo('App\Category', 'category_id');
    }
    

    您应该将外键作为第二个参数传递,而不是 id。

    另外,您忘记提及哪些属性是可填充的:

    $fillable = ['name',...
    
    ]
    

    【讨论】:

    • 您好!感谢你的快速回复!我做了这些更改,但它没有修复错误:(我仍然得到“此集合实例上不存在属性 [类别]”,它指的是我的 @if 语句中的 $recipes->category->name。你觉得我能做什么?
    • 是的。我也尝试过这个解决方案,它给了我“方法 Illuminate\Database\Eloquent\Collection::category 不存在”。该方法确实存在......另一个奇怪的事情是,如果我简单地运行我的@foreach循环,将$recipes作为$recipe并将它们打印出来,然后我可以访问$recipe->category->name..你为什么认为是?
    • 你用的是我的 if statament 还是你的?在您的第一条评论中,您使用的是您的 if,而不是我的
    • 这就是我在回答中告诉你的,你不能在对象数组中使用关系,你应该使用 foreach 来访问每个配方中的每个类别名称。类别关系适用于一个对象,而不是整个集合。这就是它给出这个错误的原因。
    • 哦,现在我明白了!太好了,现在效果很好!谢谢您的帮助!第一次误会了!谢谢!! :)
    猜你喜欢
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多