简体   繁体   English

Laravel如何获取图像路径?

[英]Laravel how to get a image path?

Hi i'm new using Laravel and i have a form with a upload file button, but this is optional on my form, if i don't want to upload an image, my app stores one by default (no-image.jpg) and this image is in my storage_path, how i can get this path and assign it to my new record? 嗨,我是Laravel的新手,我有一个带有上传文件按钮的表单,但这在我的表单上是可选的,如果我不想上传图片,则我的应用默认存储一个图片(no-image.jpg)这张图片在我的storage_path中,我如何获得该路径并将其分配给我的新记录?

I tried this but it doesn't work: 我试过了,但是没有用:

if($request->infopath == null){
    $request->infopath = public_path() . "/infopath"."/". 'no-image.jpg';
    PreguntaAbierta::create($request->all());
    Session::flash('store-success','Datos agregados correctamente!');
    return redirect()->route('abierta.index');
}

'SOLUTION' '解'

I finally found a solution , although an unorthodox but it does what I want, but I doubt remains =/ On my view: 我终于找到了一个解决方案,尽管这是非正统的,但却可以满足我的要求,但是我怀疑仍然是= /在我看来:

  @if($pregunta->infopath == null)
     <td><img src="infopath/no-image.jpg" alt="" style="width:100px"/></td>
  @endif

LOL 大声笑

You can use ->with 您可以使用->with

return redirect()->route('abierta.index')->with('infopath',$request->infopath);

and get this as $infopath in abierta.index 并在abierta.index其作为$ infopath abierta.index

Also there is multiple option you can check here : https://laravel.com/docs/5.1/responses 另外,您可以在此处检查多个选项: https : //laravel.com/docs/5.1/responses

You can do like this: 您可以这样:

$input = $request->all();
$input['infopath'] = $your_image_public_path;
PreguntaAbierta::create($input);

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

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