Laravel eloquent relationships error

1.7k views Asked by At

Basically, I'm trying to create a relation between user, post and reply db's. Here are the models: comment model

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

reply mode:

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

main code:

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

When I'm accesing the comments page, I'm getting the following error:

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

It's my first time using relationships, so I checked the Laravel docs, but still have no idea what I did wrong. Basically what I'm trying to get is to get user's name from users database (using comments user_id as foreign), get comment details (body, rating) and get the reply data, using post_id (in reply table) as foreign and comments table primary key ID as local key.

2

There are 2 answers

0
jedrzej.kurylo On BEST ANSWER

You're getting relation definition from your models instead of the related objects.

Replace

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

with

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

Note the removed brackets at the very end of those lines.

0
Binit Ghetiya On

I have made changes regarding some relations in your foreach loop which will make it faster and more usable

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