I'm developing an Intranet that will span Categories and Posts . A post can receive "n" categories.
For this purpose I am using belongsToMany () .
The problem is when returning this data with Post :: find ($ idpost) , it returns not only 1 record but all post records where the pivot table > is related.
I did not find anything like this and I do not know if I'm doing something wrong.
The create is running perfectly, the problem is when I ask to return only 1 record.I have belongsToMany () in the 2 tables, Posts and Categories .
Can anyone tell me if this behavior is normal or if there is something wrong with the logic / construction?
PS: Laravel 5.2
EDIT
Post
table: posts
pk: id
Category
table: categories
pk: id
Pivot
table: category_post
pk: id
fk: post_id
fk: category_id
namespace App\Intranet;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $connection = 'mysql_intranet';
protected $fillable = ['title', 'text', 'categories'];
public function categories()
{
return $this->belongsToMany('App\Intranet\Category');
}
}
namespace App\Intranet;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $connection = 'mysql_intranet';
protected $fillable = ['categoria', 'herdado'];
public function posts()
{
return $this->belongsToMany('App\Intranet\Post');
}
}