Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0
Solution

I assume you used

$request = Input::all();

Try this instead

$request = Input::except('_token');

Btw, it is interesting that the token is ignored when using the 'create' method, but not when using the 'insertGetId' method...

Last updated 9 years ago.
0

Firstly thanks for the response.

I had another look at this, and basically I am mixing up Eloquent and Query builder. Looking at the documentation if using Eloquent I would use:

$id = Product::create($request->all())->id; //this inserts the values and returns the id

which ignores the token and adds the timestamps etc.

The insertGetId uses query builder, hence I need to assign the values I want inserted.

Thanks ps. the reason I'm using:

   $request->all();

instead of

   Input::all(); 

as thats just what the official documentation shows for laravel 5.0

0

Yes, I provided code for Laravel 4, because this is still the one mostly used and for production. Anyhow, the 'except' method exists also in Laravel 5.

Next assumption, you are inject the 'Request' class in your controller. The syntax should then be

public function xy(Request $request)
    {
        $all_data_except_token = $request->except('_token');
        $id = Product::insertGetId($all_data_except_token);
        // do or return something
    } 

The 'Request' is always the same. You can use all fake static methods described in the doc and access the object with them ;) http://laravel.com/docs/5.0/requests

Last updated 9 years ago.
0

I didn't know about except this will be handy in the future, and yes I was using the request controller.

Thanks

0

Sign in to participate in this thread!

Eventy

Your banner here too?

drc83 drc83 Joined 16 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.