W jaki sposób zrealizować system przyjaciół by obydwie strony zawierały znajomość? Poniższy przykład działa dla 1 osoby a nie 2.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserFriendTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('friend_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('friend_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('friend_id')->references('id')->on('users');
$table->primary(['user_id', 'friend_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('friend_user');
}
}
//User Model
public function friends()
{
return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
}
//User Model
public function addFriend(User $user)
{
$this->friends()->attach($user->id);
}
public function removeFriend(User $user)
{
$this->friends()->detach($user->id);
}
Ktoś coś?