如何在Laravel Blog App中获取用户的书签帖子?

时间:2017-12-29 05:36:56

标签: php laravel blogs

我目前正在使用laravel开发BlogApp。我的问题是如何为用户添加书签帖子?我对Eloquent Model Relationship感到困惑。请指导我。谢谢。

以下是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';   
}

我还创建了2个用于在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);
    }

2 个答案:

答案 0 :(得分:1)

将关系更改为belongsToMany()

public function bookmarks()
{
    return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id')->where('is_active', 1);
}

然后使用它:

$user->bookmarks

答案 1 :(得分:0)

因为您在需要的实际数据之间使用了一个表作为链接,所以这对于数据透视表关系https://laracasts.com/series/laravel-from-scratch-2017/episodes/30

来说是一个很好的选择

它知道书签只保存用户和帖子之间的链接

return $this->belongsToMany('App\Model\Post', 'bookmarks', 'user_id', 'post_id');

第二个参数“bookmarks”是用作数据透视表的表。

  

除了自定义连接表的名称之外,您还可以通过将其他参数传递给belongsToMany方法来自定义表上键的列名。第三个参数是您定义关系的模型的外键名称,而第四个参数是您要加入的模型的外键名称: