4

i have a function to show data with 3 where clause condition . but this output is invalid . i have 4 data and just show 1 . this function like this :

public function alkes_user()
{
    $user_id = Auth::user()->id;

    $unit        = User::where('roles_id' , 1)->pluck('nama_unit', 'id'); 


    $alat    = Alat::with('users')
    ->where('jenis' ,'Alkes')
    ->where('user_id',  $user_id)
    ->where('is_active',  'true')
    ->orderBy('created_at', 'desc')->paginate(10);
    return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}

this data only showing 1 , but in database i have so many data . what wrong and how to solve this ? thanks you

Adhik Mulat
  • 538
  • 2
  • 10
  • 39
  • Your query is correct. Show your table data. – Sehdev Feb 21 '20 at 03:27
  • if i remove 1 where clause , its can showing full data . l – Adhik Mulat Feb 21 '20 at 03:30
  • is jenis column in your users table? Please share your users table data – Sehdev Feb 21 '20 at 03:48
  • gives us more information where and how the data are stored on DB or you can simply solve this using `orWhere` method If any of the three `where` condition can be true since chaining `where` will be using 'AND' – Anuj Shrestha Feb 21 '20 at 04:07
  • I guess you want to filter `or`, can u explain what results do u want? – TsaiKoga Feb 21 '20 at 04:07
  • u mean its need OrWhere ? – Adhik Mulat Feb 21 '20 at 04:08
  • Yes use `orWhere` if any of the three `where` condition can be true to fetch the rows – Anuj Shrestha Feb 21 '20 at 04:09
  • or u can post the example datas, and post what the real result is. – TsaiKoga Feb 21 '20 at 04:09
  • hope this help you https://stackoverflow.com/questions/19325312/how-to-create-multiple-where-clause-query-using-laravel-eloquent – Muhammad Sufyan Tsauri Feb 21 '20 at 04:09
  • sir , its three where its same as if i using orWhere ? i miss perception on this . using where = orWhere ? – Adhik Mulat Feb 21 '20 at 04:12
  • chaining only `where` will generate the SQL query using `AND` while using `orWhere` it will generate the query using `OR`. You can use `toSql()` method to check what type of query is being generated too – Anuj Shrestha Feb 21 '20 at 04:18
  • So i need chaining only where on three clause . not OrWhere .its make different what i want to do – Adhik Mulat Feb 21 '20 at 04:20
  • The important thing is what result you really want, may be orWhere cannot help u.plz explain what datas do u want from database? – TsaiKoga Feb 21 '20 at 04:24
  • The question is: what are you trying to achieve? We can not help you if we do not understand the issue. First you get the id of the current user, then you query the `User` model (without using `$user_id`), and then you query the `Alat` model with a query like `"...WHERE jenis='Alkes' AND user_id=$user_id AND is_active='true' ORDER BY created_at DESC"`. Note that here you are using `true` as a string. Is all this on purpose? – theSlyest Feb 21 '20 at 08:55

3 Answers3

0

Rewrite your query like this if you want multiple where condition:

public function alkes_user(){
    $user_id = Auth::user()->id;
    $unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
    $alat    = Alat::with('users')
                ->where([['jenis' ,'Alkes'],['user_id',  $user_id],['is_active',  'true']])
                ->orderBy('created_at', 'desc')->paginate(10);
    return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}
Khalid Khan
  • 3,017
  • 1
  • 11
  • 27
0

Simple Solution

$alat= Alat::with(array('users' => function($query) {
    $query->where([['jenis' ,'Alkes'],['user_id',  $user_id],[is_active',  'true']]);
}))->orderBy('created_at', 'desc')->paginate(10);
CuriousDev
  • 56
  • 1
  • 6
0
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')->where([
        ['jenis' ,'=','Alkes'],
        ['user_id','=',$user_id],
        ['is_active','=','true']       
])->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
Naeem Ijaz
  • 805
  • 9
  • 17