我正在嘗試進行編輯,我認為該功能還可以,但是此錯誤一直顯示
在編輯視圖中,我有這個,在路線中,我有資源:
<form action="/user/{{$ad->id}}" method="POST" class="d-flex justify-content-center">
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
</form>
在我的編輯組件中,這里是 $ad 變數沒有出現的地方,我嘗試使用 dd() 并彈出錯誤,該路由有效,因為如果我評論表單并且只保留 h1,它會顯示:
<div>
<h1>Esto es el edit</h1>
{{$slot}}
<div class="formulario">
<div class="mb-3 mt-3">
<label class="label">Nombre del vendedor</label>
<input class="form-control" id="ad_seller" name="ad_name" type="text" value='{{$ad->ad_seller}}'>
</div>
<div >
<label >Nombre del producto</label>
<input id="ad_name" name="ad_name" type="text" value='{{$ad->ad_name}}'>
</div>
<div >
<label >Precio</label>
<input id="ad_price" name="ad_price" type="number" value='{{$ad->ad_price}}'>
</div>
<div >
<label >Descripción</label>
<input id="ad_description" name="ad_description" type="text" tabindex="3" value='{{$ad->ad_description}}'>
</div>
<div >
<label >Imagen</label>
<input id="ad_image" name="ad_image" type="url" tabindex="4" value='{{$ad->ad_image}}'>
</div>
<div >
<button type="button" data-bs-dismiss="modal"><a id="link_home" href="{{ route('home')}}">Cerrar</a></button>
<button type="submit" tabindex="4">Guardar</button>
</div>
</div>
</div>
控制器廣告中的功能:
public function edit($id)
{
$ad = Ad::find($id);
return view('crud.edit')->with('ad', $ad);
}
uj5u.com熱心網友回復:
在你的表單動作中,嘗試使用 Laravel Blade 指令。
這個動作:
<form action="/user/{{$ad->id}}" method="POST" class="d-flex justify-content-center">
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
應該:
<form action="{{ route('user', $ad->id )}}" method="POST" class="d-flex justify-content-center">
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
uj5u.com熱心網友回復:
我試圖在我的本地計算機上重新創建您的問題,我認為問題出在您嘗試將資料傳遞給刀片組件時。
您需要做的是編輯檔案app/View/Components/Edit.php并添加兩件事:
- 添加屬性
public $ad = []; - 在建構式中添加廣告模型作為引數,然后將其分配給屬性
像這樣更新它:
<?php
namespace App\View\Components;
use App\Models\Ad;
use Illuminate\View\Component;
class Edit extends Component
{
public $ad = [];
/**
* Create a new component instance.
*
* @return void
*/
public function __construct(Ad $ad)
{
$this->ad = $ad;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.edit');
}
}
uj5u.com熱心網友回復:
在后端,您可以按如下方式更改默認代碼:
return view('crud.edit', [
'ad' => $ad
]);
這是主要用于 Laravel 8 的指令
uj5u.com熱心網友回復:
=>your ad variable just compact and return .this line add in edit method.
return view('crud.edit', compact('ad'));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426302.html
