samedi 28 avril 2018

Method in model doesn't seem to be being recognised in view

I was tidying up my code. Instead of having my logic all in the views, I broke it off into functions in the Tenancy model, and called them in the view.

For example

This was my original line in index.blade.php

@if($Tenancy->accepted == 0 && $Tenancy->request_sent != 1)

I changed it to this

@if($Tenancy->addTenancy()

And moved the logic to my Tenancy model like so

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

This works for adding a user.

But for detecting requests, and detecting friends it is not working. The IF is't progressing past the first if.else block

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

          <!-- 
            If the user signed in, isn't the owner of this profile.
            Do not show these buttons that control accept/reject/end
          -->

        @if(Auth::user()->id == $user->id)
          <!-- 
            If the request has been sent, but hasn't been accepted.
            Give option to accept and reject.
            This updates the values in DB.
          -->
          @if($Tenancy->hasRequestPending())
            <form method="POST" action="/account/tenancy//accept">
              
              <input type="submit" class="btn btn-primary" value="Accept Request">
            </form>
            <form method="POST" action="/account/tenancy//reject">
              
              <input type="submit" class="btn btn-warning" value="Reject Request">
            </form>
              <!-- 
                If the request has been accepted.
                Show button to end the tenancy,
                and property details
              -->
          @elseif($Tenancy->isTenancy())
            <form method="POST" action="/account/tenancy//end">
              
              <input type="submit" class="btn btn-primary" value="End Tenancy">
            </form>
            <h5>Currently in Tenancy with </h5>
            <h5>Your property is </h5>
          @endif <!-- End of current user vs this user-->
        @endif <!-- Initial If-->

Correlating functions in Tenancy Model

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

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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KlmuI9
via IFTTT

Aucun commentaire:

Enregistrer un commentaire