1 year ago

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');


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


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.


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

                    </div><!-- /input-group -->
                  </div><!-- /.col-lg-12 -->
                  <div class="col-lg-12">
                      <?php echo e($offices->links()); ?> &nbsp
                    <table class="table table-hover">
                            <th>Телефон 2</th>
                        <?php $__currentLoopData = $offices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $office): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <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>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                  <div class="col-lg-12">
                      <?php echo e($offices->links()); ?> &nbsp
<?php $__env->stopSection(); ?>
"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 sign in or create an account to participate in this conversation.