关联'广告'使用“用户与用户”关系'

时间:2018-03-24 13:00:36

标签: laravel laravel-5 eloquent blade laravel-5.5

我正在构建一个属性应用。我目前有能力允许用户互相开始租赁。将此视为添加为朋友的能力。我的下一个挑战是将特定房产/广告与租赁相关联。 addastenant目前通过导航到另一个用户配置文件来完成。我应该在属性广告显示页面上具有此功能,例如property / 1。其中1是财产ID。发送请求时,它的user_id,tenancy_id,property_id?这会有用。

这是目前的表格。如果我在这里也添加了属性ID,它会起作用吗。

enter image description here

理想的出行方式是什么?我将在下面提供"添加为租户代码"

带方法的用户模型

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

租赁添加方法当前是用户的配置文件页面。或许我可以将它移到属性页面。

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>{{$users->name}}</span>
            <span class="text-muted">{{$users->userType}}</span>
          </p>
        @endforeach
      @endif
    </div>
    <div class="col-md-6">
      @if (Auth::user()->hasTenancyRequestsPending($user))
        <p class="text-lead">Waiting for {{$user->name }} to accept your request.</p>
        @elseif (Auth::user()->hasTenancyRequestsReceived($user))
          <a href="/account/{{$user->id}}/accept" class="btn btn-primary">Accept Friend Request</a>
        @elseif(Auth::user()->isInTenancyWith($user))
          <p class="text-sub">You and {{$user->name}} are friends</p>
        @elseif(Auth::user()==$user)
          @else
            <a href="/account/{{$user->id}}/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>{{$Request->name}}</span>
            <span class="text-muted">{{$Request->userType}}</span>
          @endforeach
      @endif
    </div>
  </div>

实施添加和接受的控制器

  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/{{$user->id}}')->with('status', "Friend request already pending");
      }

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

    //After passing all checks. Add other account
    Auth::user()->addTenancy($user);

    return redirect('/account/{{$user->id}}')->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/{{$user->id}}')->with('status', "Request request accepted");


  }

0 个答案:

没有答案