Laravel eloquent seems weird showing "[key] is null and [key] is not null" in SQL query -
i'm sorry i'm still new laravel/lumen , i'm having issue table relationships.
this error keep on receiving.
"message": "sqlstate[42s22]: column not found: 1054 unknown column 'clients.client_project_id' in 'where clause' (sql: select *
clientsclients.client_project_idnull ,clients.client_project_idnot null)",
if noticed, sql query contains weird condition , don't know in code problem.
i need expertise on how resolve this. if need other info, please inform me.
[additional info]
migration files
class createclientprojectassignmenttable extends migration { /** * run 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 migrations. * * @return void */ public function down() { schema::dropifexists('client_project_assignment'); } } class createclientstable extends migration { /** * run 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 migrations. * * @return void */ public function down() { schema::drop('clients'); } } seeder files
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 $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 $client) { $obj = new client; $obj->fill($client); $obj->save(); } } } models
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'); } } repository
class clientprojectrepository extends abstractcrudrepository { public function __construct(clientproject $model) { $this->setmodel($model); } public function getclients($project) { $this->model()::find(1)->clients()->get(); } } what i'm expecting:
i should able retrieve project id 1 , client's info particular project.
you try clients belong clientproject. relation not setup in migration. laravel tries find clients client_project_id relates clientproject id. need add migration client_projects.
$table->integer('client_project_id')->unsigned();
Comments
Post a Comment