Sagiruddin Mondal Sagiruddin Mondal - 1 month ago 9
MySQL Question

Like or favorite a post with unique user using Laravel Eloquent

I was creating a like system for my website. in this I wanted one user can only like one time for a post. and a post can be liked by many user. Also many user can like many post.

So if I guess it right, It is a many to many reletionship.

in this context,
I create the following table

... users table:

id

name


....
posts table :

id

post


...post_likes table

id

user id
poost_id


Now I am having the following model for

user :

class User extends SentryUserModel {
public function post_likes()
{
return $this->has_many('Post_like', 'id');
}
}


post :

class Post extends Eloquent {

public function post_likes()
{
return $this->has_many('Post_like', 'id');
}
}


post_like :

class Post_like extends Eloquent {

public function posts()
{
return $this->belongs_to('Post', 'post_id');
}
public function users()
{
return $this->belongs_to('User', 'user_id');
}

}


now when I am going to insert into the database (for post_likes table) I am getting an error called

Illuminate \ Database \ Eloquent \ MassAssignmentException
user_id


Also I want to know is there any way to inset into database like

$user->like()->save($user); ?????


Thank you in advance. Happy coding . \m/

Answer

I'll start with a basic issue, firstly you might want to make sure all your tables are lower case (still as a snake case as well), it's not required but it's ultimately how it's expected to be with Laravel so it makes life easier to keep with that. Also a note to the wise, like Class names, database tables are typically in the singular so user instead of users

Secondly yes you can do an insert with $user->post_likes()->save($debate); as your post_likes method on the user class returns has_many.

Thirdly, your design of the Post_like class is a bit off, you could be better off make it like so:

class PostLike extends Eloquent { // note that PostLikes is a more standard naming for a class, they should ideally be camel case names but with all capitals for words

    protected $table = 'post_like'; // specifies the table the model uses

    public function post() // this should be singular, the naming of a belngs_to method is important as Laravel will do some of the work for you if let it
    {
        return $this->belongs_to('Post'); // by naming the method 'post' you no longer need to specify the id, Laravel will automatically know from the method name and just adding '_id' to it.
    }
    public function users()
    {
        return $this->belongs_to('User');
    }

}

Fourthly, your other classes could be better as:

class Post extends Eloquent {

    public function post_likes()
    {
        return $this->has_many('PostLike'); // doesn't require you to specify an id at all
    }
}

I can't exactly tell you why you're getting that mass assign error, your post is a bit garbled and doesn't look like you've included the code that actually causes the exception? I have a feeling though is that you're trying to do an insert for multiple database rows at one time but haven't defined a fillable array for PostLike such as with here: http://four.laravel.com/docs/eloquent#mass-assignment