Join the community and participate in the ongoing discussions. Join Now!
How can I find a particular ID while looping through a column

I trying to validate a user to check if the user has a wallet table or not, if the user happens not to have a wallet, then i automatically my function should create one for the user.

This is my code below:


public function index(){

$uid = Auth()->user()->id;

$caccountid = Auth()->user()->accountid;

// return $cid;

// $spl = DB::select("SELECT userid FROM payments");

$retrive = payment::select('userid')->get();

foreach($retrive as $value){

$retriveUserid = $value->userid;


if($retriveUserid != $cid){

$createWallet = new payment;

$createWallet->userid = $cid;

$createWallet->fundamount = 0;

$createWallet->earnamount = 0;

$createWallet->addamount = 0;

$createWallet->debitamount = 0;

$createWallet->accountid = $caccountid;

$createWallet->transid = 0;


return view('admin.home');

// return $createWallet;


return view('admin.home');




The major problem is that the foreach loop is only taking the first ID in column userid, instead of loop through all the fields in the column to check if anyone match the Authenticated User ID.

Even when i tried adding the IF structure inside the FOREACH Loop, it still working the same way.


07th May, 2020 @ 01:48 am