【问题标题】:Undefined variable: category in edit blade [closed]未定义的变量:编辑刀片中的类别[关闭]
【发布时间】:2021-07-25 04:03:37
【问题描述】:

我的控制器中有这个,但它在我的 edit.blade 中始终出现以下错误

错误异常 未定义变量:类别(查看:

命名空间 App\Http\Controllers;

使用应用\模型\类别; 使用 Illuminate\Http\Request;

类 CategoryController 扩展 Controller { /** * 显示资源列表。 * * @return \Illuminate\Http\Response * */ 公共函数 __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">

【问题讨论】:

  • 这是一个拼写错误,它将是 {{$categories-&gt;name}},还将您的控制器代码更改为 [$categories = 'category'][$categories =&gt; 'category']==&gt; 这是您代码的另一个拼写错误跨度>
  • [$categories = 'category'] - 它的作用是将字符串'category' 分配给$category 变量,覆盖您从数据库中获得的内容。我假设您想将$category 变量放在'category' 索引下。
  • @Espresso [$categories =&gt; 'category'] 无效。应该反过来。
  • 哎呀,我的错,应该是['categories' =&gt; $categories]@El_Vanja

标签: php laravel controller laravel-blade edit


【解决方案1】:

像这样修复你的函数。我假设你的模型都是小写的

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]); 
}

categories 字符串是刀片文件中的变量,$categories 是来自控制器的变量

这是您的索引刀片文件中的示例

@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

这是用于编辑刀片文件

<input type="text" name="name"  id="name" class="form-control @error('name') is-invalid @enderror" value="{{ $category->name }}" placeholder="Category Name">

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2018-11-26
    • 2017-06-17
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2017-08-24
    • 2022-11-02
    相关资源
    最近更新 更多