Mikail Mikail - 15 days ago 4x
MySQL Question

laravel 5 reletionship hasMany on two columns

I'm trying to implement relationship on two columns, record_id and table_name,

here is what I got so far, Model name BatteryAssets:

public function Attachments()
return $this->hasMany('App\Attachments','record_id','id')->where('tabel_name','`battery_assets`');

please note that 'battery_asstes' is a string, so it's not dynamic, I'm trying to make it as a string, but get the following error :

QueryException in Connection.php line 761:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tabel_name' in 'where clause' (SQL: select * from `attachments` where `attachments`.`record_id` = 1 and `attachments`.`record_id` is not null and `tabel_name` = `battery_assets`)

as you see I'm I have tried to put battery_assets into `` and also I've tried to put them into double quotes ' "battery_assets" ' and also no quotes

and also please not that BatteryAssets table does not have table_name column, I'm only want to use it as a static value in the where clause, also I've tryied to do


but It gives the same error


The error says Colomn not found, the column name you are trying to 'call' is called 'tabel_name'. Are you sure the column name isn't 'table_name'? (table in english and not dutch, atleast I think you are dutch)

So it should be:

public function Attachments()
       return $this->hasMany('App\Attachments','record_id','id')->where('table_name','`battery_assets`');

If your column is called 'tabel_name' in your database. I would recommend using English naming, since 'name' is English.