【问题标题】:Laravel 8: Trying to get property 'name' of non-object errorLaravel 8:试图获取非对象错误的属性“名称”
【发布时间】:2021-06-05 18:35:37
【问题描述】:

我正在使用 Laravel 8 开发我的论坛项目。所以我创建了一个名为answers 的表,这里是它的迁移:

public function up()
    {
        Schema::create('answers', function (Blueprint $table) {
            $table->id();
            $table->text('answer');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('question_id')->constrained()->onDelete('cascade');
            $table->timestamps();
        });
    }

然后为了在Question Model 和Answer Model 之间建立关系,我添加了这些:

Question.php:

public function answers()
    {
        return $this->hasMany(Answer::class);
    }

用户.php:

public function answers()
    {
        return $this->hasMany(Answer::class);
    }

在那之后,我添加了这个来显示问题信息:

@foreach($show->answers as $ans)
<table class="table table-striped table-bordered table-responsive-lg">
    <thead class="thead-light">
        <tr>
            <th scope="col"></th>
            <th scope="col">Answer</th>
        </tr>
    </thead>
    <tbody>
        <tr></tr>
        <tr>
            <td style="text-align:right">
                <div><span>Writer: </span><a href="">{{ $ans->id->name }}</a></div>
            </td>
            <td style="text-align:right">
                <p>
                    {{ $ans->answer }}
                </p>
            </td>
        </tr>
    </tbody>
</table>
 @endforeach

但现在我得到了这个错误:

ErrorException Trying to get property 'name' of non-object

那么这里出了什么问题?如何正确返回提出问题的用户的姓名?

如果您对此有任何想法或建议,我将不胜感激,

谢谢。

更新 #1:

更新 #2:

class Answer extends Model
{
    protected $fillable = ['answer', 'user_id', 'question_id'];

}

【问题讨论】:

  • @u_mulder 我试过了,得到了 Null 结果
  • 答案和用户模型之间的关系名称是什么?是“用户”吗?
  • @OMR 在User.php 我命名为answers()。而Answer.php 仅包含 fillable 变量。
  • $ans-&gt;id-&gt;name?你不是说$ans-&gt;user-&gt;name之类的吗?
  • 请分享整个答案模型

标签: php laravel laravel-8


【解决方案1】:

您应该在 Answer 模型中建立 Answer an User 模型之间的关系:

class Answer extends Model
{
    protected $fillable = ['answer', 'user_id', 'question_id'];

   public function user()
    {
        return $this->belongsTo(User::class);
    }

  public function question()
    {
        return $this->belongsTo(Question::class);
    }
}

并且在您的刀片中,假设 $ans 是 Answer 模型的一个实例,您可以使用:

{{ $ans->user->name }}

【讨论】:

  • 谢谢你,你不知道你帮了我多少:)
【解决方案2】:
$table->foreign('user_id')->references('id')->on('users')->constrained()->onDelete('cascade');

$table->foreign('question_id')->references('id')->on('questions')->constrained()->onDelete('cascade');

【讨论】:

  • 我回滚了迁移并使用了它,但仍然出现错误! Trying to get property 'name' of non-object
  • public function up() { Schema::create('answers', function (Blueprint $table) { $table->id(); $table->text('answer'); $ table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users')->constrained()->onDelete('cascade'); $table->unsignedBigInteger('question_id'); $table->foreign('question_id')->references('id')->on('questions')->constrained()->onDelete('cascade') ; $table->timestamps(); }); }
  • 你可以擦除 $table->unsignedBigInteger('user_id');和 $table->unsignedBigInteger('question_id');
猜你喜欢
  • 2021-06-04
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2021-07-18
  • 2016-06-13
  • 2018-06-02
  • 2017-08-20
相关资源
最近更新 更多