I'm trying to do an edit, I think the function is okay, but this error keeps showing
In edit view I have this, also, in routes, I have resource:
<form action="/user/{{$ad->id}}" method="POST" >
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
</form>
In my edit component, here is where the $ad variable doesn't come, I tried using dd() and the error pops up, the route works because if I comment the form, and only keep the h1, it shows:
<div>
<h1>Esto es el edit</h1>
{{$slot}}
<div >
<div >
<label >Nombre del vendedor</label>
<input 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>
The function in controller Ad:
public function edit($id)
{
$ad = Ad::find($id);
return view('crud.edit')->with('ad', $ad);
}
CodePudding user response:
Inside your form action, try to use the Laravel Blade Directive.
This action:
<form action="/user/{{$ad->id}}" method="POST" >
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
Should be:
<form action="{{ route('user', $ad->id )}}" method="POST" >
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
CodePudding user response:
In the backend side, you can change your default code as follows:
return view('crud.edit', [
'ad' => $ad
]);
This is directive which is mainly used in Laravel 8
CodePudding user response:
I tried to recreate your issue on my local machine, and I think that the issue is when you try to find your model with $id.
So instead of:
public function edit($id)
{
$ad = Ad::find($id);
return view('crud.edit')->with('ad', $ad);
}
Try this:
public function edit($id)
{
$ad = Ad::where('id', $id)->first();
return view('crud.edit')->with('ad', $ad);
}