samedi 24 mars 2018

Adding friends system. It works, but I need to add another column now

I have implemented a friends system in my app. But I'm using it as a relationship between tenants and landlords.

The relationship works, but I need to add another attirbute to the table, and I'm not sure how. I'm attempting to add a property address or add when the request is sent. Along with user_id, tenancy_id, and obviously the accepted column in false.

User Model where all the functions/relationships for tenancy are definded.

//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();
}

The Account controller. The functions for add, accepte, and the create form view.

  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");
  }

  //Create a Tenancy.
  //Loads create form
  public function tenancyIndex($id){
    $user = User::where('id', $id)->first();
    $properties = PropertyAdvert::where('user_id', Auth::id())->get();

    return view('/pages/account/tenancy/create', compact('user', 'properties'));
  }

Create Form view This is what load after a user clicks add on the tenancy page. There directed to this form.

<form action="/account//add" method="POST">
              
              <div class="row">
                <div class="col-md-6">
                  <label for="property_address">Property Address</label>
                </div> <!-- ./col6 -->
              </div> <!-- ./ row-6 -->
              <div class="row">
                <div class="col-md-10">
                  <select class="form-control" id="property_address" name="property_address">
                    <!--Gets all counties from DB -->
                    @foreach ($properties as $property)
                      <option value=></option>
                    @endforeach
                  </select>
                </div> <!-- ./ col-6-->
              </div> <!-- ./ row-5  -->
              <div class="row mt-2">
                <div class="col-md-6">
                  <label for="landlord-name">Landlord Name</label>
                </div> <!-- ./col=6 -->
              </div> <!-- ./ row-4-->
              <div class="row">
                <div class="col-md-6">
                  <select class="form-control" name="landlord-name">
                    <option value=""></option>
                  </select>
                </div> <!-- ./ row 3-->
              </div> <!-- ./col-3 -->
              <div class="row mt-2">
                <div class="col-md-6">
                  <label for="tenand-name">Tenant Name</label>
                </div> <!-- ./col=6 -->
              </div> <!-- ./ row-4-->
              <div class="row">
                <div class="col-md-6">
                  <select class="form-control" name="tenant-name">
                    <option value=""></option>
                  </select>
                </div> <!-- ./ row 3-->
              </div> <!-- ./col-3 -->
              <button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
            </form> <!-- ./form -->

The Routes

Route::post('/account/{id}/add', 'AccountController@getAdd')->middleware('auth');
Route::get('/account/{id}/accept', 'AccountController@getAccept')->middleware('auth');

Route::get('account/tenancy/create/{id}', 'AccountController@tenancyIndex')->middleware('auth');

This is the table. It works as is. But I need to add in the property address, or property id. Table



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire