切换到router / catalog / {id}
时出现错误。试图得出相应类别的标题,但显然做错了,请告诉我错误在哪里。
// Routers
Route::get('/', 'SiteController@index');
Route::get('/catalog', 'SiteController@catalog');
Route::get('/catalog/{id}', 'SiteController@catalog_page');
Route::get('/contacts', 'SiteController@contacts');
//Controllers
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(),]);
}
}
// Model Menu
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model
{
protected $table = 'menus';
}
// views
@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
答案 0 :(得分:0)
检查category是string还是array。如果是字符串,您可以
// views
@extends('template')
@section('content')
<div class="content__right">
<h1>{{$result}}</h1>
</div>
@endsection
如果数组比
检查$category
是否具有 title 键。只需打印并验证即可。
您能否转储$category
数据并显示它。
答案 1 :(得分:0)
该错误表示$category
没有索引title
。
您可以像这样使用dd()
来检查变量$category
。
$category = $category->with('children')->where('id', $id)->get()->toArray();
dd($category);
您可以这样:
// views
@extends('template')
@section('content')
<div class="content__right">
@if($result)
<h1>{{$result['title']}}</h1>
@else
<h1>no title</h1>
</div>
@endif
@endsection