检索上传的照片laravel

时间:2014-08-17 21:17:11

标签: php laravel upload

所以我设法上传了多张图片,但现在我需要在一个页面中显示它,我尝试了类似下面的代码,但它说" unidentified variable:photo"

<img class="img-responsive" src="/images/{{ $photo->fileName }}" >

这是我的上传代码,更新了我的代码。

    Route::post('space/add', array('before' => 'auth', function()
{
  $data = Input::all();

  $provider_id = Auth::user()->id;
  $spaces = Space::where('provider_id', '=', $provider_id)->get();

  $space = new Space;
  ....
  $space->save();


  $file = Input::file('image');
  $provider_email = Auth::user()->email;
  // $space = Space::find($id);
   $rules = array(
                 'file' => 'required|mimes:png,gif,jpeg'
             );

             $validator = \Validator::make(array('file'=> $file), $rules);
             if($validator->passes())
             {
               foreach(Input::file('image') as $file)
               {
                  $ext = $file->guessClientExtension(); // (Based on mime type)
                  $name = $file->getClientOriginalName();
                  $fileName = md5($name) . '.' .$ext;
                  $destinationPath = 'images/' . $provider_email;

                 $file = $file->move($destinationPath, $fileName);
                 $photo              = new Image;
                 $photo->provider_id = $provider_id;
                 $photo->spaces_id   = $space->id;
                 $photo->filename    = $fileName;
                 $photo->path        = $destinationPath;
                 $photo->save();
                }
              } else{
                  //Does not pass validation
                  $errors = $validator->errors();
              }


  return Redirect::to('user/spaces')->with(array('spaces' => $spaces, 'photo' => $photo));
}));

1 个答案:

答案 0 :(得分:0)

你需要做类似

的事情
return View::make('yourviewname')->with('photo', $photo);

根据您的设置

,这将在您的路线或您的控制器中

编辑:

您将通过$photo = Session::get('photo')从用户/空间访问用户/空间中的变量,您将在with中传递该变量,即->with('photo', $photo)