D
D
dk-web2015-10-19 18:29:18
Laravel
dk-web, 2015-10-19 18:29:18

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');
    }

Also create model Holidays extends Model
public function users()
{
return $this->belongsTo('App\User);
}
Next:
$data['holidays']=$holidays->get();
then return view('holidays',['data'=>$data]
into view:
@foreach($holidays as $holiday)
{{$holiday->country_name}} {{\App\Holidays::get()- >users($holiday->country_id)->count()}}
@endforeach
A little confused... - I like everything, but I can't get used to it yet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-10-19
@dk-web

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 question

Ask a Question

731 491 924 answers to any question