I'm quite new in Laravel and have some question about Laravel SQL query through using polymorphic relationship
The situation is: I have two different tables in my SQL. One table 'estates' contains a list of estates and their parameters Another table is 'type_categories' which contains a list of type categories Every estate can be related to many type categories and every type category can have many estates (so I use polymorphic relationships many to many). They are related through the third table ''
So, as a result, I need to do a query, which will find all estates which is related to category, but my user can choose a numerous of category, not just one.
I tried to use functionfind(1)->estates;
which as a parameter have an id of type category, which related estates I tried to find. It works fine, but the problem is, that my user needs to find an estate to several categories and function, but function finds works with just one parameter in the situation we nave polymorphic relation, not with the several parameters as I need.
Is there some way to use several parameters in function find() and also use polymorphism? Sure, that there exist some other functions for it...
Then I show you my code.
Estates model looks:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Estate extends Model
{
protected $fillable = ['title', 'slug', 'estate_text', 'created_by', 'modified_by'];
public function categories() {
return $this->morphToMany('App\TypeCategory', 'categoryable');
}
}
Type categories model looks:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class TypeCategory extends Model
{
protected $fillable = ['title', 'slug', 'parent_id', 'created_by', 'modified_by'];
public function estates() {
return $this->morphedByMany('App\Estate', 'categoryable');
}
}
And finally, estates controller where I tried to select estates model looks:
use Illuminate\Http\Request;
use App\Estate;
use App\TypeCategory;
class EstateController extends Controller {
public function searchEstate() {
$estates = TypeCategory::find(1)->estates;
return view('main', [
'estates' => $estates
] );
}
}
I don't sure it is your problem or not. try this:
$estates = Estate::whereHas('categories', function ($query) {
$query->whereIn('slug', ['xxx','yyy','zzz']);
})->get();
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.