Laravel eloquent 关系错误

Laravel eloquent relationships error

基本上,我试图在用户、post 和回复数据库之间建立关系。以下是模型: 评论模型

<?php

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Model;

class comments extends Model
{
    public $timestamps = true;
    protected $table = 'comments';
    protected $guarded = ['id'];

    public function userInfo()
    {
        return $this->belongsTo('App\Eloquent\User', 'user_id');
    }
    public function reply()
    {
        return $this->hasOne('App\Eloquent\reply', 'post_id');
    }
}

回复模式:

<?php

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Model;

class reply extends Model
{
    public $timestamps = true;
    protected $table = 'replies';
    protected $guarded = ['id'];


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

主要代码:

<?php

namespace App\Http\Controllers;

use App\Eloquent\comments;
use App\Eloquent\reply;
use Illuminate\Http\Request;

class CommentsController extends Controller
{
    public function index()
    {
        $commentsData = [];
        $replyData = [];
            $comments = comments::all();
        foreach ($comments as $comment)
        {
            if($comments !== null) {
                $user = comments::find($comment->user_id)->userInfo();
                $reply = comments::find($comment->id)->reply();
            }
            if(reply::all() !== null) {
                $user_reply = reply::find($comment->id)->user();
            }
            $commentsData[$comment->id]['name'] = $user->name;
            $commentsData[$comment->id]['message'] = $comment->body;
            $commentsData[$comment->id]['rating'] = $comment->rating;
            $commentsData[$comment->id]['timestamp'] = $comment->created_at;
            foreach($reply as $re)
            {
                $replyData[$re->post_id][$re->id]['name'] = $user_reply->name;
                $replyData[$re->post_id][$re->id]['body'] = $reply->body;
            }

        }

        return view('comments')->with('comments', $commentsData)->with('reply', $replyData);
    }
}

当我访问评论页面时,出现以下错误:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name.

这是我第一次使用关系,所以我检查了 Laravel 文档,但仍然不知道我做错了什么。基本上我想要得到的是从用户数据库中获取用户名(使用评论 user_id 作为外国),获取评论详细信息(正文,评级)并获取回复数据,使用 post_id (在回复 table) 作为外键并评论 table 主键 ID 作为本地键。

您正在从模型而不是相关对象中获取关系定义。

替换

$user = comments::find($comment->user_id)->userInfo();
$reply = comments::find($comment->id)->reply();
$user_reply = reply::find($comment->id)->user();

$user = comments::find($comment->user_id)->userInfo;
$reply = comments::find($comment->id)->reply;
$user_reply = reply::find($comment->id)->user;

请注意这些行末尾删除的括号。

我已经对您的 foreach 循环中的一些关系进行了更改,这将使它更快、更可用

You are using relationship and still finding user using array key it's more likely to use on $comment bcz $comment is already Model and you can apply relationship on that easily. as same as for replay Model.

foreach ($comments as $comment)
    {
        $user = $comment->userInfo();
        $reply = $comment->reply();

        $commentsData[$comment->id]['name'] = $user->name;
        $commentsData[$comment->id]['message'] = $comment->body;
        $commentsData[$comment->id]['rating'] = $comment->rating;
        $commentsData[$comment->id]['timestamp'] = $comment->created_at;
          foreach($reply as $re)
            {
                $replyData[$re->post_id][$re->id]['name'] = $re->user()->name;
                $replyData[$re->post_id][$re->id]['body'] = $re->body;
            }
        }