0

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                     |
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+
Narktor
  • 977
  • 14
  • 34

1 Answers1

1

return $this->hasOne('App\ModelName', 'foreign_key', 'local_key');

The problem occurs because when you are using with('extensiontable_itc');

laravel according to your relationship method extensiontable_itc to find the foreign_key, and what you given is extensiontable_itc, and you don't have this column,

Change to your extensiontable_itc to coretable_id in the hasOne method:

  public function extensiontable_itc()
  {
    return $this->hasOne('App\extensiontable_itc', 'coretable_id');
  }

and coretable has one extensiontable_itc, extensiontable_itc belongs to coretable, you need to fine the inverse of a hasOne relationship using the belongsTo method:

  public function coretable()
  {
    return $this->belongsTo('App\coretable', 'coretable_id');
  }

More reference: one-to-one

TsaiKoga
  • 12,914
  • 2
  • 19
  • 28