Get patents data data only child has empty data in relationship? - laravel

I have a collection like the below. I need to fetch those users, whom does not uploaded documents. Now from the relationship, How can I fetch this?
[0] => Array
(
[id] => 2
[user_id] => 8
[referred_by] =>
........
[user_documents] =>
)
[1] => Array
(
[id] => 3
[user_id] => 9
[referred_by] =>
[reference_id] => NM1100008
.......
[user_documents] => Array
(
[id] => 1
[customer_id] => 3
[doc_type] => 1
[document] => N8xFORsPpkTayTQ9Ihyz0ly7QM62TJHxvHhSFQSN.png
[admin_comment] =>
[approve_status] => approve
[other_gov_id] =>
[doc_no] => 1
[created_at] => 2020-04-04 12:39:12
[updated_at] => 2020-04-06 13:57:01
[deleted_at] =>
)
)
I want to get rows only when user_documents is empty.. To get those users who does not uploaded documents to the system.
[user_documents] =>
Thanks in advance.

You can use laravel querying-relationship-absence
$users= User::doesntHave('user_documents')->get();

Depending on how you have setup your relations, something like this should work:
User::doesntHave('documents')->get();

Related

How to return array instead of object in laravel 5

I have use in model .
$data = DB::select('select * from users');
return $data;
But in controller I have got like this.
Array
(
[0] => stdClass Object
(
[Id] => 10
[Name] => Sachin
[Gender] => M
)
[1] => stdClass Object
(
[Id] => 12
[Name] => Sourav
[Gender] => M
)
)
But I want Like this
Array
(
[0] => Array
(
[Id] => 10
[Name] => Sachin
[Gender] => M
)
[1] => Array
(
[Id] => 12
[Name] => Sourav
[Gender] => M
)
)
I already tried using get() and toArray() but its giving call to unknown member function.
Anyone have an idea how to resolved this issue.
You just use the laravel standard function
public function testData(){
$data = YOUR_MODEL_NAME::all()->toArray();
echo "<pre>";print_r($data);
}
UPDATED:
public function testData(){
$data = DB::select('select * from users');
$data = collect($data)->map(function($x){ return (array) $x; })->toArray();
echo "<pre>";print_r($data);
}
Output will be :
UPDATED:
Array
(
[0] => Array
(
[email] => bmc-admin#gmail.com
[member_code] => BMC0001ADMIN
)
[1] => Array
(
[email] => bmc-analyst#gmail.com
[member_code] => BMC0003ADMIN
)
[2] => Array
(
[email] => bmc-product-manager#gmail.com
[member_code] => BMC0002ADMIN
)
)
Array
(
[0] => Array
(
[id] => 1
[drewry_user_id] => 1
[email] => bmc-admin#gmail.com
[name] => BMC Administrator
[mobile] =>
[member_name] => BMC-Admin
[member_code] => BMC0001ADMIN
[shipper_size] => Large
[fk_role_id] => 1
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-12 04:56:31
[updated_by] =>
[deleted_at] =>
)
[1] => Array
(
[id] => 6
[drewry_user_id] => 3
[email] => bmc-analyst#gmail.com
[name] => BMC Analyst
[mobile] =>
[member_name] => BMC-Analyst
[member_code] => BMC0003ADMIN
[shipper_size] => Large
[fk_role_id] => 3
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-26 10:55:41
[updated_by] =>
[deleted_at] =>
)
[2] => Array
(
[id] => 9
[drewry_user_id] => 2
[email] => bmc-product-manager#gmail.com
[name] => BMC Product Manager
[mobile] =>
[member_name] => BMC-Product-Manager
[member_code] => BMC0002ADMIN
[shipper_size] => Large
[fk_role_id] => 2
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-14 09:09:10
[updated_by] =>
[deleted_at] =>
)
[3] => Array
(
[id] => 19
[drewry_user_id] => 4
[email] => bmc-customer1#gmail.com
[name] => User 1
[mobile] =>
[member_name] => User-1
[member_code] => BMC0004CUSTOMER1
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-27 11:31:20
[updated_by] =>
[deleted_at] =>
)
[4] => Array
(
[id] => 20
[drewry_user_id] => 5
[email] => bmc-customer2#gmail.com
[name] => User 2
[mobile] =>
[member_name] => User-2
[member_code] => BMC0004CUSTOMER2
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:34:08
[updated_by] =>
[deleted_at] =>
)
[5] => Array
(
[id] => 21
[drewry_user_id] => 6
[email] => bmc-customer3#gmail.com
[name] => User 3
[mobile] =>
[member_name] => User-3
[member_code] => BMC0004CUSTOMER3
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:35:25
[updated_by] =>
[deleted_at] =>
)
[6] => Array
(
[id] => 22
[drewry_user_id] => 7
[email] => bmc-customer4#gmail.com
[name] => User 4
[mobile] =>
[member_name] => User-4
[member_code] => BMC0004CUSTOMER4
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:35:54
[updated_by] =>
[deleted_at] =>
)
[7] => Array
(
[id] => 23
[drewry_user_id] => 8
[email] => bmc-customer5#gmail.com
[name] => User 5
[mobile] =>
[member_name] => User-5
[member_code] => BMC0004CUSTOMER5
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-25 15:35:46
[updated_by] =>
[deleted_at] =>
)
)
I hope it will work to you.
If you just need to get raw nested arrays of data, without using Eloquent:
$data = DB::connection()->getPdo()
->query("SELECT * FROM users")
->fetchAll(\PDO::FETCH_ASSOC);
This should be much more efficient than the current accepted answer.
toArray is a model method of Eloquent, so you need to a Eloquent model, try this:
User::all()->toArray();
http://laravel.com/docs/eloquent#collections

