如何在 Laravel 博客应用程序中获取用户的书签帖子?
How to get User's bookmarked Posts in Laravel Blog App?
我目前正在使用 laravel 开发 BlogApp。我的问题是如何让用户为帖子添加书签?我对 Eloquent 模型 Relationship.Please 指导我感到困惑。谢谢。
这里是 UsersTable
迁移架构:
<?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');
}
}
这里是 PostsTable
迁移模式:
<?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');
}
}
这里是 BookmarksTable
迁移架构:
<?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');
}
}
这里是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();
}
}
这里是 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);
}
}
这是Bookmark
型号:
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
protected $table='bookmarks';
}
我还在 PostsController
中创建了 2 个用于添加和删除书签的函数
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();
}
并且还创建了在 UserController
中获取用户书签帖子的方法
public function get_bookmarks(){
$bookmarks=auth()->user()->bookmarks();
return response()->json($bookmarks);
}
将关系更改为 belongsToMany()
:
public function bookmarks()
{
return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id')->where('is_active', 1);
}
然后使用它:
$user->bookmarks
因为你在你需要的实际数据之间使用 table 作为 link 那么这将是 Pivot table 关系的绝佳选择 https://laracasts.com/series/laravel-from-scratch-2017/episodes/30
它知道书签只包含用户和帖子之间的 links
return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id');
第二个参数 "bookmarks" 是用作基准 table 的 table。
In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
我目前正在使用 laravel 开发 BlogApp。我的问题是如何让用户为帖子添加书签?我对 Eloquent 模型 Relationship.Please 指导我感到困惑。谢谢。
这里是 UsersTable
迁移架构:
<?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');
}
}
这里是 PostsTable
迁移模式:
<?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');
}
}
这里是 BookmarksTable
迁移架构:
<?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');
}
}
这里是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();
}
}
这里是 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);
}
}
这是Bookmark
型号:
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
protected $table='bookmarks';
}
我还在 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();
}
并且还创建了在 UserController
public function get_bookmarks(){
$bookmarks=auth()->user()->bookmarks();
return response()->json($bookmarks);
}
将关系更改为 belongsToMany()
:
public function bookmarks()
{
return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id')->where('is_active', 1);
}
然后使用它:
$user->bookmarks
因为你在你需要的实际数据之间使用 table 作为 link 那么这将是 Pivot table 关系的绝佳选择 https://laracasts.com/series/laravel-from-scratch-2017/episodes/30
它知道书签只包含用户和帖子之间的 links
return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id');
第二个参数 "bookmarks" 是用作基准 table 的 table。
In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to: