The issue you're experiencing is likely due to the way you're merging the new file path into the request data. When you use $request->merge(), it modifies the request object, but it doesn't affect the original data that is being passed to the update() method. This can lead to the temporary file path being saved instead of the new path.
To resolve this, you should directly update the $order model with the new path before calling the update() method. Here's how you can modify your code:
public function update(StoreOrderRequest $request, Order $order)
{
if ($request->user()->id === $order->user_id) {
if ($request->hasFile('logo')) {
if ($order->logo) {
Storage::disk('public')->delete($order->logo);
}
$newLogoPath = $request->file('logo')->storeAs(
'uploads',
$request->user()->id . '_' . Str::of($request->user()->name)->snake() . '.' . $request->file('logo')->extension(),
'public'
);
// Directly update the order's logo path
$order->logo = $newLogoPath;
}
// Update the order with the request data
$order->update($request->except('_method', '_token', 'logo'));
} else {
if ($request->hasFile('logo')) {
if ($order->logo) {
Storage::disk('public')->delete($order->logo);
}
$newLogoPath = $request->file('logo')->storeAs(
'uploads',
$request->user()->id . '_' . Str::of($request->user()->name)->snake() . '.' . $request->file('logo')->extension(),
'public'
);
// Directly update the order's logo path
$order->logo = $newLogoPath;
}
// Update the order with the request data
$order->update($request->except('_method', '_token', 'logo'));
}
return redirect('/dashboard')->with('status', 'Order updated');
}
Key Changes:
-
Directly Assign the New Path: Instead of merging the new path into the request, directly assign it to the
$ordermodel'slogoattribute. -
Exclude 'logo' from
update(): When callingupdate(), exclude the 'logo' field from the request data since it's already been set directly on the model.
This approach ensures that the correct file path is saved to the database.