【问题标题】:Laravel eloquent relationships errorLaravel 雄辩的关系错误
【发布时间】:2017-05-16 13:21:14
【问题描述】:

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

<?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);
    }
}

当我访问 cmets 页面时,我收到以下错误:

未定义的属性: Illuminate\Database\Eloquent\Relations\BelongsTo::$name.

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

【问题讨论】:

    标签: php laravel laravel-5 eloquent relationships


    【解决方案1】:

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

    替换

    $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;
    

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

    【讨论】:

      【解决方案2】:

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

      您正在使用关系并且仍然使用数组键查找用户,它更有可能在 $comment 上使用 bcz $comment 已经是模型,您可以轻松地应用关系。与回放模型相同。

      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;
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-14
        • 2018-11-28
        • 2015-01-11
        • 2021-06-03
        • 2020-03-26
        • 1970-01-01
        相关资源
        最近更新 更多