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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