I want to save results to cache but datamapper result objects is huge array.
I want to get only my query results without other data that referenced codeigniter data (models/configs/languages/etc..)
How can do this?
I searched on SO, internet and manual page (http://datamapper.wanwizard.eu/) but i couldnt find anything..
If you just want to access the core information about your records, try using the array extension here: http://datamapper.wanwizard.eu/pages/extensions/array.html
This allows you to run something like:
$objects-> all_to_array();
... which returns an array of objects, with all properties, but without the models/configs/languages &c. that you mention.
I want to addition some tips on #sevenpointsix answer
If you are using include_related, you have to specify columns like following (relation_column):
$fields = array('id', 'title', 'user_firstname', 'user_lastname', 'category_name');
$posts->get()->all_to_array($fields);
Related
I'm trying to paginate my json response in terms of multiples of 20 but for some reason, I'm getting Error: Call to a member function chunk() on array.
To clarify, $results has data in it and $request->get('offset'); has a value. Is there something I'm doing wrong? I'm seeing examples when searching on SO where people are doing this on a collection but for some reason, it isn't working for me. How can I make this work?
Note: If I do dd($results['num_results']);, it displays 360. I feel like I need to use this some how but I'm not sure how. I'm trying to achieve this in the most Laravel way possible. Also note, no database is being used - this is simply a get request on an external API.
Thanks in advance.
collect($results);
$outputs = $results->chunk(20)[$request->get('offset')];
dd($outputs);
$results is still an array. You are not setting the new Collection you are creating to a variable: collect($results). This does not change $results. So you are calling chunk on the array, $results. Call chunk on the Collection instead:
$collection = collect($results);
$outputs = $collection->chunk(20)...;
I have 3 related tables and I want to sort the data by programme_title which is in the programmes table. This method in my controller works and I can dump the new array sorted by programme_title.
However, when I print this in my view (vue.js), the array is sorted by default, id again. Any reason for this?
return Inertia::render('Applications/Edit', [
$programmes = AppProgrammes::with(['programmeInstances', 'programmes'])
->get()
->sortBy(function($programmes) {
return $programmes->programmes->programme_title;
}),
'programmes' => $programmes,
]);
This happened once to me, the "issue" is that when you get the final result in javascript, surely it is a JSON, and it is automatically sorted by index (by javascript, that is the unchangeable behavior of Javascript related to JSON), you cannot have a "custom sort"... so you will have to find another solution for this...
I know it is really frustrating, but it is the only way...
If you dump your result, I think I could try to help you more.
For fun I am trying to figure out different ways of doing things in PHP & Laravel (the version of Laravel I am using is 5.8.37) and I am having trouble when I use the 'only' arrow function (->only) on a request object variable.
For example, if I do the following to store only certain fields from my request like so:
$imageRequest = ($request->only('image_file', 'filename'));
This ends up working out well to get the two inputs/data I require, but the result is stored in an array, which is where the problem I am trying to understand how to overcome arises.
After storing just the values I need, I would like to pass $imageRequest to a function, and in that function I would like to check if $imageRequest->hasFile, as well as accessing the file from it like so:
if($imageRequest->hasFile('image_file')){
...
$image = $imageRequest->file('image_file');
...
}
I have tried to convert the results of the ->only array to an object but I am still unable to utilize ->hasFile or ->file. I know that these arrow functions are meant to be used with request objects.
Is it possible to achieve what I am trying to do with ->only (limiting what inputs/data I choose to include) but maintaining the functionality of ->hasFile and ->file?
I have a Laravel app where I am using a bit of code which feels really unintuitive.
In the code I return a list of objects ($occupied) which all have the the column 'property'. I then go on to create an array of the list of objects 'property's ($occupiedproperty) just to use it in a whereNotIn call.
if ($occupied = Residency::currentResidents()){
// Here is the pointless part //////
$occupiedproperty = array();
foreach ($occupied as $occ) {
array_push($occupiedproperty, $occ->property);
}
///////////////////////////////////
return Property::whereNotIn('id', $occupiedproperty)->get();
}
This code works fine to do the job but creating a new array when I already have a list of objects seems lazy. I tried looking at eloquent's documentation but I couldn't figure this out.
I need to be able to access the 'property' column of $occupied so I can run something like whereNotIn('id', $occupied->property)
Thank you
Can't test it right now, but this should work (it should work even without casting to array the $occupied collection):
$occupiedProperties = array_pluck((array)$occupied, 'property');
It uses the array_pluck() helper method: http://laravel.com/docs/4.2/helpers#arrays
I am editing somebody else's code and I am a PHP beginner.
I want to access the contents of a column called "email" in the database "tga_purchase_items" where the "id" of the row is "14". I want to save the output to a variable "$sp_email".
The code I have is thus:
$sp_email = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
The variable is coming out empty although the database field is definitely populated.
What am I doing wrong? I am not used to this "->" syntax at all.
The syntax used in your database leans towards Codeigniter and active record.
If this is the case, the following code will retrieve the email column.
$result_set = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
$result_object = $result_set->row();
$sp_email = $result_object->email;
What you're missing is retrieving the data from the result set which you get. The get() functions return a result set which is similar to the native MySQLi Result class. This means that you need to retrieve the data you want from this set. There are various functions available in CodeIgniter to do this, namely row(), row_array(), result() and result_array() which you can read about on their manual page.
I haven't done too much with CodeIgniter and the Active Record (I prefer Zend Framework) but I think something like this would do the trick:
$this->db->select('email');
$sp_email = $this->db->get_where('tga_purchase_items', array('id' => '14'));
Look at this for a more detailed explanation: http://codeigniter.com/user_guide/database/active_record.html