dimanche 29 avril 2018

Call to a member function method-name on null

I'm calling a method but i'm getting the error

Call to a member function addTenancy() on null

Now, it is null, that is fine. But I need the method to be accessed, as it provides an add button, which is then populate the database.

The method is defined in my Tenancy Model

 public function addTenancy()
    {
        return $this->accepted == 0 && $this->request_sent == 0;
    }

This is the index view.

@if(Auth::user()->id == $tenancy->tenant_id)
            <h1>You cannot add yourself</h1>
          @elseif($Tenancy->addTenancy())
            <a href="/account/tenancy//create" class="btn btn-primary">Start Tenancy</a>
          @endif

This is the controller which renders the view, and passes the $Tenancy variable.

public function index($id){

   $user = User::where('id', $id)->first();

   $Tenancy = Tenancy::first();
   $tenancy =  DB::table('tenancy')->first();

  return view('/pages/account/index', compact(''user', 'Tenancy', 'tenancy'));
}

How do I access the button, even if the DB is null



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire