Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tawanda's avatar

Escaping Model name in Polymorphic query

Hello,

I am using Laravel 5.0 & can't go higher due to the PHP version on my host. I have set up a polymorphic relationship (among other relationship that are working well) that fails to pull data from the table. After installing "barryvdh/laravel-debugbar": "2.0" to see where things were falling down, I noticed that the line (and `MEDIA`.`MEDIA_MOPH_TYPE` = 'App\Student';) generated by Eloquent is causing the problem.

select * 
from `MEDIA` 
where `MEDIA`.`deleted_at` is null 
and `MEDIA`.`MEDIA_MOPH_ID` in ('1', '2', '3') 
and `MEDIA`.`MEDIA_MOPH_TYPE` = 'App\Student';

If I escape the backslash in the model name, using Sequel Pro, to (and `MEDIA`.`MEDIA_MOPH_TYPE` = 'App\\Student';) the query works. The question is how do I get Eloquent to escape the name model string too?

Could be missing something obvious here (other than the r in the word morph ^_^). Tables & models as below with all primary key declared where not the standard id:

Media table:

Media_id
Media_folder_url
Media_blob
Media_moph_id
Media_moph_type

Media model relationship:

/**
* Get all of the owning media(ble) mophable models.
*/
public function media_moph()
{
        return $this->morphTo();
}

Students table:

Student_id 
Student_fname
Student_lname
(etc)

Student model relationship:

/**
* Get all of the media for the student.
*/
public function media()
{
        return $this->morphMany('App\Media', 'media_moph');
} 

Thanks in advance!

0 likes
3 replies
jekinney's avatar

Never had that issue before. MySQL should return a string, so what is the column type? Varchar?

Tawanda's avatar

The 'MEDIA_MOPH_TYPE' field is a VARCHAR created by a migration using the $table->morphs('MEDIA_MOPH');. I haven't tried it in Oracle yet because usually if it doesn't work in MySQL in won't work in Oracle as well.

Could it be I haven't stuck to the naming convention for my column fields & relational methods in the model?

Tawanda's avatar
Tawanda
OP
Best Answer
Level 8

I finally got it work. After some digging around, I found the $morphClass model property which allowed me to save the model name Student without the App\ namespace (protected $morphClass = 'Student';). This meant I didn't need to escape the query anymore. I also changed the:

  • I also changed $table->morphs('Media_moph'); migration column declaration to all lowercase $table->morphs('media_morph'); which eloquent seemed to like better as it generates the two morph columns _id & _type in lowercase. Having mixed case still wasn't working.
  • morphMany relationship on student to morphOne, which what I really wanted (a 1-1 relationship between student and media).

Media table (updated):

Media_id
Media_folder_url
Media_blob
media_moph_id
media_moph_type

Media model relationship (unchanged):

/**
* Get all of the owning media(ble) mophable models.
*/
public function media_moph()
{
        return $this->morphTo();
}

Students table (unchanged):

Student_id 
Student_fname
Student_lname
(etc)

Student model relationship (updated):

protected $morphClass = 'Student';

/**
* Get all of the media for the student.
*/
public function media()
{
        return $this->morphOne('App\Media', 'media_moph');
} 

SQL now being generated and returning results:

select * 
from `MEDIA` 
where `MEDIA`.`deleted_at` is null 
and `MEDIA`.`media_moph_id` in ('1', '2', '3') 
and `MEDIA`.`media_moph_type` = 'Student';

Please or to participate in this conversation.