我试图将用户添加为租户。我使用朋友系统教程实现了它,它工作正常,直到我尝试修改它。我想在发送租赁请求时添加属性地址。我得到的错误是没有默认值。
SQLSTATE [HY000]:常规错误:1364字段' property_address'没有默认值(SQL:插入
tenancies
(tenancy_id
,user_id
)值(,4))
模型中的所有方法
//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();
}
这是添加方法
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");
}
这是创建表单。房东和租户已经由user_id和租赁ID表示。所以我只需要在其中使用property_Address。其他名称用于视觉效果
<form action="/account/{{$user->id}}/add" method="POST">
{{ csrf_field() }}
<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={{$property->address . ', ' . $property->town . ', ' . $property->county}}>{{$property->address . ', ' . $property->town . ', ' . $property->county}}</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="{{Auth::user()->name}}">{{Auth::user()->name}}</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="{{$user->name}}">{{$user->name}}</option>
</select>
</div> <!-- ./ row 3-->
</div> <!-- ./col-3 -->
<button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
</form> <!-- ./form -->
答案 0 :(得分:1)
纯粹是因为你得到的SQL错误,也许你需要确保property_address
在你的数据库中可以为空? (即删除not null
约束(如果有约束)。