I am trying to get Laravel to update a database record, if it's already exists. This is my table:
id | booking_reference | description | date
------------------------------------------------------
PRI KEY | UNIQUE | MEDIUM TEXT | DATE
AUTO INC | |
My model looks like this:
Document.php
:
class Document extends Model
{
protected $fillable = [
'booking_reference', 'description', 'date'
];
}
And my controller, looks like this - please note that it's webhook()
that's being called.
DocumentController.php
:
class DocparserController extends Controller
{
//This is the function to capture the webhook
public function webhook(Request $request)
{
$document = new Document();
$document->fill($request->all());
//Grab the date_formatted field from our request.
$document->date = $request->input('date_formatted');
$document->updateOrCreate(
['booking_reference' => $document->booking_reference],
//How can I do so it updates all fields?
);
return response()->json("OK");
}
}
So my problem is, that I cannot figure out how to update my entire row, where the booking_reference
is already present.
I want to update all fields (description, date), without having to enter them all like:
['booking_reference' => $document->booking_reference],
['description' => $document->comments, 'date' => $document->date]
$request->all()
directly or make a method which maps/translates values from the input to a given array (having the appropriate keys:'booking_reference', 'description', 'date'
) since you are sendingdate_formatted
instead ofdate
? – ka_lin$request->formatted_date
is set to$document->date
– oliverbj