Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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</p> <p>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.</p> <p>Thirdly, your design of the Post_like class is a bit off, you could be better off make it like so:</p> <pre><code>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-&gt;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-&gt;belongs_to('User'); } } </code></pre> <p>Fourthly, your other classes could be better as:</p> <pre><code>class Post extends Eloquent { public function post_likes() { return $this-&gt;has_many('PostLike'); // doesn't require you to specify an id at all } } </code></pre> <p>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: <a href="http://four.laravel.com/docs/eloquent#mass-assignment" rel="nofollow">http://four.laravel.com/docs/eloquent#mass-assignment</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COThank you for your awesome answer. It was helpful. But still I am into the problem. like I have changed things according to you. `Call to undefined method Illuminate\Database\Query\Builder::has_many()` this error is displayed there.. I have used `$user1->post_likes()->save($debate);` to save it to the database. In the last code you mensioned `return $this->has_many('PostLike');` I have used `return $this->has_many('Post_Like');` PS : yes I have changed the words while posting here. I have used debate instead post in my database.
      singulars
    2. COYes solved this error `Call to undefined method Illuminate\Database\Query\Builder::has_many()` as I am using LARAVEL 4 it should be `hasMany()`. But I am having this error : `Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, string given, called in E:\xampp\htdocs\ffdd\app\controllers\DebatelikesController.php on line 72 and defined` and in line 72 the code goes like this `$user1->debate_likes()->save($debate);`
      singulars
    3. COcongrats, was going to say it's possibly that you were putting the table name instead of the class. But glad I could help, also the reason for your possible MassAssignmentException is if you use something like Model::create(array of row values) then it'll through this, instead you should use: $model = new Model(), $model->save() which will do the insert for you singularly
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload