如何正确地从多态关系中检索数据?

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

您好,我在 Laravel 11 应用程序中从多态关系检索数据时遇到问题。我正在尝试检索与单个帖子相关的所有报告。

它可以像这样与数据库门面一起使用

 $reports = DB::table('reports')
    ->where('reportable_id', $this->post->id)
    ->where('reportable_type', 'Post')
    ->get();

它给了我 2 份报告,这些报告与当前的一篇文章相关,并且根据数据库,这是正确的。

但是对于关系,它不会检索任何内容。

这是Report模型中的关系

  <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;


class Report extends Model
{
    public $guarded = ['id'];


    public function reportable(): MorphTo
    {
        return $this->morphTo();
    }

这是我的帖子模型

<?php

namespace App\Models;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Post extends Model
{
    use HasFactory;
    public $guarded = ['id'];

    protected $casts = [
        'archived_at' => 'datetime',
    ];


    public function reports(): MorphMany
    {
        return $this->morphMany(Report::class, 'reportable');
    }

这就是我使用关系检索数据的方式,它显示了空集合。

$post = $this->post->reports;

这是报告迁移

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('reports', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->unsignedBigInteger('reportable_id')->nullable();
            $table->string('reportable_type')->nullable();
            $table->text('description')->nullable();
            $table->boolean('spam')->nullable()->default(null);
            $table->boolean('victimize')->nullable()->default(null);
            $table->boolean('offensive')->nullable()->default(null);
            $table->boolean('solved')->default(false);
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        });
    }

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

这是迁移后

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
            ->constrained('users')
            ->cascadeOnDelete();
            $table->foreignId('post_category_id')
            ->constrained('post_categories')
            ->cascadeOnDelete();
            $table->string('title');
            $table->text('body');
            $table->date('archived_at')->nullable();
            $table->timestamps();
        });
    }

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

只是让您知道,我通过“喜欢”和“评论”获得了相同的逻辑,并且它运行良好,并且我能够通过关系检索数据。但据报道我不是。 :/也许这是我没有看到的东西。

php laravel database relation
1个回答
0
投票

您分享的代码没问题。问题似乎出在您创建报告时:“reportable_type”应该是完全限定的类名,或者在本例中为“App\Models\Post”,而不仅仅是像数据库示例中那样的Post

所以,这应该不起作用:

DB::table('reports') ->where('reportable_type', 'Post') ...
    
© www.soinside.com 2019 - 2024. All rights reserved.