Search code examples
phplaravellaravel-4laravel-3

Cannot insert to my new table


I have created a migration for ratings, and the table also working when i am entering phpmyadmin.

The problem is, i cannot figure out, how to write to the table? I am running the code from "story" controller

I am using this:

     $z = new Rating();
     $z->story_id = 10;
     $z->save();
     print_r($z);

My "ratings.php" model:

<?php
 class Rating extends Eloquent {
    protected $table = 'ratings';
  }
 ?>

Is there some place where i should notify laravel that new Rating() means my table "ratings"?

It doesn't seem like i have done the migration correctly, but i am completely new still, so hope someone can figure it out for me.


Solution

  • well instead of using the save() function for laravel you can use the insert() function

     Rating->insert_get_id(array('story_id' => '10'));
    

    or

     $insert_id = Rating->insert_get_id(array('story_id' => '10'));
    

    for insertion into table.This is much easy to use and I have used this in my whole project and so far I haven't face any problems.

    Also if you have not created the model for rating table then go to the models folder under application folder and create a file name rating.php and inside the file write this:

    class Rating extends Eloquent
    {
        public static $timestamps = false;  
    }
    

    Also please note that table which you created in the phpmyadmin should have name of the form "ratings".

    I hope this can be of some help.