Laravel 多态关系在 Laravel Nova 中不起作用

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

我有以下模型和关系。

应用程序\订阅者.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Grosv\LaravelPasswordlessLogin\Traits\PasswordlessLogin;

class Subscriber extends Authenticatable
{
    use Notifiable, PasswordlessLogin;

    public function customFieldValues()
    {
        return $this->morphMany(CustomFieldValue::class, 'customFieldValueable');
    }
}

应用程序\CustomField.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Campaign;

class CustomField extends Model
{
    protected $fillable = [
        'name',
        'key',
        'type',
        'field_options'
    ];

    public function customFieldValues()
    {
        return $this->hasMany(CustomFieldValue::class);
    }
}

应用程序\CustomFieldValues.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CustomFieldValue extends Model
{
    use HasFactory;

    protected $fillable = ['custom_field_id', 'value', 'custom_field_valueable_id', 'custom_field_valueable_type'];

    public function customField()
    {
        return $this->belongsTo(CustomField::class);
    }

    public function customFieldValueable()
    {
        // If you are using custom column names, specify them here. Otherwise, ensure they match the migration.
        return $this->morphTo(null, 'custom_field_valueable_type', 'custom_field_valueable_id');
    }
}

应用\Campaign.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\CustomField;

class Campaign extends Model
{
    protected $dates = [
        'imported_at'
    ];

    protected $casts = [
        'imported_at' => 'datetime',
        'exported_at' => 'datetime'
    ];

    protected $fillable = [
        'uuid',
        'name',
        'slug',
        'client_id'
    ];
    
    public function customFieldValues()
    {
        return $this->morphMany(CustomFieldValue::class, 'customFieldValueable');
    }
   }

应用\Newsletter.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Newsletter extends Model
{

    protected $fillable = [
        'uuid',
        'name',
        'brand_id',
        'active',
        'create_send_list_id',
        'last_imported'
    ];

    public function brand()
    {
        return $this->belongsTo(Brand::class);
    }

    public function subscribers()
    {
        return $this
            ->belongsToMany(Subscriber::class)
            ->withPivot('active')
            ->withTimestamps();
    }

    public function customFieldValues()
    {
        return $this->morphMany(CustomFieldValue::class, 'customFieldValueable');
    }
}

我创建表来支持这一点的迁移看起来像这样,

<?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('custom_field_values', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('custom_field_id');
            $table->string('value');
            // Manually create the polymorphic columns
            $table->unsignedBigInteger('custom_field_valueable_id');
            $table->string('custom_field_valueable_type');
            // Manually specify a shorter index name
            $table->index(['custom_field_valueable_type', 'custom_field_valueable_id'], 'custom_field_valueable_index');
            $table->timestamps();

            $table->foreign('custom_field_id')->references('id')->on('custom_fields')->onDelete('cascade');
        });


    }

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

但是,在 Nova 界面中,当我尝试查看订阅者时,出现以下错误,

SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“custom_field_values.customFieldValueable_type”(连接:mysql,SQL:从

custom_field_values
中选择*,其中
custom_field_values
customFieldValueable_type
= App\Models\Subscriber且
custom_field_values
.
customFieldValueable_id
= 1016 且
custom_field_values
.
customFieldValueable_id
不为空,按
custom_field_values
.
id
desc limit 6 offset 0)

如果有人可以向我解释我哪里出了问题,那就太好了。我对多态关系非常陌生。

php laravel eloquent laravel-10 laravel-nova
1个回答
0
投票

laravel 查询正在寻找列 customFieldValueable_type 但您的数据库中有一个 custom_field_valueable_type 。

如果您重新使用标准命名约定,它应该可以正常工作。

错误似乎来自于函数中的

App\CustomFieldValues.php

public function customFieldValueable()
{
    // If you are using custom column names, specify them here. Otherwise, ensure they match the migration.
    return $this->morphTo(null, 'custom_field_valueable_type', 'custom_field_valueable_id');
}

正确的命名约定可以在这里在 laravel 文档中找到

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