I have two tables: Coretable and extensiontable_itc. They are in a 1-n relationship, extensiontable_itc can have N records referencing 1 record on coretable.
If understood correctly, I learnt here Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?
that model::with('relatedModel')->get()
can fetch me all the records from coretable and extensiontable_itc wherever the coretablerecords are referenced by the extensiontable_itc records.
However, when I apply this to my code like here:
$join = coretable::with('extensiontable_itc')->get();
log::info($join);
I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'extensiontable_itc.extensiontable_itc' in 'where clause' (SQL: select * from `extensiontable_itc` where `extensiontable_itc`.`extensiontable_itc` in (1))
As far as I understand, its looking for a column "extensiontable_itc" on my extensiontable_itc table. This of course cant work, but I dont understand why this happens? The foreign keys are in place in my db, and the respective models should be fine, here is their code:
coretable.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CoreTable extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'coretable';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'Internal_key'
];
/**
* Many-To-Many relationship with User-Model.
*/
public function extensiontable_itc()
{
return $this->hasOne('App\extensiontable_itc', 'extensiontable_itc');
}
public function inaccessibletable()
{
return $this->hasOne('App\inaccessibletable', 'inaccessibletable');
}
}
extensiontable_itc.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class extensiontable_itc extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'extensiontable_itc';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'description'
];
/**
* Many-To-Many relationship with User-Model.
*/
public function coretable()
{
return $this->hasOne('App\coretable', 'coretable');
}
}
And here is an overview of the foreign keys in my DB:
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+
| ad_usersxad_groups | Ad_user_id | fk_ad_groupxad_user | ad_users | id |
| ad_usersxad_groups | Ad_group_id | fk_ad_userxad_group | ad_groups | id |
| extensiontables_registryxad_groups | ad_group_id | fk_ad_groupxextensiontables_registry | ad_groups | id |
| extensiontables_registryxad_groups | extensiontables_registry_id | fk_extensiontables_registryxad_group | extensiontables_registry | id |
| extensiontable_itc | coretable_id | fk_extensiontable_itc_coretable | coretable | id |
| inaccessibletable | coretable_id | fk_inaccessibletable_coretable | coretable | id |
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+