Hello I was trying to use the Edit function from Laravel CRUD with resource. The problem that I got is that the form is not filling any of the old data that I already created using the value="{{ old('kodecust', $customer->kodecust ) }}"````` or `````value="{{ $customer->kodecust ) }}"
. The other function like Read,Delete, and Create are working except the Update/Edit.
Here is my Route:
Route::resource('/customer/mastercustomer', CustomerController::class)->except(['destroy'])->middleware("auth");
Route::delete('/customer/mastercustomer/{customer:perusahaancust}', [CustomerController::class, 'destroy'])->middleware('auth');
My Controller:
public function edit(Customer $customer)
{
return view('Master\Customer\editcustomer', [
"customer" => $customer,
"title" => "Edit Customer"
]);
}
public function update(Request $request, Customer $customer)
{
$validatedData = $request->validate([
'kodecust' => 'required|string|max:15',
'perusahaancust' => 'required|string|max:255',
'kontakcust' => 'required|string|max:100',
'kotacust' => 'required|string|max:255',
'alamatcust' => 'required|string|max:255',
'alamat2cust' => 'nullable|string|max:255',
'notelponcust' => 'required|string|max:30',
'termcust' => 'required|integer',
'limitcust' => 'required',
'desccust' => 'nullable|string|max:50',
]);
Customer::where('perusahaancust', $customer->perusahaancust)
->update($validatedData);
return redirect('/customer/mastercustomer')->with('success', 'Berhasil Edit Customer!');
}
And My View:
<tr>
<td><label for="kodecust">Kode</label></td>
<td>
<input type="text" name="kodecust" id="kodecust" value="{{ old('kodecust', $customer->kodecust ) }}" required style="width: 50px"
@error('kodecust') class="is-invalid" @enderror>
@error('kodecust')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</td>
<td><label for="perusahaancust">Perusahaan</label></td>
<td>
<input type="text" name="perusahaancust" id="perusahaancust" value="{{ old('perusahaancust', $customer->perusahaancust) }}" required
style="width: 200px" @error('perusahaancust') class="is-invalid" @enderror>
@error('perusahaancust')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</td>
</tr>
<tr>
<td><label for="kontakcust">Kontak</label></td>
<td>
<input type="text" name="kontakcust" id="kontakcust" value="{{ old('kontakcust', $customer->kontakcust) }}" required style="width: 100px"
@error('kontakcust') class="is-invalid" @enderror>
@error('kontakcust')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</td>
<td><label for="kotacust">Kota</label></td>
<td>
<input type="text" name="kotacust" id="kotacust" value="{{ old('kotacust', $customer->kotacust) }}" required style="width: 150px"
@error('kotacust') class="is-invalid" @enderror>
@error('kotacust')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</td>
</tr>
Let me know if you guys wanted to see more of my code. I will appreciate any help, thankyou!
Using dd($customer);
result ini:
App\Models\Customer {#1407 ▼ // app\Http\Controllers\CustomerController.php:73
#connection: null
#table: "customers"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
0 => "id"
]
}
I think there is some issue with implicit-model-binding
with Resource- Controller
.Because model-binding
fetch record by id
which in your case is not. So dont use implicit-model-binding
untill you know what the actual issue is. Instead you try this.
public function edit($customer)
{
$results = Customer::where('perusahaancust', $customer)->first();
return view('Master\Customer\editcustomer', [
"customer" => $results,
"title" => "Edit Customer"
]);
}
Before using the above code please make sure the following notes.
Customer
model class has protected $table = 'table-name-in DB'
.
Sometime naming convention also causes error.dd($customer)
to see that it returns a single value upon which
DB record will be fetched.If above mentioned two condition is met,I hope the code will work. If not the please let me know.
For update(...)
method do this.
public function update(Request $request, $customer)
{
$results = Customer::where('perusahaancust', $customer)->first();
$validatedData = $request->validate([
'kodecust' => 'required|string|max:15',
'perusahaancust' => 'required|string|max:255',
'kontakcust' => 'required|string|max:100',
'kotacust' => 'required|string|max:255',
'alamatcust' => 'required|string|max:255',
'alamat2cust' => 'nullable|string|max:255',
'notelponcust' => 'required|string|max:30',
'termcust' => 'required|integer',
'limitcust' => 'required',
'desccust' => 'nullable|string|max:50',
]);
Customer::where('perusahaancust', $results->perusahaancust)
->update($validatedData);
return redirect('/customer/mastercustomer')->with('success', 'Berhasil Edit Customer!');
}
Don't use implicit model-binding
.I have also mentioned this before.
Try this.I hope it will work.