Код: Выделить всё
$personQuery = Person::query();
$column = 'value';
$needle = '%09133333%';
$personQuery->orWhereHasMorph('digitalContacts', '*', function ($contactQuery) use ($column, $needle) {
$contactQuery->where($column, 'like', '%' . $needle . '%');
});
$data = $personQuery->get();
Код: Выделить всё
{
"success": false,
"payload": [],
"errors": [
{
"type": "warning",
"code": "42703",
"message": "SQLSTATE[42703]: Undefined column: 7 ERROR: column \"digital_contactable_type\" does not exist\nLINE 1: select distinct \"digital_contactable_type\" from \"people\"\n ^ (Connection: tenant, SQL: select distinct \"digital_contactable_type\" from \"people\")"
}
]
}
отношения определяются в таких моделях:
Код: Выделить всё
class Person extends Model
{
public function digitalContacts()
{
return $this->morphMany(DigitalContact::class, 'digital_contactable', 'digital_contactable_type');
}
//...
}
Код: Выделить всё
class DigitalContact extends Model
{
public function digitalContactable()
{
return $this->morphTo();
}
//...
}
Код: Выделить всё
Schema::create('people', function (Blueprint $table) {
$table->unsignedBigInteger('id', true);
//...
$table->timestamps();
});
Код: Выделить всё
Schema::create('digital_contacts', function (Blueprint $table) {
$table->unsignedBigInteger('id', true);
$table->unsignedBigInteger('digital_contactable_id');
$table->string('digital_contactable_type'); // person, organization, etز
$table->string('value');
//...
$table->timestamps();
});
Подробнее здесь: https://stackoverflow.com/questions/784 ... lationship
Мобильная версия