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 : '' }}

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

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?