Saturday 21 April 2018

Understanding Laravel Return set

Difference between Laravel Eloquent, DB Raw, Json Encode and Json Decode result set. If you're using a DB:select raw command

$currencyRaw = DB::select( DB::raw('select * from currency));
It will give you a type array of objects in which you can display with -> command. The return set will be like this

array (
  0 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'EUR',
     's_name' => 'European Union euro',
     's_description' => 'Euro €',
     'b_enabled' => 1,
  )),
  1 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'GBP',
     's_name' => 'United Kingdom pound',
     's_description' => 'Pound £',
     'b_enabled' => 1,
  )),
  2 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'USD',
     's_name' => 'United States dollar',
     's_description' => 'Dollar US$',
     'b_enabled' => 1,
  )),
)  
If you're using a json_decode with FALSE parameter on eloquent object

        $currencyEloquent = Currency::all();
        $currencyJsonDecodeFalse  = json_decode(($currencyEloquent),false);
It will give you a type array of objects in which you can display with -> command. The return set will be like this

array (
  0 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'EUR',
     's_name' => 'European Union euro',
     's_description' => 'Euro €',
     'b_enabled' => true,
  )),
  1 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'GBP',
     's_name' => 'United Kingdom pound',
     's_description' => 'Pound £',
     'b_enabled' => true,
  )),
  2 => 
  stdClass::__set_state(array(
     'pk_c_code' => 'USD',
     's_name' => 'United States dollar',
     's_description' => 'Dollar US$',
     'b_enabled' => true,
  )),
)
If you're using a ->toArray on eloquent object

        $currencyEloquent = Currency::all();
        $currencyArray  = $currencyEloquent->toArray();
It will give you a type array with keys and values in which you can display with $currency['s_name']. The return set will be like this

array (
  0 => 
  array (
    'pk_c_code' => 'EUR',
    's_name' => 'European Union euro',
    's_description' => 'Euro €',
    'b_enabled' => true,
  ),
  1 => 
  array (
    'pk_c_code' => 'GBP',
    's_name' => 'United Kingdom pound',
    's_description' => 'Pound £',
    'b_enabled' => true,
  ),
  2 => 
  array (
    'pk_c_code' => 'USD',
    's_name' => 'United States dollar',
    's_description' => 'Dollar US$',
    'b_enabled' => true,
  ),
)  
If you're using a json_decode with TRUE parameter on eloquent object

        $currencyEloquent = Currency::all();
        $currencyJsonDecodeFalse  = json_decode(($currencyEloquent),true);
It will give you a type array with keys and value in which you can display with $currency['s_name'] command. The return set will be like this

array (
  0 => 
  array (
    'pk_c_code' => 'EUR',
    's_name' => 'European Union euro',
    's_description' => 'Euro €',
    'b_enabled' => true,
  ),
  1 => 
  array (
    'pk_c_code' => 'GBP',
    's_name' => 'United Kingdom pound',
    's_description' => 'Pound £',
    'b_enabled' => true,
  ),
  2 => 
  array (
    'pk_c_code' => 'USD',
    's_name' => 'United States dollar',
    's_description' => 'Dollar US$',
    'b_enabled' => true,
  ),
)  

Various ways to play with the return set of DB:RAW


        $sql = "select * from users";
        $keyUserIds = DB::select(DB::raw($sql));
        // $keyUserIds is an array of objects

        //converted to collection
        $collectionUserId = collect($keyUserIds);

        //to get columns
        $pluckedUserId = $collectionUserId->pluck('user_id');


        //Convert array of objects to array of ids only
        $user_ids = array_column($keyUserIds, 'user_id');
Share:

0 comments:

Post a Comment

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.