【问题标题】:Laravel relationship designLaravel 关系设计
【发布时间】:2019-07-18 07:03:30
【问题描述】:

我正在从事团队管理项目。 我在处理人际关系方面有问题。

我想在看板中显示用户,但也希望看起来美观且易于阅读。我已经设计了关系,但是当我看到关系的 JSON 输出时,它完全一团糟。

有没有办法以这种方式显示关系数据?

{
    "id": 1,
    "name: "Test",
    "members": [
        {
            ... user model here
            "name": "Peter",
            "email": "peter@peter.com",
        }
    ]
}

我的迁移:

板迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBoardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('boards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('boards');
    }
}

会员迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMembershipsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('memberships', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('board_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('memberships');
    }

}

编辑:

型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Board extends Model
{
    protected $fillable = ['name'];

    public function members() {
        return $this->hasMany(Membership::class)->with('user');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Membership extends Model
{
    protected $fillable = ['board_id', 'user_id'];
    protected $hidden = ['created_at', 'updated_at'];

    public function user() {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
}

【问题讨论】:

  • 告诉我们你在哪里建立了关系,又名belongsTohasOnehasMany等等。
  • 您的问题是什么?你说的是eager loading 有董事会的成员吗? Board::with('members')-&gt;get() ?

标签: php laravel eloquent relationship


【解决方案1】:

您可以尝试将模型中的关系定义为多对多,即:

class User
{    
    public function boards()
    {
        return $this->belongsToMany('App\Board', 'memberships', 'user_id', 'board_id');
    }
}

class Board
{    
    public function members()
    {
        return $this->belongsToMany('App\User', 'memberships', 'board_id', 'user_id');
    }
}

定义了关系后,如果没有附加字段,您就不需要成员模型,您可以编写如下代码:

User::find($id)->boards
Board::find($id)->members
Board::with('members')->find($id)

您还可以阅读 Many to Many Relationships 上的文档

【讨论】:

  • 这不是我想要的,我得到的 JSON 看起来像这样 pastebin.com/mrJ6BgZg
  • 那个JSON是App\Board::with('members')-&gt;get();的结果?
  • 不,$board-&gt;members; 的 JSON 结果
  • 请尝试App\Board::with('members')-&gt;get();App\Board::with('members')-&gt;find($id);
  • 恕我直言,它看起来像是你们关系的结果,而不是我发布的那些
猜你喜欢
  • 2016-04-07
  • 2017-07-20
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
相关资源
最近更新 更多