简体   繁体   中英

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

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

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.

  3. The with() method is not appropriate here, you just need to pass a parameter to a function like this

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

Optionally, I recommend following the PSR2 code style standard which includes camel cased variable names so createemoji() should be createEmoji() . Also I think you can usually omit response() when returning most data types in Laravel as it will handle that automatically for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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