lundi 29 juin 2020

Cant get Property from the model realtion Laravel

I got an issue when i use fooreach loop with 3 model realtion in 1 view

This is my Thread model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    protected $primaryKey = "id_threads";

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

    protected $fillable = [
        'id_users', 'threads',
    ];

    public function comment(){
        return $this->hasMany('App\Comment','id_threads');
    }
        
}

This is my Comment model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $primaryKey = 'id_comments';

    public function thread(){
        return $this->belongsTo('App\Thread','id_threads');
    }

    public function users(){
        return $this->belongsTo('App\User');
    }

    protected $fillable = [
        'id_threads', 'id_users', 'comments','status'
    ];
}

This is my index controller

    public function index()
    {
        $user = Auth::user();
        $comment = Comment::all();
        
        //Threads
        $threads = Thread::orderBy('created_at','desc')
        ->get();
        $hitung = $threads->where('id_users', '=',$user->id);
        
        
        return view('/home',compact('threads','hitung','comment'));
    }

And this is piece of my view

@foreach ($threads as $thread)
    <div class="media-body">
        <h5 class="mb-0"></h5>
            <div class="card-date mb-2">
                 at 
            </div>
        <input type="hidden" name="$id_threads" value="">
            
            <div class="card-delete mt-5">
                @if (Auth::user()->id==$thread->id_users)
                <a href="">Delete</a>
                @else
                
                @endif
            </div>
            <hr>
            <div class="media comment mt-3">
                <a class="mr-3" href="#">
                    

The Error comes when i want to show comment

And an error like this appears

Property [comments] does not exist on this collection instance. (View: D:\Jendro\Project Laravel\projectCO\resources\views\home.blade.php)

But when i just call the object only, like this

Thats no error appears, in my view a data set appears from the Comment model

[{"id_comments":6,"id_threads":54,"id_users":2,"comments":"asdqweasd","created_at":"2020-06-29 08:58:53","updated_at":"2020-06-29 08:58:53","status":"comment"}] 

It was same when i use to call user object, but threse no problem with user object when i call property of user's object

I was stuck in this issue, does anyone have solution for this issue ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NGumHg
via IFTTT

Aucun commentaire:

Enregistrer un commentaire