How to join two collections (custom collection with customer collection) in magento?

I am creating a module in which I tried to JOIN two Collection.
1st is custom collection ('custom/custom') and second is ('customer/customer'), I want to filter the collection based on a custom customer attribute 'pushnotification'.
What so far I did :
$collection = Mage::getModel('custom/custom')->getCollection();
$collection->getSelect()->join(
array('cust' => 'customer_entity'),
'cust.entity_id = main_table.user_id');
Output
[id] => 1
[user_id] => 1
[registration_id] => 123456
[device_type] => 0
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[website_id] => 1
[email] => deepak#gmail.com
[group_id] => 1
[increment_id] =>
[store_id] => 1
[created_at] => 2015-10-03 13:26:37
[updated_at] => 2015-10-22 06:25:33
[is_active] => 1
[disable_auto_group_change] => 0
My query : How can I JOIN custom ('custom/custom') collection with customer ('customer/customer') collection filter based on attribute pushnotification?

How to collect such info in magento listing page?

I need the catalog_product_entity_media_gallery table value data only on listing page.
When i am going to listing page and use the print_r($_productCollection);
$_productCollection on listing page returns me a array like this.
[_resourceName:protected] => catalog/product
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/product_collection
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[entity_id] => 160
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => 104580
[has_options] => 0
[required_options] => 0
[created_at] => 2014-12-01 10:40:25
[updated_at] => 2014-12-01 11:51:15
[cat_index_position] => 1
[price] => 4.4900
[tax_class_id] => 4
[final_price] => 4.4900
[minimal_price] => 4.4900
[min_price] => 4.4900
[max_price] => 4.4900
[tier_price] =>
[name] => E Shisha E Liquid eKaiser *Menthol Flavour* 10ml Platinum Bottle Refill for Rechargeable E cigarette and E Shisha
[msrp_enabled] => 1
[msrp_display_actual_price_type] => 1
[thumbnail] => amazon_images/104580.jpg
[small_image] => amazon_images/104580.jpg
[image_label] =>
[small_image_label] =>
[thumbnail_label] =>
[url_key] => e-shisha-e-liquid-ekaiser-menthol-flavour-10ml-platinum-bottle-refill-for-rechargeable-e-cigarette-and-e-shisha
[short_description] => The eKaiser Platinum e Liquid range is made specifically for Cigarette smokers.
I need the only [small_image] value, which returns me only the amazon_images/104580.jpg
If any One have any idea, how to do it
Please help me.
Thank you
And also you can do like this :
$product = Mage::getModel('catalog/product')->load($_product->getId());
foreach ($product->getMediaGallery('images') as $image) {
...
}
You can do like this on listing page.
$product = Mage::getModel('catalog/product')->load($_product->getId());
foreach ($product->getMediaGalleryImages() as $image) {
var_dump($image->getUrl());
}
A for loop in listing with getModel will slow down your listing page considerably.
With listing page you should only use flat tables for collection.
So you need to make sure that small image is populated in flat table.

Magento solr search - how to add custom field to product search index

