title: Laravel 5.1 Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string
body: <blockquote>
    <p>I have problem with my laravel 5.1 error Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string when i click delete</p>
  </blockquote>
  
  <p><strong>GalleryController.php</strong></p>
  
  <pre><code>public function deleteGallery($id)
  {
      //load the gallery
      $currentGallery = Gallery::findOrfail($id);
  
      // check ownership
      if ($currentGallery-&gt;created_by != Auth::user()-&gt;id) {
          abort('403','You are not allowed to delete this gallery');
      }
  
      // get the images   
      $images = $currentGallery-&gt;images();    
  
      // delete the images
      foreach ($currentGallery-&gt;$images as $image) {
          unlink(public_path($image-&gt;file_path));
      }
  
      // delete the DB records
      $currentGallery-&gt;images()-&gt;delete();
  
      $currentGallery-&gt;delete();
  
      return redirect()-&gt;back();
  }
  </code></pre>
  
  <p><strong>Route.php</strong></p>
  
  <pre><code>Route::get('gallery/delete/{id}','GalleryController@deleteGallery');
  </code></pre>
  
  <p><strong>Model Gallery.php</strong></p>
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Gallery extends Model
  {
      protected $table = 'gallery';
  
      public function images()
      {
          return $this-&gt;hasMany('App\Image');
      }
  }
  </code></pre>
  
  <p><strong>Model Image.php</strong></p>
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class image extends Model
  {
      protected $fillable = ['gallery_id', 'file_name', 'file_size', 'file_mime', 'file_path', 
      'created_by'];
  
      public function gallery()
      {
          return $this-&gt;belongsTo('App\Gallery');
      }
  
  }
  </code></pre>
  
  <p><strong>Views gallery.blade.php</strong></p>
  
  <pre><code>@extends('master')
  
  @section('content')
  &lt;div class="row"&gt;
      &lt;div class="col-md-12"&gt;
          &lt;h1&gt;My Gallery&lt;/h1&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="col-md-8"&gt;
          @if ($galleries-&gt;count() &gt; 0)
          &lt;table class="table table-striped table-bordered table-responsive"&gt;
              &lt;thead&gt;
                  &lt;tr class="info"&gt;
                      &lt;th&gt;Name of the gallery&lt;/th&gt;
                      &lt;th&gt;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;                
              &lt;tbody&gt; 
                  @foreach($galleries as $gallery)
                  &lt;tr&gt;
                      &lt;td&gt;{{$gallery-&gt;name}}
                          &lt;span class="pull-right"&gt;
                              {{ $gallery-&gt;images()-&gt;count() }}
                          &lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td&gt;&lt;a href="{{url('gallery/view/'.$gallery-&gt;id)}}"&gt;View&lt;/a&gt; / 
                          &lt;a href="{{url('gallery/delete/'.$gallery-&gt;id)}}"&gt;Delete&lt;/a&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;       
                  @endforeach             
              &lt;/tbody&gt;
          &lt;/table&gt;
          @endif
      &lt;/div&gt;
  
      &lt;div class="col-md-4"&gt;
       @if (count($errors) &gt; 0)
       &lt;div class="alert alert-danger"&gt;
          &lt;ul&gt;
              @foreach ($errors-&gt;all() as $error)
              &lt;li&gt;{{ $error }}&lt;/li&gt;
              @endforeach
          &lt;/ul&gt;
       &lt;/div&gt;
       @endif
          &lt;form class="form" method="POST" action="{{url('gallery/save')}}"&gt;  
          &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;  
  
          &lt;div class="form-group"&gt;
              &lt;input type="text" name="gallery_name"
              id="gallery_name" placeholder="Name of the gallery" 
              class="form-control"
              value="{{ old('gallery_name')}}" /&gt;
          &lt;/div&gt;
  
          &lt;button class="btn btn-primary"&gt;Save&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  @endsection
  </code></pre>