I am new to laravel and eloquent and i have this problem "Trying to get property 'comments' of non-object" and i don't know what is the mistake here
model > Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function comments(){
return $this->hasMany('App\Comment','id');
}
protected $primaryKey = 'post_id';
//protected $fillable = ['title','body'];
}
model > Comment.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function posts(){
return $this->belongsTo('App\Post','post_id');
}
protected $primaryKey = 'id';
}
controller > CommentsController
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class CommentsController extends Controller
{
public function show(){
$comments = Post::find(1)->comments;
return view('comments', compact('comments'));
}
}
view > comments.blade.php
@extends('fixed')
@section('content')
<div class="container">
<div class="col-10">
<table class="table table-bordered">
@foreach($comments as $comment)
<tr>
<td></td>
<td> </td>
<td></td>
</tr>
@endforeach
</table>
</div>
</div>
@endsection
and i get that error Error
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire