简体   繁体   English

SQLSTATE[HY000]:一般错误:1364 字段“已接受”没有默认值

[英]SQLSTATE[HY000]: General error: 1364 Field 'accepted' doesn't have a default value

Hello I am attempting to create a friend system somewhat like the Facebook where you can add another user as a friend however, once I click the button add friend it gives me this error.您好,我正在尝试创建一个类似于 Facebook 的朋友系统,您可以在其中将另一个用户添加为朋友,但是,一旦我单击添加朋友按钮,就会出现此错误。 Any help will be very much appreciated thank you.任何帮助将不胜感激,谢谢。

SQLSTATE[HY000]: General error: 1364 Field 'accepted' doesn't have a default value (SQL: insert into friends ( friend_id , user_id ) values (1, 3)) SQLSTATE[HY000]:一般错误:1364 字段“接受”没有默认值(SQL:插入friendsfriend_iduser_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();
      }
}

FriendController.php朋友控制器.php

<?php

namespace Kermode\Http\Controllers;

use Auth;
use Kermode\User;
use Illuminate\Http\Request;

class FriendController extends Controller
{
  public function getIndex()
  {
    $friends = Auth::user()->friends();
    $requests = Auth::user()->friendRequests();

    return view('friends.index')
      ->with('friends', $friends)
      ->with('requests', $requests);
  }

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

    if (!$user) {
      return redirect()
        ->route('home')
        ->with('info', 'That user could not be found');
    }

    if (Auth::user()->hasFriendRequestPending($user) || $user->
        hasFriendRequestPending(Auth::user())) {
          return redirect()
            ->route('profile.index', ['first_name' => $user->first_name])
            ->with('info', 'Friend request already pending.');

    }

    if (Auth::user()->isFriendsWith($user)) {
        return redirect()
        ->route('profile.index', ['first_name' => $user->firstname])
        ->with('info', 'You are already friends');

    }

    Auth::user()->addFriend($user);

    return redirect()
      ->route('profile.index', ['first_name' => $first_name])
      ->with('info', 'Friend request sent.');

  }
}

profile.index.blade.php profile.index.blade.php

@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

friendstable朋友表

<?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');
    }
}

你必须添加acceptable到$fillable

if you didnt add your column name into $fillable in your model, this error occurred如果您没有将列名添加到模型中的 $fillable 中,则会发生此错误

1364 Field 'accepted' doesn't have a default value 1364 字段 'accepted' 没有默认值

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['accepted'];
}

This error might be because of database.这个错误可能是因为数据库。 The accepted column needs to be nullable or have a default value like接受的列需要可以为空或具有默认值,例如

$table->boolean('accepted')->nullable();

or或者

$table->boolean('accepted')->default(false);

去把你的数据库数据类型中你接受的列更改为 tinyint 然后按照定义更改为 0 .. 当我发现这个时它会起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:常规错误:1364字段“照片”在laravel 5.5中没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5 SQLSTATE[HY000]:一般错误:1364 字段“contactId”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'contactId' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“性别”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ starting_balance”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'starting_balance' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ reply_text”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'reply_text' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'agent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“名称”没有默认值 laravel 5.5 - SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value laravel 5.5 SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ practice_name”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'practice_name' doesn't have a default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM