dongnius85154 2018-10-10 13:22
浏览 151
已采纳

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

I'm working with Laravel 5.6 and in my application I have a category create form in the form.blade.php file...

@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>

I have both new category create form and edit form in the above blade file.

this is my CategoryController

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

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

My edit form is working fine but when I click new Category create button it is generating following error

Undefined variable: image (View: C:\Users\Nalaka\Desktop\acxianesources\views\categories\form.blade.php)

  • 写回答

1条回答 默认 最新

  • dtbonklcs575884485 2018-10-10 13:35
    关注

    Im not sure I've seen this method withImage inside of Laravel, I would suggest you try with just with, like this:

    public function edit($id)
    {
        $image = Category::find($id);
    
        return view('categories.form')->with(['image' => $image]);
    }
    

    If that doesn't work, then you need to include if statement for the image description too, because you have {{ $image->categoryname }} and you are not checking for it's existence at that point

    EDIT: To envelop this into an if else statement, you can use ternary operator for it, like this:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密