Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel attach() method not working to hasMany side

Tags:

php

laravel-4

The application has the models:

Atividade.php

class Atividade extends Eloquent {
    public function intervencoes() {
        return $this->belongsToMany('Intervencao');
    }
}


Intervencao.php

class Intervencao extends Eloquent {
    public function atividades() {
        return $this->hasMany('Atividade');
    }
}


The following code works:

Atividade::find($id)->intervencoes()->attach($intervencao_id);

But, this...

Intervencao::find($id)->atividades()->attach($atividade_id);

Returns an BadMethodCallException:

Call to undefined method Illuminate\Database\Query\Builder::attach()


SOLUTION (thanks to @gnack):

I was trying to set a many-to-many relationship, so just needed to change this...

return $this->hasMany('Atividade');

To this:

return $this->belongsToMany('Atividade');
like image 728
Bledson Avatar asked Feb 05 '14 01:02

Bledson


People also ask

How do you attach a many to many relationship?

The key in many to many relationship is the join (or pivot) table. The pivot table allows the relationship id from one model to be related to many other models and vice-versa. Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany.

What is hasMany relationship?

A hasMany relation builds a one-to-many connection with another model. You'll often find this relation on the “other side” of a belongsTo relation. This relation indicates that each instance of the model has zero or more instances of another model.

What is the use hasMany in laravel?

hasMany relationship in laravel is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship .


2 Answers

See the Laravel documentation here: http://laravel.com/docs/eloquent#inserting-related-models

Basically you have set up two different types of relationships for the same two tables - you've set up a many-to-many and a one-to-many. It looks as though you probably wanted a many-to-many, so you'll need to change this line:

return $this->hasMany('Atividade');

To this:

return $this->belongsToMany('Atividade');

This will set the relationship up as a many-to-many relationship, which will then support the attach() method.

The attach() method is only for many-to-many, for other relationships there's save() or saveMany() and associate() (see the docs linked above).

like image 50
Nick Coad Avatar answered Sep 21 '22 06:09

Nick Coad


See the documentation Laravel 5.7

A Comment belongTo an unique Post

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

A Post can Have multiple Comments

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

When you want to update/delete a belongsTo relationship, you may use the associate/dissociate method.

$post= App\Post::find(10);
$comment= App\Comment::find(3);
$comment->post()->associate($post); //update the model

$comment->save(); //you have to call save() method

//delete operation
$comment->post()->dissociate(); 

$comment->save(); //save() method
like image 25
UserHelpNeeding02356 Avatar answered Sep 24 '22 06:09

UserHelpNeeding02356