belongsToMany Laravel - find()

Asked

Viewed 2,215 times

2

I’m developing an Intranet that will contemplate Categories and Posts. A post can receive "n" categories.

To that effect I’m using belongsToMany().

The problem is when returning this data with Post::find($idpost), it returns not only 1 record but all post records in that table pivot is related.

I haven’t found anything like it and I don’t know if I’m doing anything wrong.

The create is running perfectly, the problem is when I ask to return only 1 record.

I inserted belongsToMany() in the 2 tables, Posts and Categories.

Could someone tell me if this behavior is normal or if there is something wrong in 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');
    }
}
  • I think I should return a record, only the post with id X

  • That’s right, Miguel. I have 3 Posts records, id 35, 36 and 39. Anyone who tests, he returns me all Posts. Not only the ID I want.

  • Check on the model Post if you are acting at the right table, protected $table = ..

  • If you pass one array for find, it will bring more than one value. See if $idPost is a array or int.

  • I used convention. Do I need to declare anyway? Table names are: posts and Categories

  • the $idPost is integer... even tested by typing the numeric value, without variable.

  • try to put the name of the pivot table as category_post in the singular...

  • @Marcogarcia enter the code of the 2 models, please.

  • Ready @Rafaelacioly

Show 4 more comments

3 answers

1

You can use the hasMany instead of belongsToMany. Seria belongsToMany if you did the SELECT through the table Categorypost.

Model

Post.php

public function categorias(){
    return $this->hasMany('App\CategoryPost', 'post_id');
}

Controller

MainController.php

use App\Post;    
public function index(){

    $posts = Post::find(1);    
    dd($posts->categorias);
}
  • Diego, would this Categorypost be the pivot table? But in the case there is no Model for it. That’s right?

  • You have to create the Model for this table.

  • Diego, I don’t quite understand your solution. I could be more specific?

  • Create a Model named Categorypost that refers to your Pivot table.

1

Have you tried using the relationship hasManyThrough? It serves exactly for relationship ManyToMany, which is what seems to be your case.

There is a complete example in the Laravel documentation below:

https://laravel.com/docs/5.2/eloquent-relationships#has-Many-through

Using hasManyThrough, you can define the relationship in both Models and access the multiple related records of each.

I hope I’ve helped! ;)

0

The convention for pivots is the name of each table (singular) separated by _ alphabetically.

category_post

  • I got it, Rafael. But in the case my Migration/table is configured with a pivot table. Would it be a problem in this sense, that is, to declare only hasMany() in the Post, rather than belongsToMany()? This would not misread the pivot table concept?

  • In my case, a post may belong more than 1 category. You are judging that a post can only have 1 category, the post_id you mention does not have in tb Categories.

  • what are the field names of the 2 tables you are trying to use the pivot? in the post table and category which is the relationship field?

  • I edited the question, I inserted the basic structure

  • I entered it wrong in EDIT. It’s singular.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.