mercredi 26 octobre 2016

Laravel Eloquent relationship error

I'm using Laravel 5 for my blog. But when I use relation "hasMany", I got following error "FatalErrorException in 45abc28f0139bedaa1467307304d448ffaaed95e.php line 5: syntax error, unexpected '->' (T_OBJECT_OPERATOR) "

Here is my 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'));



 }
 }

 ?>

Here is my Post Model

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

And here is my User Model

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

Please, how can I solve this problem ?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire