如何修复未定义的变量:Laravel 5.6中的图像

我正在使用Laravel 5.6,在我的应用程序中,我在form.blade.php文件中有一个类别创建表单...

@if(isset($image))
    <form method="PUT" action="http://localhost:8000/update" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="put">
@else
   <form method="POST" action="http://localhost:8000/create" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="1LLYc0D1spmVSFMboLDjGM9MR4O5APVwng7giejx">
                                {{csrf_field()}}
@endif

            <div class="form-group row required">
                <label for="description" class="col-form-label col-md-3 col-lg-2">Description</label>
                <div class="col-md-8">
                    <input class="form-control" autofocus placeholder="Description" name="description" type="text" id="description" value="{{$image->categoryname}}">
                </div> 
            </div>

在上述刀片文件中,我同时拥有新类别创建表单和编辑表单。

这是我的CategoryController

public function edit($id)
{
    $images = Category::find($id);

    return view('categories.form')->withImages($images);
}

我的编辑表单运行正常,但是当我单击“新建类别创建”按钮时,它会产生以下错误

未定义的变量:图像(视图:C:\ Users \ Nalaka \ Desktop \ acxian \ resources \ views \ categories \ form.blade.php)

尼古拉·加夫里克(Nikola Gavric)

林不知道我已经看到了这个方法withImage里面Laravel,我建议你尝试只是with,这样的:

public function edit($id)
{
    $image = Category::find($id);

    return view('categories.form')->with(['image' => $image]);
}

如果不工作,那么你需要包括if statementimage description过,因为你有{{ $image->categoryname }},你是不是在这一点上检查它的存在

编辑:要将其包含到一条if else语句中,可以对其使用三元运算符,如下所示:

{{ isset($image) ? $image->categoryname : '' }}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章