对不起,我还是Laravel / Lumen的新人,我遇到了桌面关系问题。
这是我一直接收的错误。
“message”:“SQLSTATE [42S22]:找不到列:1054未知列 'where子句'中的'clients.client_project_id'(SQL:select * from
clients
clients
client_project_id
。clients
为空client_project_id
。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'); } }
不为空)“,
如果您注意到,SQL查询包含一个奇怪的where条件,我不知道代码在哪里出现问题。
我需要你的专业知识来解决这个问题。如果您需要其他信息,请告知我。
[其他信息]
移民文件
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();
}
}
}
SEEDER FILES
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();
}
}
REPOSITORY
%dopar%
我期待的是:
我应该能够检索id为1的项目以及该特定项目的客户信息。
答案 0 :(得分:0)
您尝试获取属于ClientProject的客户端。您的迁移中未设置此关系。 Laravel尝试使用与ClientProject id相关的client_project_id来查找客户端。您需要将此迁移添加到client_projects。
$table->integer('client_project_id')->unsigned();