JoroATC
69
10
Laravel

Mystery Error in view

Posted 1 year ago by JoroATC

I have an app where companies are stored. Each company has many offices. Each office belongs to a city. I have: Route:

Route::get('/cities/{city}/offices', '[email protected]')->name('city.offices');

controller:

    public function offices(City $city){
        
        $totalOfficesCount = $city->offices()->count();
        
        $offices = $city->offices()->paginate(50);
        
        return view('cities.offices',compact('offices','city','totalOfficesCount'));
        
    }

Model:

class City extends Model
{
    
    public function offices(){
        return $this->hasMany(Office::class);
    }
    
    public function country(){
        return $this->belongsTo(Country::class);
    }
    
}

As you can see the controller paginates.

THE PROBLEM:

When I ask for/cities/6/offices it throws an error. Here is the view:

/Users/gv/Public/www/om4/storage/framework/views/f19c5e6fa2a2a6210d9885484c7a223de31242de.php
                      </span>
                    
                    </div><!-- /input-group -->
                  
                  </div><!-- /.col-lg-12 -->
                  <div class="col-lg-12">
                      <?php echo e($offices->links()); ?> &nbsp
                  </div>
                    <table class="table table-hover">
                        <tr>
                            <th>Фирма</th>
                            <th>Имейл</th>
                            <th>Телефон</th>
                            <th>Телефон 2</th>
                            <th>Факс</th>
                            <th>Адрес</th>
                        </tr>
                        <?php $__currentLoopData = $offices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $office): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                        <td><a href="/companies/<?php echo e($office->company->id); ?>"><?php echo e($office->company->name); ?></a></td>
                        <td><?php echo e($office->email); ?></td>
                        <td><?php echo e($office->phone); ?></td>
                        <td><?php echo e($office->phone2); ?></td>
                        <td><?php echo e($office->fax); ?></td>
                        <td><a href="/offices/<?php echo e($office->id); ?>"><?php echo e($office->street); ?>.</a> <?php echo e($office->post_code); ?> <?php echo e($office->city->name); ?></td>
    
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </table>
                  <div class="col-lg-12">
                      <?php echo e($offices->links()); ?> &nbsp
                  </div>
                </div><!--/.panel-body-->
            
            </div>
        
        </div>
    
    </div>
<?php $__env->stopSection(); ?>
Arguments
"Trying to get property of non-object (View: /Users/gv/Public/www/om4/resources/views/cities/offices.blade.php)"

And this error appear only for city with id=6 and on the first page. If I try: http://localhost:8888/cities/6/offices?page=2 It works. And it works for all the other cities that I tried. They paginate as well and their first page works. Even if i provide ?page=1 still get the error.

PLEASE HELP!!!

Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.