This are my routes
Route::get('/product/create',[ProductoController::class,'createProduct'])->name('product.create')->middleware('auth');
Route::post('/product/create',[ProductoController::class,'storeProduct'])->middleware('auth');
Route::get('/product/edit/{id}',[ProductoController::class, 'editProduct'])->name('product.edit');
Route::put('/product/update/{id}',[ProductoController::class, 'updateProduct'])->name('product.update');
I have a view that I use to create products and edit products but when I create the product the entire design appears but when I click edit it updates the product but the form design does not appear unlike if I go into create product What if the design appears?
This function of my controller creates the product
public function storeProduct(Request $request): RedirectResponse
{
//Array de id de categorías
$idsCategories = $request['categories'];
// Valida los datos del formulario
$validated = $request->validate($this->rules,$this->rulesMessages);
//Creamos el producto y asociamos sus categorías
Producto::create([
'name' => $validated['name'],
'price' => $validated['price'],
'description' => $validated['description'],
'stock' => $validated['stock'],
])->categories()->attach($idsCategories);
//Redirecciona al menu
return redirect(RouteServiceProvider::HOME);
}
This function of my controller updates the product
public function updateProduct(Request $request, $idProduct): RedirectResponse
{
//Recupers el producto
$producto = Producto::find($idProduct);
//Valida los cambios
$validated = $request->validate($this->rules,$this->rulesMessages);
//Creamos el producto y asociamos sus categorías
$producto->update([
'name' => $validated['name'],
'price' => $validated['price'],
'description' => $validated['description'],
'stock' => $validated['stock'],
]);
//Asociar categorías
$producto->categories()->sync($request['categories']);
//Redirecciona al menu
return redirect(RouteServiceProvider::HOME);
}
And this is the form that I want to use to create and edit products, where if I create products the design is seen but if I update the design is not seen
<x-app-layout>
<main class="main-content position-relative max-height-vh-100 h-100 border-radius-lg overflow-x-hidden">
<div class="container-fluid py-4 px-5">
<form role="form" class="text-start" method="POST" action="{{ !isset($product) ? route('product.create') : route('product.update',['id' => $product->id]) }}">
@csrf
@isset($product)
@method('PUT')
@endisset
{{-- Nombre --}}
<div class="mb-3">
<label for="name" class="form-label">Nombre</label>
<input type="text" class="form-control" id="name" name="name" value="{{ $product->name ?? '' }}" aria-describedby="Introducción del nombre">
@error('name')
<span class="text-danger text-sm">{{ $message }}</span>
@enderror
</div>
{{-- Precio --}}
<div class="mb-3">
<label for="price" class="form-label">Precio</label>
<input type="number" class="form-control" id="price" name="price" step="0.01" value="{{ $product->price ?? '' }}" aria-describedby="Introducción del precio">
@error('price')
<span class="text-danger text-sm">{{ $message }}</span>
@enderror
</div>
{{-- Descripción --}}
<div class="mb-3">
<label for="description" class="form-label">Descripción</label>
<input type="text" class="form-control" id="description" name="description" value="{{ $product->description ?? '' }}" aria-describedby="Introducción de la descripción">
@error('description')
<span class="text-danger text-sm">{{ $message }}</span>
@enderror
</div>
{{-- Categorías --}}
<div class="mb-3">
<label for="description" class="form-label">Categoría:</label>
<select class="form-select" id="categories" name="categories[]" multiple required>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
</div>
{{-- Stock --}}
<div class="mb-3">
<label for="stock" class="form-label">Cantidad</label>
<input type="number" class="form-control" id="stock" name="stock" value="{{ $product->stock ?? '' }}" aria-describedby="Introducción de la cantidad">
@error('stock')
<span class="text-danger text-sm">{{ $message }}</span>
@enderror
</div>
<button type="submit" class="btn btn-primary">{{!isset($product) ? 'Crear producto' : 'Actualizar producto'}}</button>
</form>
</div>
</main>
</x-app-layout>
I don’t understand why one can see the design and another can’t if it is the same template. Could you help me?
I would like to solve why the css does not appear if I update the product. Because when I create a product the same template does appear in the css
2
Answers
My suggestion is to create 2 views, one for adding and the other one for updating. Why should we do that? So that we can easily debug and organize the program better.
I see that the view you are using is only one, and your controller for adding and editing uses the same view.
Do you mean that, when you want to edit a product, the fields are not filled with the details?
The route
...->name('product.edit');
is responsible for retrieving the the product with id, and showing the view with the data, right?You haven’t shown the controller which does this, but are you showing the
view
and passing the data into this view correctly?Something like:
If you don’t pass data into the view like the above example, the variable you call inside the view,
$product
, won’t have anything inside it. Which means that when you call$product->price
, for example, it won’t show anything, because there’s nothing in it.