Мне интересно, возможно ли это. Имею 3 модели. Пользователи Арендатор PropertyAdverts
Я пытаюсь выяснить, могу ли я сделать такой запрос.
Найдите всех арендаторов, чьи настройки соответствуют свойствам текущего пользователя, вошедшего в систему.
3 базы данных похожи на
Модель пользователя
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('userType');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
PropertyAdverts
Schema::create('property_adverts', function (Blueprint $table) {
$table->increments('id');
$table->string("photo");
$table->string('address');
$table->string('county');
$table->string('town');
$table->string('type');
$table->string('rent');
$table->string('date');
$table->string('bedrooms');
$table->string('bathrooms');
$table->string('furnished');
$table->longText('description');
$table->integer('user_id'); //Landlord ID
$table->timestamps();
});
Предпочтения арендатора
Schema::create('tenant_preferances', function (Blueprint $table) {
$table->increments('id');
$table->string('county');
$table->string('type');
$table->string('rent');
$table->string('bedrooms');
$table->string('bathrooms');
$table->boolean('status')->default('0');
$table->integer('user_id'); //Tenant ID
$table->timestamps();
});
Да, это возможно. Вам нужно покопаться в связи. Затем вы можете создать функцию для определения отношения в вашей модели:
function propertyAdverts() {
return $this->hasMany(PropertyAdverts::class);
}
Вы можете получить доступ к отношению из пользовательской модели с помощью $user->propertyAdverts
. Если вы хотите загрузить их, вы можете сделать это:
User::with('propertyAdverts')->find(3);
Но обратите внимание, что Laravel по умолчанию не выполняет обычное соединение. Сначала он выбирает всех пользователей, а затем извлекает все propertyAdverts
, используя один запрос с помощью оператора in
.