dimanche 25 mars 2018

How would I go about adding a property address column to my tenancy table (friends table)

Ok.

I'm building a prroperty app. I have a friends system implemeneted in the app. I'm using this to allow a landlord to add a tenant and this works as a connection. My question is how would I assoicated one of the landlords poperties with the request when it is send.

At the moment, when a request is sent, the following is posted to the database. User_id (Landlord), Tenancy_id (Tenant), Accepted (false). I would like to post a property address with this also, but I'm stuck on how to do that.

I have a form, where I have the landlords name, and tenants name, but also a list of the landlords property. I have no idea how to send this to the database. As it's not a normal store method. I will post the code.

The add method in the ACCOUNT CONTROLLER

  public function getAdd($id){
    $user = User::where('id', $id)->first();

    //If the user can be found
    if(!$user){
      return redirect('/')->with(['status', 'Profile Not Found']);
    }

    if(Auth::user()->id === $user->id){
        return redirect()->route('home');
    }

    //If current sign in user has request pending with the users
    //If the uer has a request pending with auth user
    if (Auth::user()->hasTenancyRequestsPending($user) ||
        $user->hasTenancyRequestsPending(Auth::user())){
          return redirect('/account/')->with('status', "Friend request already pending");
      }

    //Check if tenancy already exists
    if(Auth::user()->isInTenancyWith($user)){
      return redirect('/account/')->with('status', "Already i a tenancy");
    }

    //After passing all checks. Add other account
    //Landord is adding the tenant
    Auth::user()->addTenancy($user);

    return redirect('/account/')->with('status', "Request Sent");
  }

The create FORM The properties variable is loaded from a controller which holds all properties owned by a landlord.

<form action="/account//add" method="POST">
              
                  <select class="form-control" id="property_address" name="property_address">
                    <!--Gets all counties from DB -->
                    @foreach ($properties as $property)
                      <option value=></option>
                    @endforeach
                  </select>

                  <label for="landlord-name">Landlord Name</label>

                  <select class="form-control" name="landlord-name">
                    <option value=""></option>
                  </select>

                  <label for="tenand-name">Tenant Name</label>

                  <select class="form-control" name="tenant-name">
                    <option value=""></option>
                  </select>

              <button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
            </form> <!-- ./form -->

THE User model which has the methods to add friends

//Tennancies of this uers. User model, tenancy table.
public function tenanciesOfMine(){
  return $this->belongsToMany('App\User', 'tenancies', 'user_id', 'tenancy_id');
}

//Users who have this user as a friend
//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(){
  //Getting friends of this user. Where accepted is true
  return $this->tenanciesOfMine()->wherePivot('accepted', true)->get()->
  //merge the inverse. Tennancy created with both users
    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,
  ]);
}

Here are the migrations for Tenancies (Which holds the landlord and Tenant) relationship.

  Schema::create('tenancies', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('tenancy_id');
            $table->string('property_address');
            $table->boolean('accepted')->default('0');
        });

And for property

Schema::create('property_adverts', function (Blueprint $table) {
            $table->increments('id');
            $table->string("photo");
            $table->string('address');
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire