Home > Software engineering >  Laravel Table Issue
Laravel Table Issue

Time:11-04

I have two tables 1.users 2.screenshots

what i'm doing - I'm fatching data from screenshots table on the basis of userid & the userid is coming from users table

what's the issue - when i'm retrieving unique id of a users table but getting only one user id, but there are 3 users in the database

My Controller Code

    $data = ['LoggedUserInfo'=>Admin::where('id','=', session('LoggedUser'))->first()];
    $da = \DB::table('users');
    $ll = null;
    $ll2 = null;

    foreach ($data as $ll) {
        $ll = $ll->admin_id;
    }
    
    $ff = $da->where('admin_id', '=', $ll)->get();
    foreach ($ff as $llo2) {
        $ll2 = $llo2->uniqueid;
    }
    $sc = \DB::table('screenshots');
    $km = $sc->where('userid','=', $ll2);
    $kk = $km->paginate(15);

The output of $ll2 is 58641475 the first user uniqueid

My screenshots database

screenshots database

Users database

users database

You can also give me suggestions for better logic

CodePudding user response:

That's because you only take the last user :

$ll2 = null;

foreach ($ff as $llo2) {
    $ll2 = $llo2->uniqueid;
}

$ll2 will replace the previous result.

You need to make it an array, if you want screenshots of all users :

$ll2 = [];

foreach ($ff as $llo2) {
    $ll2[] = $llo2->id; // Not uniqueid or?
}

$sc = \DB::table('screenshots');
$km = $sc->whereIn('userid', $ll2);
$kk = $km->paginate(15);
  • Related