A
A
alexei_20022022-02-13 20:58:03
Laravel
alexei_2002, 2022-02-13 20:58:03

What is the error in passing/substituting data when editing a category?

When you click on the edit button (the buttons have the correct url), an error occurs

Missing required parameter for [Route: categories.update] [URI: admin_panel/categories/{category}] [Missing parameter: category]. (View: C:\OpenServer\domains\LaravelAdminPanel\resources\views\admin\categories\edit.blade.php)

Can't figure out how to pass data from CategoriesController.php (namely, edit function) to edit.blade.php form
CategoriesController.php

public function edit(Categories $categories)
    {
        return view('admin.categories.edit', ['categories' => $categories]);
    }


edit.blade.php
<form action="{{ route('categories.update', $categories->id) }}" method="POST">
                @csrf
                @method('PUT')
                <div class="card-body">
                  <div class="form-group">
                    <label for="title">Название</label>
                    <input type="text" value="{{ $categories->title }}" class="form-control" name="title" id="title" placeholder="Введите название категории" required>
                  </div>


PS Oddly enough, everything works in the lesson on YouTube

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alexei_2002, 2022-02-13
@alexei_2002

I rewrote the function in my own way and it worked

public function edit(Categories $categories, $id)
    {
        $categories = new Categories;
        $categories = $categories->find($id);
        return view('admin.categories.edit', ['categories' => $categories]);
    }

PS I'm not sure about the correctness of this approach

S
Sergey delphinpro, 2022-02-13
@delphinpro

Categories is a model, right? Then why is it named in the plural?
You are editing the category , not the categories Model
- Category, table - categories
Route:

Route::post('categories/{category}')->name('category.update');

And then the magic of implicit binding will work, and the correct object will be passed to the controller.
public function edit(Category $category)
{
    return view('admin.category.edit', ['category' => $category]);
}

<form action="{{ route('category.update', $category) }}"
      method="POST"
>

T
Timur Maslov, 2022-02-14
@tmaslov22

There is a naming convention in laravel that should be followed. Then the magic of the framework will start working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question