未定义的变量:编辑中的类别 blade
Undefined variable: category in edit blade
我的控制器里有这个,但我的 edit.blade
中一直出现以下错误
错误异常
未定义的变量:类别(视图:
命名空间App\Http\Controllers;
使用App\Models\Category;
使用 Illuminate\Http\Request;
class 类别控制器扩展控制器
{
/**
* 显示资源列表。
*
* @return \Illuminate\Http\Response
*
*/
public 函数 __construct()
{
}
public function index()
{
$categories = category::all();
return view('category.index', [$categories = 'category']);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('category.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required'
]);
Category::create($request->all());
return redirect()->back()->with('success', 'Category Successfully Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$category=category::find($id);
return view('category.edit', [$categories = 'category']);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
]);
$category=category::find($id);
$category->update();
return redirect()->route('category.index')
->with('success','Category updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这是我的edit.blade
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{$category->name}}" placeholder="Category Name">
像这样修复你的功能。我假设你的模型都是小写的
public function index()
{
$categories = category::all();
return view('category.index', ['categories' => $categories]);
}
public function edit($id)
{
$category=category::find($id);
return view('category.edit', ['category' => $category]);
}
类别 字符串是 blade 文件中的变量, $categories 是来自控制器的变量
这是您的索引 blade 文件中的示例
@foreach ($categories as $category)
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{ $category->name }}" placeholder="Category Name">
@endforeach
这是用于编辑 blade 文件
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{ $category->name }}" placeholder="Category Name">
我的控制器里有这个,但我的 edit.blade
中一直出现以下错误错误异常 未定义的变量:类别(视图:
命名空间App\Http\Controllers;
使用App\Models\Category; 使用 Illuminate\Http\Request;
class 类别控制器扩展控制器 { /** * 显示资源列表。 * * @return \Illuminate\Http\Response * */ public 函数 __construct() {
}
public function index()
{
$categories = category::all();
return view('category.index', [$categories = 'category']);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('category.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required'
]);
Category::create($request->all());
return redirect()->back()->with('success', 'Category Successfully Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$category=category::find($id);
return view('category.edit', [$categories = 'category']);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
]);
$category=category::find($id);
$category->update();
return redirect()->route('category.index')
->with('success','Category updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这是我的edit.blade
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{$category->name}}" placeholder="Category Name">
像这样修复你的功能。我假设你的模型都是小写的
public function index()
{
$categories = category::all();
return view('category.index', ['categories' => $categories]);
}
public function edit($id)
{
$category=category::find($id);
return view('category.edit', ['category' => $category]);
}
类别 字符串是 blade 文件中的变量, $categories 是来自控制器的变量
这是您的索引 blade 文件中的示例
@foreach ($categories as $category)
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{ $category->name }}" placeholder="Category Name">
@endforeach
这是用于编辑 blade 文件
<input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" value="{{ $category->name }}" placeholder="Category Name">