title: How to check to see if Image exists, else display something else Laravel 5.2
body: <p>I need to check to see if a dynamically populated image exists for my banner that a user can upload, if a user did not upload a banner image, then I want to display something else, like a default image.</p>
  
  <p>Here is my image banner that i display.</p>
  
  <pre class="lang-html prettyprint-override"><code>@foreach ($flyer-&gt;bannerPhotos as $photo)
      &lt;img src="/travel/{{ $photo-&gt;thumbnail_path }}" alt="{{ $flyer-&gt;owner-&gt;username }}" data-id="{{ $photo-&gt;id }}" id="Banner-image"&gt;
  @endforeach
  </code></pre>
  
  
  
  <p>I tried doing "file_exists" and if $photo == '', then display something else, but it doesnt work. Do any of u know of a way of doing this?</p>
  
  <p>*********** EDIT ************</p>
  
  <p>TravelFlyerController.php  (shortened)</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\User;
  use App\Flyer;
  use App\FlyerPhoto;
  use App\FlyerBanner;
  use App\Http\Requests;
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use Illuminate\Support\Facades\Auth;
  use App\Http\Requests\FlyerPhotoRequest;
  use App\Http\Requests\FlyerBannerRequest;
  use App\Http\Requests\TravelFlyersRequest;
  
  class TravelFlyersController extends Controller {
  
      /**
       * Add banner photo to a flyer.
       *
       * @param $title
       * @param FlyerBannerRequest $request
       */
      public function addBannerPhoto($title, FlyerBannerRequest $request) {
          // create a new photo instance from a file upload
          $photo = FlyerBanner::fromFile($request-&gt;file('photo'))-&gt;upload();
  
          // Set Flyer::LocatedAt() in (Flyer.php Model)
          // = to the title, and add the banner photo.
          // -- Find the flyer and add the banner photo.
          Flyer::LocatedAt($title)-&gt;addBannerPhoto($photo);
      }
  </code></pre>
  
  
  
  <p>FlyerBanner.php Model:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?php
  
  namespace App;
  
  use Image;
  use Illuminate\Database\Eloquent\Model;
  use Symfony\Component\HttpFoundation\File;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  class FlyerBanner extends Model {
  
      /**
       * @var string
       */
      protected $table = "flyer_banner";
  
      /**
       * @var array
       */
      protected $fillable = ['name', 'path', 'thumbnail_path'];
  
      /**
       * @var
       */
      protected $file;
  
      /**
       * @var
       */
      protected $name;
  
  
      /**
       * A banner photo belongs to a flyer.
       *
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function flyer() {
        return $this-&gt;belongsTo('App\Flyer');
      }
  
  
      /**
       * Make a new instance from an uploaded file.
       *
       * @param UploadedFile $file
       * @return static
       */
      public static function fromFile(UploadedFile $file) {
          // Make new instance of photo.
          $photo = new static;
  
          // Assign the Uploaded file to the $file object.
          $photo-&gt;file = $file;
  
          // Set $photo to the fill properties, which are
          // the name, path, and thumbnail path of a photo.
          $photo-&gt;fill([
              'name' =&gt; $photo-&gt;setFileName(),
              'path' =&gt; $photo-&gt;filePath(),
              'thumbnail_path' =&gt; $photo-&gt;thumbnailPath()
          ]);
  
          // return the photo
          return $photo;
  
      }
  
  
      /**
       * Get the banner photos base directory.
       */
      public function baseDir() {
          return 'src/public/FlyerBanner/photos';
      }
  
  
      /**
       * Get the name and extension of the banner photo.
       *
       * @return string
       */
      public function setFileName() {
          // Get the file name original name
          // and encrypt it with sha1
          $hash = sha1 (
              $this-&gt;file-&gt;getClientOriginalName()
          );
  
          // Get the extension of the photo.
          $extension = $this-&gt;file-&gt;getClientOriginalExtension();
  
          // Then set name = merge those together.
          return $this-&gt;name = "{$hash}.{$extension}";
      }
  
  
  
      /**
       *  Return the full file path of the banner photo, with the name.
       *
       * @return string
       */
      public function filePath() {
          return $this-&gt;baseDir() . '/' . $this-&gt;name;
          // Ex: 'BannerPhoto/photos/foo.jpg'
      }
  
  
      /**
       * Return the full file thumbnail path of the banner photo, with the name.
       *
       * @return string
       */
      public function thumbnailPath() {
          return $this-&gt;baseDir() . '/tn-' . $this-&gt;name;
          // Ex: 'BannerPhoto/photos/tn-foo.jpg'
      }
  
  
      /**
       * Upload the file to the proper directory.
       *
       * @return $this
       */
      public function upload() {
          // move a file to the base directory with the file name.
          $this-&gt;file-&gt;move($this-&gt;baseDir(), $this-&gt;name);
  
          // Make the thumbnail.
          $this-&gt;makeThumbnail();
  
          return $this;
      }
  
  
      /**
       * Function to make the actual thumbnail.
       * -- make and save reference the Image intervention library, not Eloquent. --
       */
      protected function makeThumbnail() {
          Image::make($this-&gt;filePath())
              -&gt;fit(2000, 800)
              //-&gt;resize(null, 400, function ($constraint) {
               //   $constraint-&gt;aspectRatio();
               //   $constraint-&gt;upsize();
              //})
              -&gt;save($this-&gt;thumbnailPath());
      }
  
  
      /**
       * Delete the banner photo path and thumbnail path in DB.
       * Access the delete function in FlyerController@destroyBannerPhoto method
       */
      public function delete() {
  
          $image = $this-&gt;path;
          $thumbnail_image = $this-&gt;thumbnail_path;
  
          \File::delete([
              $image,
              $thumbnail_image
          ]);
  
          parent::delete();
      }
  
  }
  </code></pre>
  
  
  
  <p>Flyer Photo Banner Upload Form:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form action="/travel/{{ $flyer-&gt;title }}/banner" method="post" class="dropzone" id="addBannerForm" enctype="multipart/form-data"&gt;
    {{ csrf_field() }}
  &lt;/form&gt;
  </code></pre>
  
  
  
  <p>My Route:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Route::group(['middleware' =&gt; ['web']], function () {
  
      /** Resource Route For Travel Flyers */
      Route::resource('travelflyers', 'TravelFlyersController');
  
      /** Show a Flyer. **/
      Route::get('{title}', 'TravelFlyersController@show');
  
     
  
      /** Add a photo banner to a flyer **/
      Route::post('{title}/banner', 'TravelFlyersController@addBannerPhoto');
  
      /** Delete Flyer Banner photo **/
      Route::delete('photo/{id}', [
          'uses' =&gt; '\App\Http\Controllers\TravelFlyersController@destroyBannerPhoto',
          'as'   =&gt; 'flyer.delete.banner',
      ]);
  
  });</code></pre>
  </div>
  </div>
  </p>