I have a basic adding system, that allows landlords to add tenants, and tenants then have the ability to accept/reject. Accepting is very simple, when the tenant clicks accept, the accept boolean changes to 1. At the moment this is changeing all rows to accepted, not just the one between the landlord and tenant.
This is an example row in the database:
If a tenant clicks accept, the accepted row will change to 1, and request sent will revert to 0. The users are now connected.
However, all rows that have accepted = 0
, and request = 1
, will be affected, not just the current row.
This is the accepted controller
public function accept(Request $request)
{
Tenancy::where('accepted', 0)
->where('request_sent', 1)
->where('tenant_id', Auth::id())
->update([
'accepted' => 1,
'request_sent' => 0,
]);
return back();
}
Any ideas?
EDIT
Route URL
Route::post('/account/tenancy/{id}/accept', 'AccountController@accept')->middleware('auth');
Entire form and logic for accept/reject
@if($currentUser->userType == "Tenant")
@if($tenancy == null ||$tenancy->accepted == 0 && $tenancy->request_sent == 1 && $tenancy->tenant_id == $currentUser->id)
<form method="POST" action="/account/tenancy/{{$user->id}}/accept">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="Accept Request">
</form>
<form method="POST" action="/account/tenancy/{{$user->id}}/reject">
{{ csrf_field() }}
<input type="submit" class="btn btn-warning" value="Reject Request">
</form>
@endif
Based on your comments/recent edits, i believe this should work:
Change all
action="/account/tenancy/{{$user->id}}/accept"
to
action="/account/tenancy/{{$tenancy->id}}/accept"
and your
accept/reject
methods in the controller like this:You are missing another where. You are just filtering for the two statuses and the tenant_id, but you want to filter by the landlord_id aswell.
Something like
Tenancy::where('accepted', 0)->where('request_sent', 1)->where('tenant_id', Auth::id())->where('landlord_id', $id);
.However, what happens, if there are two tenancies between a landlord and a tenant? You probably want to pass the tenancy id in the request and retrieve the tenancy in your accept function by the id. Important thing to keep in mind here is to make sure, that the current user is allowed to accept the tenancy with the id he passed.
Edit based on your new code in the question:
This would be the approach of my recommendation, not the additional landlord_id.
You should still keep in mind, that an authorization/check would be needed.