Reputation:
I'm working with Laravel 5.8 to develop my project and I have this table which shows some data from the DB:
@foreach(\App\Shop\ProductDelivery::all() as $delivery)
<tr>
<td>{{ $delivery->name }}</td>
<td>{{ $delivery->price }}</td>
<td>
<a href="{{ route('editFreeDelivery', $delivery->id) }}">Edit</a>
</td>
</tr>
@endforeach
As you can see there's a link named Edit
for editing these data, so when someone clicks on that, this method runs:
Route::get('product-information-pages/free-deliveries/{productDelivery}/edit', 'ShopInformationPagesAdminController@editFreeDelivery')->name('editFreeDelivery')->middleware('permission:static-page-manage');
public function editFreeDelivery(ProductDelivery $productDelivery)
{
return view('admin.shop.deliveries.edit', compact('productDelivery'));
}
I have also added this form for updating the data sent to the edit.blade.php
:
<form action="{{ route('updateProductDelivery', [$productDelivery->id]) }}" method="POST" enctype="multipart/form-data">
@csrf
{{ @method_field('PATCH') }}
<label for="title" class="control-label">Name</label>
<input type="text" id="title-shop" name="name" disabled="disabled" class="form-control" value="{{ old('name' , $productDelivery->name) }}" autofocus>
<label for="price" class="control-label">Price</label>
<input type="text" id="price_shop" name="price" class="form-control" value="{{ old('price' , $productDelivery->price) }}" autofocus>
<button class="btn btn-success" type="submit">Submit</button>
</form>
And here is the method for updating data:
Route::patch('product-information-pages/free-deliveries/{productDelivery}', 'ShopInformationPagesAdminController@updateProductDelivery')->name('updateProductDelivery')->middleware('permission:static-page-manage');
public function updateProductDelivery(Request $request, ProductDelivery $productDelivery)
{
try {
$data = $request->validate([
'name' => ['required'],
'price' => ['required','integer'],
]);
$productDelivery->update($data);
} catch (\Exception $e) {
dd($e);
}
return redirect(route('product-information-pages.create'));
}
But now the problem is, data does not be changed and updated somehow and shows this as dd($e)
:
So what is going wrong here? How can I fix this issue?
And finally here is the Model ProductDelivery.php
:
class ProductDelivery extends Model
{
protected $table = "product_deliveries";
}
And the table product_deliveries
looks like this:
UPDATE #1:
Result of dd($productDelivery->toArray());
goes like this:
array:5 [▼
"id" => 1
"name" => "Free Delivery"
"price" => 300000
"created_at" => "2021-07-04 14:16:09"
"updated_at" => "2021-07-04 14:16:09"
]
Upvotes: 0
Views: 159
Reputation: 1891
Your input is disabled and disabled fields are not submitted with the request.
<input type="text" id="title-shop" name="name" class="form-control" value="{{ old('name' , $productDelivery->name) }}" autofocus>
try using readonly="readonly"
instead of disabled, or omit the field from the request entirely if it should not be changed.
Upvotes: 1
Reputation: 43
You have a
$productDelivery->update($data);
But, how you say to your function what product update? You need first something like
$productDelivery = productDelivery::find($id)
$productDelivery->update($data);
Upvotes: 0