简体   繁体   中英

Laravel : fill() and update() not work when i use $request->all() but work in fill($request->only('…'))

I have some column in JSON format. for example 'Bank' column in database be like this :

{"name":"Bank of America", "card":"6037...", "account":"321354...", "expire" : "2018-12-09"}

i used Laravel Cast to in Model like this :

protected $casts = [
    'bank' => 'array',
]

In view my form fileds named by these :

<input name="bank[name]" ...>
<input name="bank[account]" ...>
<input name="bank[card]" ...>

Now when i want to update Bank in database. when i use this

$profile->fill($request->only('bank'));
$profile->save();

every things work OK. but when i use this

$profile->fill($request->all());
$profile->save();

i got this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bank[card]' in 'field list' (SQL: update...

What's wrong ?!

Solution with thanks to @Jonas ::

it was the problem : in Requests->myFormRequest i cleaned my data whit this :

 if( $this->bank['card'] ) { $input['bank[card]'] = preg_replace("/[^0-9]/", "", $this->bank['card']); } 

and it was wrong for arrays. i changed it to this and every thing worked perfectly.

 if( $this->bank['card'] ) { $input['bank']['card'] = preg_replace("/[^0-9]/", "", $this->bank['card']); } 

I am not sure about this but in the error message I see bank[cart]. In your html there is bank[card], maybe its just a typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM