试图获取非对象的属性'id' - Laravel(拉拉维尔)



我的路线有问题。当我发布更新时,错误出现在

这是我的表单

这可能在表单操作属性上是错误的,但我认为已经可以了。

@foreach ($products as $p)
<form action="{{ url('edit_product/', $p->id)}}" method="POST" enctype="multipart/form-data">
@method('patch')
@csrf
<div class="form-group">
<label for="product_id">Id Produk</label>
<input type="text" name="id" value="{{ $p->id }}" class="form-control" id="product_id" disabled>
</div>
<div class="form-group">
<label for="product_name">Example label</label>
<input type="text" class="form-control" id="product_name" name="product_name" value="{{ $p->product_name }}" autofocus="" autocomplete="off">
</div>
<div class="form-group">
<label for="image">Pilih Gambar</label>
<input type="file" class="form-control-file" id="image" name="image">
</div>
<div class="form-group">
<label for="preview-image">Gambar saat ini</label><br>
<img src="{{ URL::to('/') }}/admin/image/{{ $p->image }}" alt="{{ $p->image }}" style="width: 200px; height: 100px">
</div>
<div class="form-group">
<label for="preview-image">Preview Gambar Terbaru</label><br>
<img src="" id="preview-image" alt="" style="width: 200px; height: 100px">
</div>
<div class="form-group">
<label for="description">Deskripsi</label>
<textarea type="text" class="form-control" id="description" name="description" autocomplete="off">{{ $p->description }}</textarea>
</div>
<div class="form-group">
<label for="type">Tipe</label>
<select type="text" class="form-control" id="type" name="type">
@foreach ($products as $p)
<option value="{{ $p->id }}" name="type" {{ (isset($p->id) || old('id'))? "selected": "" }}>{{ $p->type }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="price">Harga</label>
<input type="number" class="form-control" id="price" name="price" value="{{ $p->price }}" autocomplete="off">
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-success" value="Simpan">
</div>
</form>
@endforeach

这是我的数据表

我想我在href属性中遇到了问题,坚果,我不知道问题在哪里。

<div class="table-responsive m-b-40">
<table id="dataTable" class="table table-striped table-bordered text-center" style="width:100%">
<thead class="table-primary">
<tr>
<th>No.</th>
<th>Nama Produk</th>
<th>Gambar</th>
<th>Deskripsi</th>
<th>Tipe</th>
<th>Harga</th>
<th>Aksi</th>
</tr>
</thead>
<tbody id="test">
@foreach ($products as $p)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $p->product_name }}</td>
<td><img src="{{ URL::to('/') }}/admin/image/{{ $p->image }}" alt="{{ $p->image }}" style="width: 160px; height: 80px;"></td>
<td>{{ $p->description }}</td>
<td>{{ $p->type }}</td>
<td>{{ $p->price }}</td>
<td>
<a href="/admin/product/{{ $p->id }}/edit" class="btn btn-primary" title="Ubah Data"><i class="far fa-edit "></i>
</a> |
<form action="{{ $p->id }}" method="post" class="d-inline">
@method('delete')
@csrf
<button type="submit" name="submit" onclick="return confirm('Anda yakin ingin menghapus data ini?');" class="btn btn-danger" title="Hapus Data"><i class="far fa-trash-alt"></i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>

这是我的路线

这样写id是正确的方式吗?

Route::get('/admin/product/{id}/edit', 'AdminAdminProductController@edit')->name('edit_product');
Route::patch('/admin/product/{id}', 'AdminAdminProductController@update')->name('update_product');

这是我的控制器

我在"产品"上总是遇到问题

public function edit(Product $products)
{
$products = DB::table('products')->where('id', $id)->get();
return view('admin.partials.product.edit-product', compact('products'));
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  AppadminAdminProduct  $adminProduct
* @return IlluminateHttpResponse
*/
public function update(Request $request)
{
if ($files = $request->file('image')) {
$name = $files->getClientOriginalName();
$files->move('admin/image', $name);
DB::table('products')->where('id', $request->id)->update([
'image' => $name,
'product_name' => $request->product,
'description' => $request->description,
'type' => $request->type,
'price' => $request->price
]);
}
return redirect('/admin/product')->with('message', 'Successfull Save Your Image file.');
}

请尝试一下,它未经测试,但这次你会知道问题在哪里,因为我添加了try-catch块,加上参数中的ID

public function edit(Product $products, $id = 0)
{
try{
$products = DB::table('products')->where('id', $id)->first();
if($products === null){
return redirect()->back()->with([
'status'    => 'danger',
'message'   => 'Invalid Product Id.'
]);
}
return view('admin.partials.product.edit-product', compact('products'));
}catch (Exception $exception){
return redirect()->back()->with([
'status'    => 'danger',
'message'   => $exception->getMessage(),
'lineNum'   => $exception->getLine()
]);
}
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return RedirectResponse|Response
*/
public function update(Request $request, $id = 0)
{
try{
$checkIfProductExist = DB::table('products')->where('id', $id)->first();
if($checkIfProductExist === null){
return redirect()->back()->with([
'status'    => 'danger',
'message'   => 'Invalid Product Id'
]);
}
if($request->hasFile('image')){
$files = $request->file('image');
foreach ($files as $file){
$files->move('admin/image', $file);
DB::table('products')->where('id', $id)->update([
'image'         => $file->getFilename(),
'product_name'  => $request->product,
'description'   => $request->description,
'type'          => $request->type,
'price'         => $request->price
]);
}
}
return redirect()->back()->with([
'status'    => 'success',
'message'   => 'Images Saved Successfully.'
]);
}catch (Exception $exception){
return redirect()->back()->with([
'status'    => 'danger',
'message'   => $exception->getMessage(),
'lineNum'   => $exception->getLine()
]);
}
}

最新更新