Laravel 拉雷维尔雄辩的表演似乎很怪异;[key]为空且[key]不为空;在SQL查询中

Laravel 拉雷维尔雄辩的表演似乎很怪异;[key]为空且[key]不为空;在SQL查询中,laravel,eloquent,lumen,Laravel,Eloquent,Lumen,很抱歉,我对Laravel/Lumen还是一个新手,我对表关系有问题 这是我不断收到的错误 “消息”:“SQLSTATE[42S22]:未找到列:1054未知列 “where子句”(SQL:select*from)中的“clients.client\u project\u id” clients其中clientsclient\u project\u id为空且 clientsclient\u project\u id不为空)” 如果您注意到,SQL查询包含一个奇怪的where条件,我不知道代码中

很抱歉,我对Laravel/Lumen还是一个新手,我对表关系有问题

这是我不断收到的错误

“消息”:“SQLSTATE[42S22]:未找到列:1054未知列 “where子句”(SQL:select*from)中的“clients.client\u project\u id”
clients
其中
clients
client\u project\u id
为空且
clients
client\u project\u 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 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 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的项目以及该特定项目的客户信息。

您尝试获取属于ClientProject的客户。迁移中未设置此关系。Laravel尝试查找具有与ClientProject id相关的客户端项目id的客户端。您需要将此迁移添加到客户端项目

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

您尝试获取属于ClientProject的客户端。迁移中未设置此关系。Laravel尝试查找具有与ClientProject id相关的客户端项目id的客户端。您需要将此迁移添加到客户端项目

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


您应该显示代码。显示您的迁移文件和模型文件,并告诉我们您是什么trying@TecBeast我已经包含了显示迁移、播种器、模型和存储库文件的代码。@aaron0207我已经包含了显示迁移、播种器、模型和存储库文件的代码。在我看来,您犯了一个错误。您正在将关系设置为1对1(1个项目有一个客户端),然后尝试将其检索为1对N(1个项目有多个客户端)。您应该显示代码。显示迁移文件和模型文件,并告诉我们您是什么trying@TecBeast我已经包括了显示迁移的代码,seeder,模型和存储库文件。@aaron0207我已经包含了显示迁移、播种器、模型和存储库文件的代码。在我看来,您犯了一个错误。您正在将一个关系设置为1对1(1个项目有一个客户机),然后尝试将其检索为1对N(1个项目有多个客户机),难道客户机id不够吗?client_projects表中的client_id是clients表中id的外键,那么您需要查看与belongtomany的关系。对于belongsTo关系,您需要归属模型的id为该模型本身。是的,我想我应该使用belongstomy。“对于belongsTo关系,您需要归属模型的id作为该模型本身”-很抱歉,但您这么说是什么意思?belongsTo要求您在模型上具有belongsTo函数本身的_id。但它仍然不起作用。我已经在client\u projects表中将列名从client\u id更改为client\u project\u id,并将belongsTo更改为belongsToMany。client\u id还不够吗?client_projects表中的client_id是clients表中id的外键,那么您需要查看与belongtomany的关系。对于belongsTo关系,您需要归属模型的id为该模型本身。是的,我想我应该使用belongstomy。“对于belongsTo关系,您需要归属模型的id作为该模型本身”-很抱歉,但您这么说是什么意思?belongsTo要求您在模型上具有belongsTo函数本身的_id。但它仍然不起作用。我已经将client_projects表中的列名从client_id更改为client_project_id,并将belongsTo更改为belongstomy。