0

I have the following code in my controller:

    public function getBycountryCategory($country,$category)
{
    $category = TourCategory::where('slug',$category)->first();
    $country = Country::where('name','=',$country)->first();
    $tours = $country->tours()->with('region')->get(['region_id']);
    $regions = $tours->pluck('region')->unique();

        return view('public.pages.region-list')
        ->withRegions($regions)
        ->withCategory($category)
        ->withCountry($country);
 }

@foreach($regions as $region)
  <div class="col-md-4">
    <a href="{{ route('getByregion',[$category->slug,$region->slug]) }}">
      <img src="{{ asset($region->image) }}" alt="">
      <div class="location-item" style="background-image: url({{ asset($region->image) }});">

        <div class="location-term">{{ucfirst(str_replace("-", " ", $region->name))}}</div>
      </div>
    </a>
  </div>  
@endforeach

The above code works perfectly in my local machine but in live server it returns the error message of Trying to get property 'image' of non-object or anything I want to get from $region. When I dump and die $region variable it shows all the attributes of the variable.

The only thing I couldn't understand is the code doesn't give any kind of error in local development server but gives in production server.

psudo
  • 1,341
  • 3
  • 24
  • 69
  • Hi, can you check and see if this works or not? $regions = $tours->pluck('region')->unique()->first(); – ashish Jun 28 '18 at 10:34
  • 1
    @ashish I figured out the error. A null in a collection was the causing the error. – psudo Jun 28 '18 at 11:24

0 Answers0