【问题标题】:Trying to get property 'name' of non-object (View: C:\xampp\htdocs\COMPLIANCE-FAQ\resources\views\respones\show.blade.php)试图获取非对象的属性“名称”(查看:C:\xampp\htdocs\COMPLIANCE-FAQ\resources\views\respones\show.blade.php)
【发布时间】:2020-05-23 20:35:16
【问题描述】:

我有 4 个表(用户、ask_questions、respones、类别)。 我试图显示每个回答完成的问题,但我收到了这个错误: 试图获取非对象的属性“名称”(查看:C:\xampp\htdocs\COMPLIANCE-FAQ\resources\views\respones\show.blade.php)

模型respone.php

{
    public $table = 'respones';

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'updated_at',
        'created_at',
        'deleted_at',
        'category_id',
        'author_name',
        'text_answer',
        'author_email_id',
        'ask_question_id',
    ];

    public static function boot()
    {
        parent::boot();

        Respone::Observe(new \App\Observers\ResponeObserver );

    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function author_email()
    {
        return $this->belongsTo(User::class, 'author_email_id');
    }

    public function ask_question()
    {
        return $this->belongsTo(AskQuestion::class, 'ask_question_id');
    }
}

AskQuestion.php

protected $dates = [
    'created_at',
    'updated_at',
    'deleted_at',
];

protected $fillable = [
    'email',
    'created_at',
    'updated_at',
    'deleted_at',
    'text_question',
    'objet_question',
    'assigned_to_user_id',
];

public static function boot()
{
    parent::boot();

    AskQuestion::Observe(new \App\Observers\AskQuestionObserver );

    static::addGlobalScope(new CollaborateurScope);
}

/**
* In this method may be it should belongsto instead of hasmany
*/
public function respones()
{
    return $this->hasOne(Respone::class, 'ask_question_id', 'id');
}

public function assigned_to_user()
{
    return $this->belongsTo(User::class, 'assigned_to_user_id');
}

用户.php

public function askQuestionRespones()
{
    return $this->hasMany(Respone::class, 'user_id', 'id');
}

show.blade.php

               <div class="card-body">
                <table class="table table-bordered table-striped">
                    <tbody>
                        <tr>
                            <th>
                                {{ trans('Thématique') }}
                            </th>
                            <td>
                                {{ $respone->category->name }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Nom auteur') }}
                            </th>
                            <td>
                                {{ $respone->author_name }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Auteur email') }}
                            </th>
                            <td>
                                {{ $respone->author_email->email }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Question') }}
                            </th>
                            <td>
                                {{ $respone->ask_question->text_question }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Réponse') }}
                            </th>
                            <td>
                                {{ $respone->text_answer }}
                            </td>
                        </tr>
                    </tbody>
                </table>

管理部分的ResponesController.php

public function show(Respone $respone)
{
    abort_if(Gate::denies('respone_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    $respone->load('category', 'author_email', 'ask_question');

    return view('admin.respones.show', compact('respone'));
}

前面部分的ResponeController.php

public function show(Respone $respone, Category $category,
                     User $user, AskQuestion $ask_question)
{
    $respone->load('author_email', 'category', 'ask_question');

    $ask_question->load('Respones');

    return view('respones.show', compact('respone', 'ask_question'));
}

【问题讨论】:

  • 你能添加你的控制器吗?

标签: laravel eloquent foreign-keys


【解决方案1】:

其中一个关系返回 null

$respone->category
$respone->ask_question
$respone->author_email

因此,当您尝试访问空关系(在空值 null->attribute 上)的属性时,您会收到此错误。

您可以尝试在回显属性之前检查每个关系的值,使用@if@isset

@if($respone->category)

@endif

或用optional 助手包装关系

<th>
    {{ trans('Thématique') }}
</th>
<td>
    {{ optional($respone->category)->name }}
</td>

【讨论】:

  • 当我做 dd($respone)Respone #relations: array:3 [▼ "author_email" => null "category" => null "ask_question" => null ]
  • 我认为问题出在我的表关系上吗?
  • 您需要分享您的数据库结构和模型关系,但这是另一个主题,如果错误消失请标记为已解决并打开另一个线程。
  • 是的,错误消失了,但我的刀片页面上没有显示我要购买的数据
【解决方案2】:

您可以使用null coalescing operator ??(在 PHP 7 中引入)。它用于检查值是否设置或为空,或者换句话说,如果值存在且不为空,则返回第一个操作数,否则返回第二个操作数。”

<td>
  {{ $respone->category->name ?? 'Default' }} 
</td>

【讨论】:

  • 当我使用空合并运算符时?错误消失但不返回任何数据
  • 将其添加到您的帖子中
猜你喜欢
  • 2020-04-16
  • 2020-10-25
  • 2019-11-16
  • 2018-03-22
  • 2019-02-24
  • 2020-09-26
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多