kim larsen
kim larsen

Reputation: 5451

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.

Upvotes: 0

Views: 100

Answers (2)

saran banerjee
saran banerjee

Reputation: 2903

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.

Upvotes: 4

xsearingheaven
xsearingheaven

Reputation: 311

I don't really understand what you're doing. Are you trying to write into the table from php? Is Rating a sort of database connection class? You need to create a mysqli object to connect to the database, write a query, and get a result. For best security use a prepared statement. Mysqli Documentation Sorry if I'm off-base about your question, I'm just not positive about what it is.

Upvotes: 0

Related Questions