【问题标题】:Laravel eloquent seems weird showing "[key] is null and [key] is not null" in SQL queryLaravel eloquent 在 SQL 查询中显示“[key] 为空且 [key] 不为空”似乎很奇怪
【发布时间】:2018-02-19 13:20:47
【问题描述】:

很抱歉,我还是 Laravel/Lumen 的新手,我遇到了表关系问题。

这是我不断收到的错误。

“消息”:“SQLSTATE [42S22]:找不到列:1054 未知列 'where 子句'中的'clients.client_project_id'(SQL:select * from clients 其中clients.client_project_id 为空且 clients.client_project_id 不为空)",

如果您注意到,SQL 查询包含一个奇怪的 where 条件,我不知道代码中的问题出在哪里。

我需要您的专业知识来解决这个问题。如果您需要其他信息,请通知我。

[附加信息]

迁移文件

class CreateClientProjectAssignmentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('client_project', function (Blueprint $table) {
            $table->increments('id');
            $table->string('project_key', 50);
            $table->integer('client_project_id')->unsigned();
            $table->timestamps();

            $table->foreign('client_project_id')
                  ->references('id')
                  ->on('clients')
                  ->onDelete('cascade');
        });
    }

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


class CreateClientsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->increments('id');

            $table->string('last_name', 50);
            $table->string('first_name', 50);
            $table->string('email_address', 50);
            $table->timestamps();
        });
    }

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

种子文件

class ClientProjectTableSeeder extends Seeder
{
    /**
     * @inheritdoc
     */
    public function run()
    {
        $this->loadDefaultProjects();
    }

    /**
     * Load default projects
     */
    private function loadDefaultProjects()
    {
        $projects = [
            [
                'project_key'    => 'PRJ',
                'client_project_id'     => 1
            ]
        ];

        foreach ($projects as $project) {
            $obj = new ClientProject;
            $obj->fill($project);
            $obj->save();
        }
    }
}

class ClientTableSeeder extends Seeder
{
    /**
     * @inheritdoc
     */
    public function run()
    {
        $this->loadDefaultClients();
    }

    /**
     * Load default clients
     */
    private function loadDefaultClients()
    {
        $clients = [
            [
                'last_name'     => 'First',
                'first_name'    => 'Client',
                'email_address' => 'c.first@sample.com',
            ],
            [
                'last_name'     => 'Second',
                'first_name'    => 'Client',
                'email_address' => 'c.second@sample.com',
            ]
        ];

        foreach ($clients as $client) {
            $obj = new Client;
            $obj->fill($client);
            $obj->save();
        }
    }
}

模型

class ClientProject extends AbstractCrudModel
{
    /**
     * @inheritdoc
     */
    protected $table = 'client_project';

    /**
     * @inheritdoc
     */
    protected $fillable = ['project_key', 'client_project_id'];

    /**
     * @inheritdoc
     */
    protected $hidden = ['updated_at'];

    /**
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function clients()
    {
        return $this->hasMany(Client::class);
    }
}

class Client extends AbstractCrudModel
{
    /**
     * @inheritdoc
     */
    protected $table = 'clients';

    /**
     * @inheritdoc
     */
    protected $hidden = ['updated_at'];

    /**
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function projects()
    {
        return $this->belongsToMany('App\Models\ClientProject');
    }
}

存储库

class ClientProjectRepository extends AbstractCrudRepository
{
    public function __construct(ClientProject $model)
    {
        $this->setModel($model);
    }

    public function getClients($project)
    {
        $this->model()::find(1)->clients()->get();
    }
}

我的期望:

我应该能够检索 id 为 1 的项目以及该特定项目的客户信息。

【问题讨论】:

  • 你应该显示代码。
  • 显示您的迁移文件和模型文件并告诉我们您在尝试什么
  • @TecBeast 我已经包含了显示迁移、播种器、模型和存储库文件的代码。
  • @aaron0207 我已经包含了显示迁移、播种器、模型和存储库文件的代码。
  • IMO 你犯了一个错误。您将关系设置为 1 对 1(1 个项目有一个客户),然后尝试将其检索为 1 对 N(1 个项目有很多客户)

标签: laravel eloquent lumen


【解决方案1】:

您尝试获取属于 ClientProject 的客户。此关系未在您的迁移中设置。 Laravel 尝试使用与 ClientProject id 相关的 client_project_id 查找客户端。您需要将此迁移添加到 client_projects。

$table->integer('client_project_id')->unsigned();

【讨论】:

  • client_id 还不够吗? client_projects 表中的client_id 是clients 表中id 的外键
  • 那么你需要查看与 belongsToMany 的关系。对于 belongsTo 关系,您需要所属模型的 id 是该模型本身。
  • 是的,我想我应该使用belongstoMany。 “对于 belongsTo 关系,您需要归属模型的 id 是该模型本身” - 很抱歉,您的意思是什么?
  • belongsTo 要求您在具有 belongsTo 函数本身的模型上拥有 _id。
  • 但是还是不行。我已经在 client_projects 表中将列名从 client_id 更改为 client_project_id 并将 belongsTo 更改为 belongsToMany。
猜你喜欢
  • 2015-07-07
  • 1970-01-01
  • 2014-08-28
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多