samedi 24 mars 2018

Field doesn't have default value. Adding "friend"

I'm trying to add a user as a tenant. I implemented this using a friend system tutorial and it worked fine until I tried to modify it. I want to add a property address when the tenancy request is sent. The error I'm getting is that there is no default value.

SQLSTATE[HY000]: General error: 1364 Field 'property_address' doesn't have a default value (SQL: insert into tenancies (tenancy_id, user_id) values (, 4))

All methods in model

//The users tenancies
    public function tenanciesOfMine(){
      return $this->belongsToMany('App\User', 'tenancies', 'user_id', 'tenancy_id', 'property_address');
    }

    //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', 'property_address');
    }

    //If a tenancy is accepted, create the tenancy ie friendship.
    public function tenancies(){
      return $this->tenanciesOfMine()
        ->wherePivot('accepted', '=', true)
        ->wherePivot('property_address', '=', '1')->get()
          ->merge($this->tenancyOf()
            ->wherePivot('accepted', '=', true)
            ->wherePivot('property_address', '=', '1')->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();
    }

This is the add method

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

This is the create form. The landlord and tenant are already represented by user_id, and tenancy id. So I just need property_Address in. THe other names are there for visuals

<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 -->



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire