Comparing laravel query results

Inside the $get_user and $get_code , they have group_id.

I have dd (); they both made 100% sure.

a $get_user request has several group_id, and $get_code has only one group_id , which is equal to one of $get_user group_id.

Currently, the goal is to create a group_id matching request.

Get code with group id equal to one of $get_user group_id

 public function getCodesViewQr($code_id) { $userid = Auth::id(); $get_user = GroupUser::all()->where('user_id',$userid); $get_code = Code::all()->where('id',$code_id); $group_match = GroupUser::where('group_id', $get_code->group_id); $view['get_users'] = $get_user; $view['get_codes'] = $get_code; $view['group_matchs'] = $group_match; return view('codes.view_qr_code', $view); } 

The group match request does not work. $get_code->group_id does not receive group_id code.

If there is a match, set $match to rue. else $match - False

 $group_match = GroupUser::where('group_id', $get_code->group_id); 

I use two Models Code and GroupUser

My Code table looks like this:

-id

-group_id (This is the only on important right now)

-code_type

My GroupUser table is as follows:

-id

-group_id (This is the only on important right now)

-user_id

-user_role

I knitted Models

In my code model, I have a relationship with GroupUser

 public function group_user() { return $this->belongsto('App\GroupUser'); } 

And inside my GroupUser model, I have a relationship with Code

 public function code() { return $this->belongsto('App\Code'); } 

Inside my code controller, I have included my models.

 use App\Code; use App\GroupUser; 
+5
source share
2 answers

Hi guys, so I had help from the guy I work with, and this is the solution he came across. We made some adjustments. all databases and results remain the same. we just changed the method we used to get the results.

I really appreciate all the help from @ linktoahref

  public function view_code($random) { $code = Code::where('random', $random)->first(); $view['code'] = $code; if ($code->code_type == 1) { // Its a coupon if (!empty(Auth::user())) { // Someones is logged in $user = Auth::user(); $view['user'] = $user; $user_groups = GroupUser::where('user_id',$user->id)->pluck('group_id')->toArray(); if (in_array($code->group_id, $user_groups)) { // The user is an admin of this code return view('view_codes.coupon_admin', $view); }else { // Save the code to that users account return view('view_codes.generic_code', $view); } }else { // Anon return view('view_codes.coupon_anon', $view); } }elseif ($code->code_type == 2) { // Voucher.. }else { // We don't know how to deal with that code type } } 
+1
source
 $get_code = Code::find($code_id); // Check if the code isn't null, else give a fallback to group_id $group_id = 0; if (! is_null($get_code)) { $group_id = $get_code->group_id; } $group_match = GroupUser::where('group_id', $group_id) ->get(); $match = FALSE; if ($group_match->count()) { $match = TRUE; } 
0
source

Source: https://habr.com/ru/post/1274953/


All Articles