Laravel 5 pagination issue - php

I am working with laravel 5 pagination and getting some problems.
Here is my code DB::table('users')->paginate(2) and it returns an object of LengthAwarePaginator Like this :
Illuminate\Pagination\LengthAwarePaginator Object
(
[total:protected] => 4
[lastPage:protected] => 2
[items:protected] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 3
[Name] => shivani
[email] => shivani.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
[1] => stdClass Object
(
[id] => 4
[Name] => shivani
[email] => shivani2.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
)
)
[perPage:protected] => 2
[currentPage:protected] => 1
[path:protected] => http://localhost/shivani/public/check/user-list
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
My problem is that I want to iterate through this object and retrieve items object that holds database data i.e. name , email etc.
But I don't know how to access protected members from the returned object.

$data = DB::table('users')->paginate(2)->toArray()
Try this.. it might work for you

You can just use the returned object like it where an array:
$users = DB::table('users')->paginate(2);
foreach($users as $user){
echo $user->email;
}

You can access paginated objects same way you can access any other collection. For example
foreach(DB::table('users')->paginate(2) as $object) {
echo $object->name;
}

Related

Getting a specific value from a json response

I have this code
$client = json_decode($client);
echo "<pre>";
print_r($client);
which produces there
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Jojohn#doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
My question is how do I get the value of name and account_rep I tried
echo $client['0']['object']['name'];
but that does not work it just throws an error
Cannot use object of type stdClass as array
json_decode($variable), is used to decode or convert a JSON object to a PHP object.
So you could do this as $client['0'] is an object.
echo $client['0']->name;
But I'd say you should rather convert the json object to associative array instead of PHP object by passing TRUE to as an argument to the json_decode. When TRUE, returned objects get converted into associative arrays.
$client = json_decode($client, true);
Now $client is
Array
(
[0] => Array
(
[id] => 1
[name] => Jojohn#doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
Now you could simply do
echo $client[0]['name'];

Push array object into another array object in php

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;

PHP Loop Object member manipulation

I have a multi dimensional object result set to which I want to loop through to change a value of an inner object member to the value of an outer object member. When the loop finishes executing, even though the values of each outer object member is different, the values of the inner object member are all the same value which is the value of the final outer object member.
foreach ($user as $key => &$value) {
$user[$key]->user->company = $value->Company;
}
When I dump the $user array which contains the objects I get This as my result:
Array
(
[0] => common\models\UserRoleMapping Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Role_Mapping_Id] => 9b55d3a7-8de6-11e5-ae99-60a44ce86902
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[User_Role_Id] => 49c82d92-31d0-11e4-8309-60a44ce86902
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
[Company] => 79538f95-7e25-11e5-b514-60a44ce86902
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
[user] => common\models\User Object
(
[company] => 0e7505fc-7961-11e5-a563-60a44ce86902
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[Username] => andre#hotmail.com
[Password] => fbb8f9f42cb0532f30759d9e4fa3e2fc90d714321ce11fdc42f5c9b23756979c6166e674e421b8580ab53a0e75e9d2758f7b8bbe70a33a5ad0cdc25b553e08b4
[Login_Status] => Offline
[Failed_Login_Attempts] => 0
[Verification] =>
[Salt] => dd7c8a297a8d34ba5dc7cd4b45a1e593
[Last_Reset] => 0000-00-00 00:00:00
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
)
)
)
)
[1] => common\models\UserRoleMapping Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Role_Mapping_Id] => fdb962ef-8de6-11e5-ae99-60a44ce86902
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[User_Role_Id] => 49c82d92-31d0-11e4-8309-60a44ce86902
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
[Company] => 0e7505fc-7961-11e5-a563-60a44ce86902
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
[user] => common\models\User Object
(
[company] => 0e7505fc-7961-11e5-a563-60a44ce86902
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[Username] => andre#hotmail.com
[Password] => fbb8f9f42cb0532f30759d9e4fa3e2fc90d714321ce11fdc42f5c9b23756979c6166e674e421b8580ab53a0e75e9d2758f7b8bbe70a33a5ad0cdc25b553e08b4
[Login_Status] => Offline
[Failed_Login_Attempts] => 0
[Verification] =>
[Salt] => dd7c8a297a8d34ba5dc7cd4b45a1e593
[Last_Reset] => 0000-00-00 00:00:00
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
)
)
)
)
)
As you can see in the dumped values, both inner user objects have the same value for their company member which the value Found in the final Outer Object Company member.
I am seeking assistance with how I can get each inner user object to have their respective outer object company values.

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

Multidimensional array push manually php codeigniter

Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
Hi friends,
I have an array of products that I need to create manually next stdclass Object.These array are generated by pushing value.
I'm trying to solve this for more than an hour now, but I don't get it to work. I know it should be easy...but anyway - I don't get it :D
I have working for an hour to create manually next stdclass Object. I want like this.
How do i create this.
Can anyone help with this?
Thanks in Advance,
J
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
[2] => stdClass Object
(
[id] => 16
[user_id] => 1
[following_id] => 5
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
One way of doing this is to use a function like so :
function create($id, $user_id, $following_id, $type, $created_on){
$obj = new stdClass;
$obj->id = $id;
$obj->user_id = $user_id;
$obj->following_id = $following_id;
$obj->type = $type;
$obj->created_on = $created_on;
return $obj;
}
And assuming your array is $array, you add items to it like this:
$array[] = create(16, 1, 5, 'user', '2014-05-27 11:55:17');
Hope this helps

Categories