在laravel 5.4中包含视图时遇到问题 这是错误:
3/3)ErrorException未定义变量:类别(查看: C:\ xampp \ htdocs \ blog \ resources \ views \ partials \ index.blade.php)(查看: C:\ XAMPP \ htdocs中\博客\资源\视图\泛音\ index.blade.php)
我的索引:
@extends('layouts.master')
@section('content')
@include('partials.index')
@endsection
________
// this is index in partials
<div class="container index-cat">
<div class="row">
<div class="col-md-12">
<h1>Категория:</h1>
<div class="pull-right add-cat">
<a href="{{url('categories/create')}}" class="btn btn-success">Дабавить Катигорию</a>
</div>
<div class="row">
@foreach($categories as $cat)
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{asset ('storage/' . $cat->photo)}}" alt="...">
<div class="caption">
<h3>{{$cat->title}}</h3>
<form action="{{url('categories/' . $cat->id)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons-bootstrap">
<a href="{{url('categories/' . $cat->id . '/description')}}" class="btn btn-primary" role="button">подробности</a>
<a href="{{url('categories/' . $cat->id . '/edit')}}" class="btn btn-warning" role="button">редактировать</a>
<button type="submit" class="btn btn-danger">удалить</button>
</div>
</form>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
我的控制器:
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use App\Http\Requests\catRequest;
use App\Category;
use App\User;
use Auth;
use Illuminate\Support\Facades\Storage;
class CategoryController extends Controller
{
//middleware auth
public function __construct()
{
$this->middleware('auth');
}
public function show($id )
{
$cat = Category::query()->find($id);
return view('categories.description' , ['categories' => $cat]);
}
public function index(){
//$listCat = Category::all();
$listCat = Category::where('user_id' , Auth::user()->id)->get();
return view('categories.index' , ['categories' => $listCat]);
}
public function create(){
return view('categories.create');
}
public function store( catRequest $request ){
//return $request->all();
$cat = new Category();
$cat->title = $request->input('title');
$cat->description = $request->input('description');
$cat->user_id = Auth::user()->id;
if($request->hasFile('photo')){
$cat->photo = $request->photo->store('photo');
}
$cat->save();
session()->flash('success' , 'Катигория дабавлена');
return redirect('categories');
}
/**
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id){
$cat = Category::query()->find($id);
return view('categories.edit' , ['categories' => $cat]);
}
/**
* @param Request $request
* @param $id
*/
public function update(catRequest $request, $id){
$cat = Category::query()->find($id);
$cat->title = $request->input('title');
$cat->description = $request->input('description');
if($request->hasFile('photo')){
$cat->photo = $request->photo->store('photo');
}
$cat->save();
return redirect('categories');
}
public function destroy(Request $request, $id){
$cat = Category::query()->find($id);
$cat->delete();
return redirect('categories');
}
}
答案 0 :(得分:0)
您的错误似乎正在描述缺少的变量
ErrorException未定义的变量:类别。
这意味着刀片模板无法看到您在for循环中使用的$categories
变量。
检查调用刀片模板的web.php
或app/Http/Controller/file.php
,并确保将变量传递给视图功能。
例如
return view('greeting', ['name' => 'James']); // <= passing a variable $name with string 'James'