访问有关多态关系的附加列

Posted

技术标签:

【中文标题】访问有关多态关系的附加列【英文标题】:Accessing additional columns on polymorphic relationship 【发布时间】:2021-05-19 13:53:55 【问题描述】:

如果重要的话,我会使用 Lumen 和 eloquent。

你好,我是多态关系的新手——所以我希望我在这里错过了一些明显的东西。这种关系似乎工作正常,但是我需要返回一些额外的数据,但我不知道该怎么做。值得一提的是,我的用户 ID 故意是一个字符串 :)。

我有用户、想法和评论。

在我的想法模型中,我添加了这样的 cmets:

 protected $with = [
    'user:id,fname,lname',
    'comments'
];

这将返回以下内容:


    "id": 1,
    "title": "Mike Idea222",
    "body": "asasdasd",
    "status": "submitted",
    "user_id": "1a",
    "total_votes": 0,
    "created_at": "2021-02-16T03:36:19.000000Z",
    "updated_at": "2021-02-16T03:36:19.000000Z",
    "user": 
        "id": "1a",
        "fname": "Michael",
        "lname": "Poopy"
    ,
    "comments": [
        
            "id": 1,
            "commentable_id": 1,
            "commentable_type": "App\\Idea",
            "user_id": "1a",
            "comment": "idea It is a comment",
            "created_at": "2021-02-16T03:36:32.000000Z",
            "updated_at": "2021-02-16T03:36:32.000000Z"
        
    ]

我想要的是除了 user_id 之外,还让用户 fname / lname 显示评论。

也许我错过了一些东西 - 请参阅下面的代码:

User.php

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;

class User extends Model implements AuthenticatableContract, AuthorizableContract

    use Authenticatable, Authorizable;

    protected $primaryKey = 'id';
    protected $keyType    = 'string';
    public $incrementing  = false;

    public function getRouteKeyName()
    
        return 'id';
    

    protected $with = ['ideas'];

    protected $fillable = ['id', 'fname', 'lname', 'role'];

    public function ideas()
    
        return $this->belongsTo('App\Idea', 'id', 'user_id')
            ->select('user_id', 'id', 'title')
            ->without(['user'])
            ->without(['votes']);
    

Schema::create('users', function (Blueprint $table) 
    $table->string('id')->unique()->primary(); // Unique UDC from CAS / banner
    $table->string('fname');
    $table->string('lname');
    $table->json('role'); // User Role
    $table->timestamps(0); // Adds nullable created_at and updated_at
);

Idea.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class Idea extends Model

    use Commentable;

    protected $with = [
        'user:id,fname,lname',
        'comments'
    ];

    protected $fillable = [
        'user_id',
        'title',
        'body',
        'status',
    ];

    public function user()
    
        return $this->belongsTo('App\User', 'user_id')
            ->without('ideas');
    

Schema::create('ideas', function (Blueprint $table) 
    $table->increments('id');
    $table->text('title');
    $table->longText('body');
    $table->text('status');
    $table->string('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
    $table->integer('total_votes')->default(0);
    $table->timestamps();
);

Comment.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model

    protected $fillable = [
        'comment',
    ];

    protected $dates = [
        'created_at',
        'deleted_at',
        'updated_at',
    ];

    public function commentable()
    
        return $this->morphTo();
    

Commentable.php*

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;

trait Commentable

    public function comments()
    
        return $this->morphMany(Comment::class, 'commentable');
    

    public function comment($user_id, $commentText)
    
        $comment = new Comment();
        $comment->user_id = $user_id;
        $comment->comment = $commentText;

        $this->comments()->save($comment);
        return true;
    


Schema::create('comments', function (Blueprint $table) 
    $table->bigincrements('id');
    $table->biginteger('commentable_id')->unsigned()->index();
    $table->string('commentable_type');
    $table->string('user_id')->index();
    $table->longtext('comment');
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
);

【问题讨论】:

【参考方案1】:

通过将其添加到 Comment.php 来修复它

    public function user()
    
        return $this->belongsTo('App\User', 'user_id', 'id')
            ->select(['id', 'fname', 'lname'])
            ->without('ideas')
    

并更改 Commentable.php 中的变形

    public function comments()
    
        return $this->morphMany(Comment::class, 'commentable')->with('user');
    

【讨论】:

以上是关于访问有关多态关系的附加列的主要内容,如果未能解决你的问题,请参考以下文章

如何使用关系和一些逻辑附加列 db 结果

Ember 2 简单的多态关系

"引用"与多态的关系笔试经验

EF Core 2.1:具有一对多关系的自引用实体生成附加列

(Laravel)通过数据透视表的多态关系

多维数据集设计 - 带有附加列的多对多映射的桥接表