Laravel: nested "with()" function - laravel

I want to retrieve data from three tables: courses, competencies and competency_standards. The following query almost works, but it does not bring back the associated competency_standards table data.
$coursesAndComps = Course::with(
array('competencies' => function($query)
{
Competency::with('competency_standards');
})
)->get()->toArray();
Where the competencies table is linked to the courses table (competencies.course_id = course.id) and the competency_standards table links to the competencies table (competency_standards.competencey_id = competency.id).
The array returned looks like this:
Array
(
[0] => Array
(
[id] => 1
[name] => the first course
[competencies] => Array
(
[0] => Array
(
[id] => 9
[course_id] => 1
[name] => first course comp 1
)
[1] => Array
(
[id] => 10
[course_id] => 1
[name] => first course comp 2
)
)
)
)
but within the competencies array, I was hoping to find another array called competency_standards like this:
...
[0] => Array
(
[id] => 9
[course_id] => 1
[name] => first course comp 1
[competency_standards] => Array
(
[0] => Array
(
[id] => 22
[competency_id] => 9
[name] => standard foo
)
[1] => Array
(
[id] => 23
[competency_id] => 9
[name] => standard bar
)
)
)
...
Is this possible? Am I going about this the wrong way?

It should be possible to use:
$coursesAndComps = Course::with('competencies', 'competencies.standards')
->get()->toArray();
but of course you need to have defined standards relationship in Competency model to link Competency with Standard

Related

I am found this type of result where User_id and full_name print everytime. full_name print one time and related multiple data shows in a single array

$nearProviderdata = DB::table('users','category')
->leftJoin('service_provider_rating', 'users.id', '=', 'service_provider_rating.rating_receiver_user_id')
->leftJoin('service_provider_category', 'users.id', '=', 'service_provider_category.user_id')
->leftJoin('category', 'category.id', '=', 'service_provider_category.category_id')
->select('users.id as user_id','users.full_name','category.id as category_id','category.title')
->distinct()->get()->groupBy('user_id');
I am receiving the below response from the above code.
[0] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 1
[title] => Painter
)
[1] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 2
[title] => Home Cleaner
)
[2] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 3
[title] => Electrician
)
I want the result something like as below
[0] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
['category'] => [ category_id => 1,title => Painter, category_id => 2, title => Home Cleaner, category_id => 3, title => Electrician]
)
It's laravel version 5.7. I have tried group by also to get the result but it is showing only 1 row and 1 category data. So i am using the subquery to get the data. Please let me know if i am doing something wrong in it.
I think you don't need to join that much with raw query
better use models for this purpose
Simplified example:
$nearProviderdata = User::with(
'categories',
'serviceProviderRating',
'serviceProviderCategory'
)->get();
You can also add select in subquery and map response

how to submit bulk record of following array using eloquent or query builder

I have sale product form where user will receive cash against product at cash counter, and products could be many. How to submit that record at once using eloquent and query builder with following array pattern
Array
(
[_token] => jsVji1V6cTt5XmD2XgWyA7GEH5lvPlYbYWtUPIrt
[productId] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 1
)
[amountReceived] => Array
(
[0] => 100
[1] => 500
)
[deliveryLocation] => Array
(
[0] => london
[1] => new york
)
)
Simply create an associative array like below and insert:
for($i=0;$i<count(data['productId']);$i++){
$temp[]=array('productId'=>$data['productId'][$i], 'Quantity'=>$data['Quantity'][$i],'amountReceived'=>$data['amountReceived'][$i]);
}
Model::insert($temp);
Docs are here

how to get the values of multidimensional array

how to get the values of multidimensional array.
code//
Array
(
[0] => Array
(
[City] => Array
(
[title] => Nagpur
[id] => 20299
)
[Branch] => Array
(
[0] => Array
(
[id] => 8
[country_id] => 41
[state_id] => 102
[city_id] => 20299
[title] => Geotech Services Ltd.
)
)
)
[1] => Array
(
[City] => Array
(
[title] => kolapur
[id] => 20300
)
)
)
Here I want to get the only values if cities(title) so plz tel me how to fetch in cakephp..
Thanks in advance
Thus you finding as recursive so all related data is fetched with City.
To disable this recursion, you need to set $recursive = -1;
You can do this by many ways--
place $this->City->recursive = -1; before find operation
place recursive => -1 on find overation
Globally set public $recursive = -1; on AppModel.php
And if you read from this array its simple and basic programming operation...
foreach($cities as $city){
echo $city['City']['title']
}
using cakePHP what you're trying to do is usually achieved using Hash
Assuming $your_array contains the data, then you have to do:
$cities = Hash::extract($your_array, '{n}.City.title');

Transforming multidimensional array data returned from database