I have enterprise magento 1.10 and I need to add custom field to solr index in order to to have better search results. This custom field should contain category names for indexed product.
I need to know which part of magento I need to amend. I modified method Enterprise_Search_Model_Resource_Index::_getCatalogCategoryData which actualy produces some category related data but my new field doesnt appear in message to sorl.
Have I miss something?
Also I have some notes here, maybe it would someone help http://docs.nostresscommerce.com:8090/pages/viewpage.action?pageId=2490371
My output of rewrited Enterprise_Search_Model_Resource_Index::_getCatalogCategoryData, and I need category_name_level_* in solr.
[result] => Array
(
[12227] => Array
(
[#categories] => Array
(
[0] => 2
[3] => 3235
[7] => 2765
[10] => 6672
)
[#show_in_categories] => Array
(
[1] => 6671
[2] => 6670
[4] => 3224
[5] => 3199
[6] => 3191
[8] => 2761
[9] => 2760
)
[#position_category_2] => 1
[#position_category_6671] => 120000
[#position_category_6670] => 120000
[#position_category_3235] => 0
[#position_category_3224] => 150000
[#position_category_3199] => 150000
[#position_category_3191] => 120000
[#position_category_2765] => 1
[#position_category_2761] => 250001
[#position_category_2760] => 250001
[#position_category_6672] => 0
[#visibility] => 4
[#category_name_level_1] => Array
(
[0] => BCC Root Category
)
[#category_name_level_4] => Array
(
[0] => Huishouden
[1] => Top wasmachines
[2] => Accessoires wasmachine
)
[#category_name_level_3] => Array
(
[0] => Beste Koop Shop
[1] => Top
[2] => Wasmachine
)
[#category_name_level_2] => Array
(
[0] => Acties
[1] => Speciale informatie pagina's
[2] => Huishouden
)
[#category_name_level_5] => Array
(
[0] => Wasmachines
)
)
)
Thanks a lot,
Jaro.
I found a solution
class Xyz_Solrsearch_Model_Resource_Engine extends Enterprise_Search_Model_Resource_Engine {
public function __construct()
{
$this->_advancedDynamicIndexFields[] = '#category_name_level_';
parent::__construct();
}
}

multi-level comments error when deleting items from database

I have implemented a multi-level comment system where comments and their replies are pulled out of a SQL database and stored (via a PHP routine) in an array. Here is an example of the data structure of a comment which has 2 nested replies (main comment -> [child] reply -> [child] reply to reply):
Array ( [0] => stdClass Object ( **[self]** => stdClass Object ( [id] => 1 [page_id] => 47iNetBet [parent_id] => 0 [name] => adam 1 [email] => web#web.de [url] => [comment] => main comment [dt] => 2012-02-23 13:05:13 [ip] => 127.0.0.1 ) **[childs]** => Array ( [2] => stdClass Object ( [self] => stdClass Object ( [id] => 2 [page_id] => 47iNetBet [parent_id] => 1 [name] => adam 2 [email] => web#web.de [url] => [comment] => 1st reply [dt] => 2012-02-23 13:05:25 [ip] => 127.0.0.1 ) **[childs]** => Array ( [3] => stdClass Object ( [self] => stdClass Object ( [id] => 3 [page_id] => 47iNetBet [parent_id] => 2 [name] => adam 3 [email] => web#web.de [url] => [comment] => 2nd reply [dt] => 2012-02-23 13:05:35 [ip] => 127.0.0.1 ) **[childs]** => Array ( ) ) ) ) ) ) )
Each comment's parent_id refers to the id of its parent. If parent_id is zero then it's a main comment. This works fine - so far I have been able to add as many nested comments as I want without any problems. But it seems that as soon as I delete something from my database or even delete all the comments and then start adding new comments (even the exact same comments ids, parent_ids etc.) at some point the structure gets messed up. Here is the same example after deleting comments from database and repopulating database with the same comments data:
Array ( [0] => stdClass Object ( **[self]** => stdClass Object ( [id] => 1 [page_id] => 47iNetBet [parent_id] => 0 [name] => adam 1 [email] => web#web.de [url] => [comment] => main comment [dt] => 2012-02-23 13:05:13 [ip] => 127.0.0.1 ) **[childs]** => Array ( [2] => stdClass Object ( [self] => stdClass Object ( [id] => 2 [page_id] => 47iNetBet [parent_id] => 1 [name] => adam 2 [email] => web#web.de [url] => [comment] => 1st reply [dt] => 2012-02-23 13:14:40 [ip] => 127.0.0.1 ) **[childs] => Array (EMPTY???) ) ) )** [1] => stdClass Object ( [childs] => Array ( [3] => stdClass Object ( [self] => stdClass Object ( [id] => 3 [page_id] => 47iNetBet [parent_id] => 2 [name] => adam 3 [email] => web#web.de [url] => [comment] => 2nd reply [dt] => 2012-02-23 13:14:47 [ip] => 127.0.0.1 ) [childs] => Array ( ) ) ) ) )
As you can see in the latter example, the second child (main comment -> reply -> reply to reply) is an empty array. What should be in this array is now put at the end as some sort of independent/incomplete comment which breaks the flow of the display. If I drop the "comments" table and start a new one everything seems to work fine again until I start deleting stuff again.
I dont know how to explain, but this is weird. I cant imagine that there is something wrong with the code as everything seems to work fine otherwise. Could this have to do anything with the database itself? Maybe someone had a similar problem? Maybe someone could point me in the right direction. I dont even know where to start looking.
Any help, suggestions would be much appreciated.
Problem solved. It was indeed an error within the code. The records in the database need to be fetched in a certain order (sorted by parent_id in ascending order). It was as simple as that.

Resources