Php SQLSTATE[HY000]:一般错误:1364字段';接受';不';没有默认值
您好,我正在尝试创建一个类似Facebook的好友系统,您可以在其中添加其他用户作为好友。但是,一旦我单击“添加好友”按钮,就会出现此错误。非常感谢您的帮助,谢谢 SQLSTATE[HY000]:一般错误:1364字段“已接受”没有 默认值(SQL:插入到Php SQLSTATE[HY000]:一般错误:1364字段';接受';不';没有默认值,php,sql,laravel,error-handling,facebook-friends,Php,Sql,Laravel,Error Handling,Facebook Friends,您好,我正在尝试创建一个类似Facebook的好友系统,您可以在其中添加其他用户作为好友。但是,一旦我单击“添加好友”按钮,就会出现此错误。非常感谢您的帮助,谢谢 SQLSTATE[HY000]:一般错误:1364字段“已接受”没有 默认值(SQL:插入到朋友(朋友id,用户id) 价值观(1,3)) User.php <?php namespace Kermode; use Illuminate\Foundation\Auth\User as Authenticatable; cl
朋友(朋友id
,用户id
)
价值观(1,3))
User.php
<?php
namespace Kermode;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name','last_name', 'email', 'password','gender',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function getName()
{
if ($this->first_name && $this->last_name) {
return "{$this->first_name} {$this->last_name}";
}
if ($this->first_name) {
return $this->first_name;
}
return null;
}
public function getNameOrUsername()
{
return $this->getName() ?: $this->username;
}
public function getFirstNameOrUsername()
{
return $this->first_name ?: $this->username;
}
public function getAvatarUrl()
{
return "https://www.gravatar.com/avatar/{{ md5($this->email) }}
?d=mm&s=40";
}
public function friendsOfMine()
{
return $this->belongsToMany('Kermode\User', 'friends', 'user_id',
'friend_id');
}
public function friendOf()
{
return $this->belongsToMany('Kermode\User' , 'friends', 'friend_id'
, 'user_id');
}
public function friends()
{
return $this->friendsOfMine()->wherePivot('accepted', true)->get()->
merge($this->friendOf()->wherePivot('accepted', true)->get());
}
public function friendRequests()
{
return $this->friendsOfMine()->wherePivot('accepted', false)->get();
}
public function friendRequestsPending()
{
return $this->friendOf()->wherePivot('accepted', false)->get();
}
public function hasFriendRequestPending(User $user)
{
return (bool) $this->friendRequestsPending()->where('id', $user->id)->
count();
}
public function hasFriendRequestRecieved(User $user)
{
return (bool) $this->friendRequests()->where('id', $user->id)->count();
}
public function addFriend(User $user)
{
$this->friendOf()->attach($user->id);
}
public function acceptFriendRequest(User $user)
{
$this->friendRequests()->where('id', $user->id)->first()->pivot->
update([
'accepted' => true,
]);
}
public function isFriendsWith(User $user)
{
return (bool) $this->friends()->where('id', $user->id)->count();
}
}
您必须添加已接受的
如果未将列名添加到模型中的$fillable中,则会发生此错误
1364字段“已接受”没有默认值
此错误可能是由于数据库错误。接受的列需要可以为Null或具有如下默认值
$table->boolean('accepted')->nullable();
或
转到并将数据库数据类型中的已接受列更改为tinyint
然后按定义更改为0。。当我弄明白这一点时,它会起作用,然后可能需要接受
,并且您不会将其传递到查询。如果您发布答案,请更精确。否则,请使用注释来进行简短的注释。朋友的迁移文件如何将其放置在桌面上只需将布尔字段更改为:-$table->tinyInteger('accepted')->默认值(0)代码>显示错误,因为接受字段没有默认值。在您提交表单时,没有任何内容传递给accepted,因此它需要一些默认值。确认我,如果它在你的情况下工作,我会写一个详细的答案。
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-leg-6">
<h3>Your friends</h3>
@if (!$friends->count())
<p>You have no friends</p>
@else
@foreach ($friends as $user)
@include('user/partials/userblock')
@endforeach
@endif
</div>
<div class="col-lg-6">
<h4>Friend Request</h4>
@if (!$requests->count())
<p>You have no friend requests.</p>
@else
@foreach ($requests as $user)
@include('user.partials.userblock')
@endforeach
@endif
</div>
</div>
@endsection
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFriendsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('friends', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('friend_id');
$table->boolean('accepted');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('friends');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['accepted'];
}
$table->boolean('accepted')->nullable();
$table->boolean('accepted')->default(false);