I had a lot of my logic in my views. I refactored this into methods in one of my models. They're being recognized, but not actually working as the IF doesn't change, as the database changes, to reflect a different method that should be called.
For example, my code was originally like this, with all the logic in the views.
@if($tenancy == null || $tenancy->accepted == 0 && $tenancy->request_sent != 1)
<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 the option to accept and reject.
This updates the values in DB.
-->
@if($tenancy != null && $tenancy->accepted == 0 && $tenancy->request_sent == 1)
<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 != null && $tenancy->accepted == 1 && $tenancy->request_sent == 0)
<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-->
I then refactored it to this, using method names instead of logc. The methods are defined in my Tenancy controller.
@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
<!--
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 the 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->inTenancy())
<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-->
This is the controller which renders the view above
public function index($id){
$user = User::where('id', $id)->first();
$users = Auth::user();
$Tenancy = new Tenancy;
$tenancy = DB::table('tenancy')->first();
return view('/pages/account/index', compact('user', 'users', 'Tenancy', 'tenancy'));
}
This is the Tenancy model where the methods are defined.
public function addTenancy()
{
return $this->accepted == 0 && $this->request_sent == 0;
}
public function hasRequestPending()
{
return $this->accepted == 0 && $this->request_sent == 1;
}
public function inTenancy()
{
return $this->accepted == 1 && $this->request_sent == 0;
}
I can't see why the newly update view shouldn't be moving through the IF statement as the database changes. Any ideas?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire