Здравствуйте. Я пытаюсь создать систему друзей, похожую на Facebook, где вы можете добавить другого пользователя в список друзей, однако, как только я нажимаю кнопку «Добавить друга», появляется ошибка. Спасибо за любую помощь, спасибо.
SQLSTATE [HY000]: общая ошибка: 1364 Поле «принято» не имеет
значение по умолчанию (SQL: вставить в friends
(friend_id
, user_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
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
@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');
}
}