Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉维尔雄辩关系错误_Php_Laravel_Laravel 5_Eloquent_Relationships - Fatal编程技术网

Php 拉维尔雄辩关系错误

Php 拉维尔雄辩关系错误,php,laravel,laravel-5,eloquent,relationships,Php,Laravel,Laravel 5,Eloquent,Relationships,基本上,我试图在用户、post和回复数据库之间建立一种关系。以下是模型: 注释模型 <?php namespace App\Eloquent; use Illuminate\Database\Eloquent\Model; class comments extends Model { public $timestamps = true; protected $table = 'comments'; protected $guarded = ['id'];

基本上,我试图在用户、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');
    }
}

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

替换

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


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

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

您正在使用关系,但仍在使用数组键查找用户,它更可能在$commentbcz$comment上使用,它已经是模型,您可以轻松地在该模型上应用关系。与重播模型相同

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