I'm building a property app. I currently have the ability to allow users to start a tenancy with each other. Think of this as the ability to add as friends. My next challenge is to associate a specific property/advert with the tenancy. The addastenant is currently done by navigating to another users profile. Should I have this functionality on the property advert show page eg property/1. Where 1 is the property ID. When the request is sent it's user_id, tenancy_id, property_id? Would this work.
This is the table at the moment. Would it work if I added the property ID here also.
What is the ideal way to go about? I will provide the "Add as Tenant code below"
User model with methods
//The users tenancies
public function tenanciesOfMine(){
return $this->belongsToMany('App\User', 'tenancies', 'user_id', 'tenancy_id');
}
//Inverse of user tenncies -> Both users have tenancy if one exists. One user can't be in a tenancy with someone who is not in tenacy with them.
//Like friends on FB. You can't be friends with someone, without them being friends with you also.
public function tenancyOf(){
return $this->belongsToMany('App\User', 'tenancies', 'tenancy_id', 'user_id');
}
//If a tenancy is accepted, create the tenancy ie friendship.
public function tenancies(){
return $this->tenanciesOfMine()->wherePivot('accepted', true)->get()->
merge($this->tenancyOf()->wherePivot('accepted', true)->get());
}
//Request hasn't yet been accepted. Display list of pending requests
public function tenacyRequests(){
return $this->tenanciesOfMine()->wherePivot('accepted', false)->get();
}
//Inverse of Tenancy Requests
public function tenancyRequestsPending(){
return $this->tenancyOf()->where('accepted', false)->get();
}
//If a user has a request pending from another user
public function hasTenancyRequestsPending(User $user){
return (bool) $this->tenancyRequestsPending()->where('id', $user->id)->count();
}
public function hasTenancyRequestsReceived(User $user){
return (bool) $this->tenacyRequests()->where('id', $user->id)->count();
}
//Add tenancy
public function addTenancy(User $user){
$this->tenancyOf()->attach($user->id);
}
//Add tenancy
public function acceptTenancyRequest(User $user){
$this->tenacyRequests()->where('id', $user->id)->first()->pivot->update([
'accepted' => true,
]);
}
public function isInTenancyWith(User $user){
return $this->tenancies()->where('id', $user->id)->count();
}
Profile page where the tenancy add method is currently user. Prehaps I could move this to the property page.
div class="row">
<div class="col-md-6">
<p class="text-lead">Your Landlords</p>
@if(!$user->tenancies()->count())
<p class="text-sub">You has no tenancies!</p>
@else
@foreach ($user->tenancies() as $users)
<p>
<span></span>
<span class="text-muted"></span>
</p>
@endforeach
@endif
</div>
<div class="col-md-6">
@if (Auth::user()->hasTenancyRequestsPending($user))
<p class="text-lead">Waiting for to accept your request.</p>
@elseif (Auth::user()->hasTenancyRequestsReceived($user))
<a href="/account//accept" class="btn btn-primary">Accept Friend Request</a>
@elseif(Auth::user()->isInTenancyWith($user))
<p class="text-sub">You and are friends</p>
@elseif(Auth::user()==$user)
@else
<a href="/account//add" class="btn btn-primary">Start Tenancy</a>
@endif
<p class="text-lead">Requests from Landlords</p>
@if(!$tenancyRequests->count())
<p class="text-sub">You has no Tenancy Requests!</p>
@else
@foreach ($tenancyRequests as $Request)
<span></span>
<span class="text-muted"></span>
@endforeach
@endif
</div>
</div>
Controller where add and accepted are implemented
public function getAdd($id){
$user = User::where('id', $id)->first();
if(!$user){
return redirect('/')->with(['status', 'Profile Not Found']);
}
if(Auth::user()->id === $user->id){
return redirect()->route('home');
}
if (Auth::user()->hasTenancyRequestsPending($user) ||
$user->hasTenancyRequestsPending(Auth::user())){
return redirect('/account/')->with('status', "Friend request already pending");
}
if(Auth::user()->isInTenancyWith($user)){
return redirect('/account/')->with('status', "Already i a tenancy");
}
//After passing all checks. Add other account
Auth::user()->addTenancy($user);
return redirect('/account/')->with('status', "Request Sent");
}
public function getAccept($id){
$user = User::where('id', $id)->first();
if(!$user){
return redirect('/')->with(['status', 'Profile Not Found']);
}
if(!Auth::user()->hasTenancyRequestsReceived($user)){
return redirect('/');
}
Auth::user()->acceptTenancyRequest($user);
return redirect('/account/')->with('status', "Request request accepted");
}
via Chebli Mohamed

Aucun commentaire:
Enregistrer un commentaire