我在我的博客上使用Laravel 5。但是当我使用关系" hasMany"时,我得到了以下错误" FatalErrorException 45abc28f0139bedaa1467307304d448ffaaed95e.php第5行:语法错误,意外' - >' (T_OBJECT_OPERATOR)"
这是我的PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View;
use App\Http\Requests;
use App\Post;
class PostController extends Controller
{
public function index(){
$listePosts = Post::all();
return view('post.index', compact('listePosts'));
}
public function show($detail){
$post = Post::where('detail', $detail)->firstOrFail();
$author = $post->user;
$comment = $post->comments;
return view('post.show', compact('post', 'author', 'comment'));
}
}
?>
这是我的帖子模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $guarded = ['id', 'created_at'];
public function user(){
return $this->belongsTo('App\User');
}
public function comments(){
return $this->hasMany('App\Comment');
}
}
这是我的用户模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'created_at'];
protected $fillable = [
'user_full_name', 'user_pseudo', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts(){
return $this->hasMany('App\Post');
}
public function comments(){
return $this->hasMany('App\Comment');
}
}
请问,我该如何解决这个问题?
由于
答案 0 :(得分:0)
这是我的show.blade.php源代码
@extends('template')
@section('content')
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title" align="center">{{post->post_name}}</h2>
<h3> Créé par : {{author->user_pseudo}} |
@if (post->count_comments == 0)
Pas de commentaire
@elseif (post->count_comments == 1)
1 Commentaire
@else
{{ post->count_comments}} Commentaires
@endif
</h3>
</div>
<div class="panel-body">
<p>{{post->content}}</p>
<div>
<h2 class="label label-info">Les commentaires</h2>
@foreach($comment as $oneComment)
<h4>Posté par : {{ $oneComment->user->user_pseudo}}</h4>
<p>{{ $oneComment->content}}</p>
@endforeach
</div>
</div>
</div>
@stop