【发布时间】: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 个项目有很多客户)