'Laravel I have an Error Undefined variable $ad when trying to do an edit
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" class="d-flex justify-content-center">
<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 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 class="mb-3 mt-3">
<label class="label">Nombre del producto</label>
<input class="form-control" id="ad_name" name="ad_name" type="text" value='{{$ad->ad_name}}'>
</div>
<div class="mb-3 mt-3">
<label class="label">Precio</label>
<input class="form-control" id="ad_price" name="ad_price" type="number" value='{{$ad->ad_price}}'>
</div>
<div class="mb-3">
<label class="label">Descripción</label>
<input class="form-control" id="ad_description" name="ad_description" type="text" tabindex="3" value='{{$ad->ad_description}}'>
</div>
<div class="mb-3">
<label class="label">Imagen</label>
<input class="form-control" id="ad_image" name="ad_image" type="url" tabindex="4" value='{{$ad->ad_image}}'>
</div>
<div class="modal-footer d-flex">
<button type="button" class="ml-3" 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);
}
Solution 1:[1]
Inside your form action, try to use the Laravel Blade Directive.
This action:
<form action="/user/{{$ad->id}}" method="POST" class="d-flex justify-content-center">
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
Should be:
<form action="{{ route('user', $ad->id )}}" method="POST" class="d-flex justify-content-center">
<x-edit :ad="$ad">
@csrf
@method('PUT')
</x-edit>
Solution 2:[2]
I tried to recreate your issue on my local machine, and I think that the issue is when you try to pass the data to the blade component.
What you need to do is to edit the file app/View/Components/Edit.php and add two things:
- add the property
public $ad = []; - add the ad model as a parameter in the constructor, and then assign it to the property
Update it like this:
<?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');
}
}
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | calebe santana |
| Solution 2 | |
| Solution 3 | Nice Boy |
