dimanche 12 novembre 2017

Laravel Many-To-Many relation returns no results

I am currently learning Laravel and I have an issue which I can't seem to find a solution for. I have a many-to-many relation between two tables that always returns nothing. Let me show you the basic setup:

My posts Model:

// App\Post.php
protected $fillable = [
    'name',
    'description'
    'videopath'
];

public function Tags()
{
    return $this->belongsToMany('App\Tag');
}
public function Cats()
{
    return $this->belongsToMany('App\Cat');
}

My tags model:

// App\Tag.php
protected $fillable = [
    'name',
    'description'
];

public function exercises()
{
    return $this->belongsToMany('App\Exercise');
}

My posts controller:

// PostController.php
public function show($id)
{
    $post = Post::find($id);
    return view('posts', ['post'=>$post];
}

The view:

// posts.blade.php
@foreach($post->tags() as $tag)
    //do stuff
@endforeach

The intermediate table is called post_tag and contains the post_id and tag_id columns. At first it returned the results as expected but after some while all of my posts didn't return any tags anymore. The cats model looks similar to the tags model. Anyone has an idea?



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

Aucun commentaire:

Enregistrer un commentaire