简体   繁体   English

刀片视图中的未定义变量错误

[英]undefined variable error in blade view

I am currently working with Laravel 5.2, trying to display images on click which I have currently stored in the Storage folder. 我目前正在使用Laravel 5.2,尝试显示当前存储在“ 存储”文件夹中的单击图像。 I am trying to display these images in my blade view but every time it loads the page, it gets to an undefined variable exception. 我试图在刀片视图中显示这些图像,但是每次加载页面时,都会出现未定义的变量异常。

Controller : 控制器

public function createemoji($action,$statusId)
{  
    $path = storage_path('app/public/images/'.$action.'.gif');

    /*$request=new storage();
    $request->comment=$path;
    $request->user_id=Auth::user()->id;
    $request->post_id=$statusId;
    $request->save();*/
    return redirect()->returnemoji()->with('file'->$path);

}

public function returnemoji($file)
{           
    return Image::get('$file')->response();
}

In my default view I tried using count() but everytime it loads the page, it gives me Undefined variable . 在我的默认视图中,我尝试使用count(),但是每次加载页面时,都会给我Undefined variable How should I display it? 我应该如何显示?

Try to change this: 尝试更改此:

->with('file'->$path);

To this: 对此:

->with('file', $path);

https://laravel.com/docs/5.3/views#passing-data-to-views https://laravel.com/docs/5.3/views#passing-data-to-views

I think you have to try the following: 我认为您必须尝试以下方法:

Instead of: 代替:

return redirect()->returnemoji()->with('file'->$path);

Try this: 尝试这个:

return redirect()->returnemoji($path);

And yes, remove the quotes from this: 是的,从中删除引号:

return Image::get('$file')->response();

With function takes two arguments key and value With函数带有两个参数key和value

You can use this 你可以用这个

return redirect()->returnemoji()->with('file',$path);

You can try this out: 您可以尝试以下方法:

Instead of: 代替:

return redirect()->returnemoji()->with('file'->$path);

Try this: 尝试这个:

return $this->returnemoji($path);

Hope this helps you. 希望这对您有帮助。

There are a few problems. 有一些问题。

  1. Single quotes do not process variables, so instead of this 单引号不处理变量,所以代替此

     return Image::get('$file')->response(); 

    You could do this 你可以这样做

     return Image::get("$file")->response(); 

    or 要么

     return Image::get("{$file}")->response(); 

    but none of this is necssary since you are just using the variable by itself without any additional formatting, so remove the quotes altogether 但这并不是必需的,因为您仅使用变量本身而无需任何其他格式,因此请完全删除引号

     return Image::get($file)->response(); 
  2. The object operator -> is used in object scope to access methods and properties of an object. 对象运算符->在对象范围内用于访问对象的方法和属性。 Your function returnemoji() is not a method of RedirectResponse class which is what the redirect() helper method returns. 您的函数returnemoji()不是RedirectResponse类的方法,而这是redirect()帮助器方法所返回的。

  3. The with() method is not appropriate here, you just need to pass a parameter to a function like this with()方法在这里不合适,您只需要将参数传递给这样的函数

     return redirect()->returnemoji($path); 

Optionally, I recommend following the PSR2 code style standard which includes camel cased variable names so createemoji() should be createEmoji() . 可选地,我建议遵循PSR2代码样式标准 ,其中包括驼峰式变量名,因此createemoji()应该是createEmoji() Also I think you can usually omit response() when returning most data types in Laravel as it will handle that automatically for you. 我也认为在Laravel中返回大多数数据类型时,通常可以省略response() ,因为它将为您自动处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM