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

问题描述 投票:1回答:2

我目前正在使用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';   
}

我还创建了两个用于在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);
    }
php laravel blogs
2个回答
1
投票

将关系更改为belongsToMany()

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

然后使用它:

$user->bookmarks

0
投票

因为您在需要的实际数据之间使用表作为链接,那么这将是Pivot表关系https://laracasts.com/series/laravel-from-scratch-2017/episodes/30的绝佳选择

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.