Answer the question
In order to leave comments, you need to log in
Is Laravel starting to use Eloquent right?
I continue to explore new horizons ..
For understanding, to understand the example ...
There are two tables - users (registrations) - there is a country field, holidays by countries (countries).
What spell needs to be cast so that the holiday table looks like this:
Country | Holiday | Number of users from this country
Do I understand correctly that:
In the User model, I should create:
public function countries()
{
return $this->hasMany('App\Holidays');
}
Answer the question
In order to leave comments, you need to log in
EVERYTHING is wrong here.
- The relationship is called countries, but points to the Holidays model (it's common to name models in the singular, not plural. That is, just Holiday.)
- The relationship is called users, but the relationship type belongsTo . BelongsTo A relationship is a one-to-one relationship. When linking BelongsTo - there can't be many users (in this case).
- $data['holidays']=$holidays->get(); - What is it? What is $holidays?
- The data array is passed to the view, and $holidays is used immediately in the view . Mistake.
- {{\App\Holidays::get()->users($holiday->country_id)->count()}}
- what should this line do?
All code is wrong. Reconsider your decision and use code formatting.
-
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question