mercredi 30 novembre 2016

How do you match data stored in pivots? Laravel 5.3

Let's say I have an agency and a user and I want to match the currently logged in user against agencies on age. I might write something like this:

$agematch = Agency::where('Age', 'like', Auth::user()->Age)->get();

But let's say most information like Work_Preferences is stored in pivot tables. Both users and agencies access the same Work_Preferences information, however they each have a unique pivot table set up to link to Work_Preferences. How might I perform a match query on that?

Edit: The relationships we're talking about here look something like this.

Users and Agencies are both separate tables looking after separate data. Users do not belong to Agencies and Agencies do not belong to Users. We're here to match them.

So data is similar. An agency for instance will say they want users with x preferences. There's quite a number of these, but structurally they're the same. It looks something like this.

Work_Preferences table:

id | Subcategory | Description

There's an intermediary table (the pivot table) between Work_Preferences and Users, and another one between Agencies and Work_Preferences. These tables store the ids of users, agencies and their work preferences with foreign keys pointing to their respective parents.

It's, to the best of my knowledge, a pretty normal many to many association. You know the deal. But to summarise.

Users -- user_workpreferences -- Work_Preferences

Agencies -- agency_workpreferences -- Work_Preferences

I'm still very new to this, so please forgive the egregious formatting, I'm learning as quick as I can.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire