Categories: php, sql, laravel

Problem with getting data from query in Laravel

1 answer

I have two tables

field_values (with some data)

id  field_id  value  label          sort 1       1       1     Men            1 2       1       2     Women          2 3       2       3     Relationship   1 4       2       4     Chat           2 5       2       5     Friendship     3 

user_interests (with some data)

user_id  field_id  value_id    1        1         1    1        2         4    1        2         5 

I am trying to write a query where I will get user with id 1 and have field_id 2 and to be able to echo in my blade value_id 4 and 5 but not to echo those ids but to echo value of 'label' column that corresponds to value_id form user_interests table in this case 4,5 thus Chat, Friendship from field_values table in this example. Here is what I tried but I get array of six elements which are Relationship, Chat, Friendship x2. Any help is appreciated.


public static function queryFunction($userId) {     $results = DB::table('user_interests as uin')         ->select(DB::raw("             fv.*,             uin.field_id, uin.value_id         "))         ->join('field_values as fv', 'fv.field_id', '=', 'uin.field_id')         ->where('uin.field_id', 2)         ->where('uin.user_id', $userId)         ->get();          dd($results);      return $results; } 

All answers to this question, which has the identifier 61231218

The best answer:

What about 2 clear steps, without join:

$user_interests = DB::table('user_interests')->select('value_id')->where('field_id', 2)->where('user_id', $userId)->get();

From this take values as array ($user_interests_values) and than

$results = DB::table('field_values')->whereIn('value', $user_interests_values)->get();

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data