I'm currently working on BlogApp using laravel. My Question is how to get users bookmarked posts ? Im confused about Eloquent Model Relationship.Please Guide Me. Thank You.
Here is UsersTable
Migration Schema :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique()->nullable();
$table->string('password');
$table->string('image')->nullable()->default(null);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
Here is PostsTable
Migration Schema :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->text('title');
$table->text('slug');
$table->text('coverimage')->nullable()->default(null);
$table->text('body');
$table->integer('user_id')->nullable();
$table->integer('category_id')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
Here is BookmarksTable
Migration Schema :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserPostsBookmarks extends Migration
{
public function up()
{
Schema::create('bookmarks', function (Blueprint $table) {
$table->integer('user_id')->unsigned()->index();
$table->integer('post_id')->unsigned()->index();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('bookmarks');
}
}
Here is UserModel
<?php
namespace App\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Authenticatable
{
use Notifiable;
protected $table='users';
public $primaryKey='id';
protected $fillable = [
'name', 'email', 'password','provider', 'provider_id'
];
protected $hidden = [
'password', 'remember_token',
];
public function bookmarks(){
return $this->hasMany('App\Model\Post','bookmarks','user_id','post_id')->where('is_active', 1)->with('user','category')->get();
}
}
Here is PostModel
:
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Model\Bookmark;
use App\Model\User;
class Post extends Model
{
protected $table='posts';
public $primaryKey='id';
public function user(){
return $this->belongsTo('App\Model\User','user_id');
}
public function category()
{
return $this->belongsTo('App\Model\Category','category_id');
}
public function bookmarks()
{
return $this->belongsToMany('App\Model\User', 'bookmarks','post_id','user_id');
}
public function is_bookmarked(User $user){
return $this->bookmarks->contains($user);
}
}
Here is Bookmark
Model :
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
protected $table='bookmarks';
}
I have also created 2 functions for adding and removing bookmarks in PostsController
public function add_bookmark(Post $post)
{
Auth::user()->bookmarks()->attach($post->id);
return back();
}
public function remove_bookmark(Post $post)
{
Auth::user()->bookmarks()->detach($post->id);
return back();
}
and also created method for get users bookmarked posts in UserController
public function get_bookmarks(){
$bookmarks=auth()->user()->bookmarks();
return response()->json($bookmarks);
}
Change the relationship to belongsToMany()
:
public function bookmarks()
{
return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id')->where('is_active', 1);
}
Then use it:
$user->bookmarks