Error inserting data with Mysql and Laravel

Asked

Viewed 57 times

0

I have a table called "item" in which I need to enter data, but for some reason does not process the Insert. I’m new to backend so I don’t know if there is any restriction on the table that prevents you from entering the record.

mysql> desc item;
+-------------------+---------------------------------------------------------------------------+------+-----+---------+----------------+
| Field             | Type                                                                      | Null | Key | Default | Extra          |
+-------------------+---------------------------------------------------------------------------+------+-----+---------+----------------+
| id                | int(10) unsigned                                                          | NO   | PRI | NULL    | auto_increment |
| item_id           | int(10) unsigned                                                          | NO   | MUL | NULL    |                |
| proc_id           | int(10) unsigned                                                          | YES  | MUL | NULL    |                |
| date              | datetime                                                                  | NO   |     | NULL    |                |
| code              | varchar(100)                                                              | YES  |     | NULL    |                |
| status            | enum('Waiting','Canceled')                                            | YES  |     | NULL    |                |
| user              | int(10) unsigned                                                          | YES  | MUL | NULL    |                |
| created_at        | timestamp                                                                 | YES  |     | NULL    |                |
| updated_at        | timestamp                                                                 | YES  |     | NULL    |                |
+-------------------+---------------------------------------------------------------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

mysql>

The code that performs the Insert is this, $item_id would be a string as '222' for so long I passed her to whole:

if ($item_id != '') {

                DB::table('item')->insert([
                    'item_id' => settype($item_id, 'integer'),
                    'proc_id' => null,
                    'date' => Carbon::now(),
                    'code' => null,
                    'status' => 'Waiting',
                    'user' => null
                ]);
    }
  • Opa Mario, some mistake happens?

  • 1

    @Andrélins then, I have no log available on the console. I’m seeing how I can enable it yet. The interesting thing is that this only occurs when use in Schedule with cronjob, but when use in a route, when calling it is performed the Insert. Intriguing!?

  • Laravel creates a file laravel.log in storage/logs/, check if there’s anything there.

  • in the log informs that it references a column id of another table, the strange thing is that this data exists. I’ll take a closer look here to see if there’s any more inconsistency

  • @Andrélins actually the error was occurring due to the item_id that did not exist in the other table, because I thought I was changing the data type to pass as integer, but actually the settype returns 1 for TRUE and 0 for FALSE. After fixing it worked normally. Thanks for the force, you know if you can close my question or how I can close?

  • Great Mario, answers the question and puts these answers as the correct one that I believe is enough.

Show 1 more comment

1 answer

0

As the help of @André_lins in the comments the error was occurring due to the item_id that did not exist in the other table, because I thought I was changing the data type to pass as integer, but actually the settype returns 1 for TRUE and 0 for FALSE. After fixing it worked normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.