R
R
reper6662019-03-06 01:48:27
Laravel
reper666, 2019-03-06 01:48:27

Error displaying Laravel category title?

Throws an error when going to the /catalog/{id} route. I tried to display the title of the corresponding category, but apparently I did something wrong, please tell me where the error is.

// Роуты
                 Route::get('/', '[email protected]');
                 Route::get('/catalog', '[email protected]');
                 Route::get('/catalog/{id}', '[email protected]_page');
                 Route::get('/contacts', '[email protected]');

         //Контроллеры
class SiteController extends Controller
{

public function catalog()
{
    return view('catalog', ['menus' => Menu::all(),'products' => Tovar::all()]);
}
public function catalog_page($id)
    {
    $category = new Menu();
    $category = $category->with('children')->where('id', $id)->get()->toArray();

        return view('catalog_page', ['menus' => Menu::all(),'products' => Tovar::all(),'result' => $category]);
    }

public function contacts()
{
    return view('contacts', ['menus' => Menu::all(),'products' => Tovar::all(),]);
}
public function index()
{
    return view('index',['menus' => Menu::all(),'products' => Tovar::all(),]);
 }
}

           // Модель Menu
         <?php

         namespace App;

         use Illuminate\Database\Eloquent\Model;

         class Menu extends Model
        {
         protected $table = 'menus';
        }
//Вывод заголовка

@extends('template')


      @section('content')

      <div class="content__right">

      <h1>{{$result['title']}}</h1>
      </div>

     @endsection

Undefined index: title (View: D:\OSPanel\domains\blog\resources\views\catalog_page.blade.php) - error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2019-03-06
@procode

It's better to immediately accustom yourself to solve such questions correctly
Before displaying the title, look at the contents of $result
First in the view, then inside the function in the controller. More on the situation. I think 5 minutes to solve this issue.

J
jazzus, 2019-03-06
@jazzus

public function catalog_page($id)
{
    $menus = Menu::all();
    $category = Category::with('children')->find($id);

     return view('catalog_page', compact('menus','category'));
}

in template
<h1>
{{$category->title}}
</h1>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question