I am having problems with working with multidimensional array data.
I have 2 tables (maincategories and categories) which I joined.
Table Maincategories:
id, maincategory
Table Categories:
id, maincategory_id, category
$this->db->select('m.id, m.name_en AS maincategory')
->select('c.name_en AS category')
->from('categories AS c')
->join('maincategories AS m', 'c.maincategory_id=m.id', 'left');
$query = $this->db->get();
This results in all 4 categories being listed as below.
[3] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Business Sales
)
[4] => stdClass Object
(
[id] => 1
[maincategory] => Accommodation
[category] => Camping
)
[5] => stdClass Object
(
[id] => 6
[maincategory] => Accommodation
[category] => Hostels
)
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Career Events
)
4 unique categories are in the list while 2 of them belong to maincategory: Accommodaton and the other 2 categories belong to maincategory: Career. What I wanted to achieve is that each maincategory is only listed ONCE and the categories shall fall under that main_category. Something like this:
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => array(Career Events, Business Sales, .....)
)
[7] => stdClass Object
(
[id] => 7
[maincategory] => Accommodation
[category] => array(Hostels, Camping, .....)
)
The array dump will maybe/probably look different in its end result but I hope you understand what I am trying to achieve. I read quite a bit about transforming multidimensional arrays. I just can't get my head around it yet. Thanks a lot for any direction!
Try like this:
function fetch_all(){
$data = array();
$first = $this->db->select('id, maincategory')->get('maincategory')->result_array();
if( is_array( $first ) && count( $first ) > 0 ){
foreach( $first as $key => $each ){
$data[$key]['category_id'] = $each['id']; #main category id
$data[$key]['maincategory'] = $each['id']; #main category name
$second = $this->db->select('category')->where_in('maincategory_id', $each['id'])->get('categories')->result_array();
$data[$key]['category'] = $second; #all sub-category(s) array
}
}
}
instead of using so many db calls, try using the underscore.php library http://brianhaveri.github.io/Underscore.php/
I've outlined a few examples of how to use it for this case here: http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Edit

Getting a list of magento stores

How can I get a list of store groups under a website in Magento and then a list of stores from that store group?
Try this to get the objects directly
Mage::app()->getWebsites(); < in file > app/code/core/Mage/Core/Model/App.php:920
Mage::app()->getStores(); < in file > app/code/core/Mage/Core/Model/App.php:834
iterate over to get the needed scope of one specific website or store
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
//$store is a store object
}
}
}
For the future if you have similar questions here's how i discovered those answers within 60 seconds. First i grep for method names or similar method names with space before method name to see where the methods are defined
grep ' getStores' app/code -rsn
grep ' getWebsites' app/code -rsn
Second step is grep for usage samples to see how they are meant to use by core developers. For that i add >methodName to grep and this gives me list of files where this method is called and this will give us place to look for examples:
grep '>getWebsites' app/code -rsn
Anton's answer, while correct, may be re-inventing the wheel just a bit. There is already a facility in the Magento Core to retrieve this sort of data.
You can retrieve a list of all websites, and their "children" using this:
Mage::getSingleton('adminhtml/system_store')->getStoresStructure()
You can also pass an array of websiteIds, storeIds, or storeGroupIds to the function, to filter the list:
public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())
Example output:
Array
(
[1] => Array
(
[value] => 1
[label] => Main Website
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => Madison Island
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => English
)
[2] => Array
(
[value] => 2
[label] => French
)
[3] => Array
(
[value] => 3
[label] => German
)
)
)
)
)
)
There is a similar one used to populate the "Store Scope" dropdowns and multi-selects all across the admin section.
Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true)
Array
(
[0] => Array
(
[label] => All Store Views
[value] => 0
)
[1] => Array
(
[label] => Main Website
[value] => Array
(
)
)
[2] => Array
(
[label] => Madison Island
[value] => Array
(
[0] => Array
(
[label] => English
[value] => 1
)
[1] => Array
(
[label] => French
[value] => 2
)
[2] => Array
(
[label] => German
[value] => 3
)
)
)
)
To discover this, I located a multi-select on the Admin that has the data I wanted, then I turned on template hints to find out which block class was responsible for rendering it: Mage_Adminhtml_Block_Cms_Page_Edit_Form. Knowing this, I found the class in the codebase,(app/code/core/Mage/Adminhtml/Block/Cms/Block/Edit/Form.php) and located the part that creates the input by searching for its label ("Store View"). This showed me how the input's values were being provided:
$field =$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
The Mage::getSingleton('adminhtml/system_store') points to the class Mage_Adminhtml_Model_System_Store, where I found a variety of similar methods that can also be useful. Have a look for yourself.

Resources