Reputation: 111
I tried to update an annonce but it gives me
SQLSTATE error [42S22]: Column not found: 1054 Unknown '0' field in field list (SQL: update annonces set image = profiles / cbIhz3sDKQKQLuK51OjTVadgpI5BxZpX5cUhMHKB.jpeg, images =?, 0 = cat for sale63,1 = Lorem 63 ipsum dolor sit amet, consectetur adipisicing elit. Ipsum temporibus iusto ipsa, asperiores voluptas unde aspernatur praesentium in? Aliquam, dolore !, 2 = 206231 , 3 = profiles / cbIhz3sDKQKQLuK51OjTVadgpI5BxZpX5cUhMHKB.jpeg,4 =?, Annonces.updated_at = 2020-11-11 09:07:58 where id = 553).
Knowing that announces
table does not contain field 0, but it contains other fields that I do not want to update
dd ($ request-> all ()) displays well also annn's data displays in edit.blade well.
AnnoncesController.php
public function update(Request $request, $id)
{
$request->validate([
'titre' => ['bail','required', 'string','min:3'],
'image' => ['bail','required','max:2048'],
'images.*' => ['bail','required','max:2048'],
]);
$annonce=Annonce::find($id);
if($request->hasFile('image'))
{
$path = $request->image->store('profiles');
$imagee = $request->image->store('storage');
$request->image = $path;
}
$annonce->update([
$annonce->titre = $request->titre,
$annonce->description = $request->description,
$annonce->prix = $request->prix,
$annonce->image = $request->image,
$annonce->images = $request->images
]);
session()->flash('success', 'annonce updated successfully !!');
return redirect('annonces');
}
Upvotes: 0
Views: 974
Reputation: 40690
$annonce->titre = $request->titre
is not the correct syntax to create associative arrays to use in an update
.
You need the following:
$annonce->update([
'titre' => $request->titre,
'description' => $request->description,
'prix' => $request->prix,
'image' => $request->image,
'images' => $request->images
]);
It might be shorter to write:
$annonce->update($request->only([ 'titre', 'description', 'prix', 'image', 'images' ]));
If you're appropriately using the $fillable
property in your model to specify which properties can be mass assigned you can also use:
$annonce->update($request->all());
however there is a concern here that someone might fill a property that is fillable in general but you don't want filled in the specific form the user has completed and therefore are not validating. If you are validating against all fillable fields then this is safe.
Upvotes: 1