Laravel Eager Loading through an intermediate Model - laravel

In one of my Controllers, I have the following:
return Lot::with(array('region', 'territory', 'manager')) -> get();
This works perfect, and returns the following:
Array
(
[0] => stdClass Object
(
[id] => 1
[region_id] => 3
[territory_id] => 2
[state_id] => 5
[manager_id] => 2
[lot_num] => 0
[lot_type] => managed
[name] => Some Name
[address_1] => Some Address
[address_2] =>
[address_3] =>
[city] => Some City
[zip] => 00000
[opened_at] =>
[deleted_at] =>
[created_at] => 2014-11-06 00:49:39
[updated_at] => 2014-11-06 00:49:39
[region] => stdClass Object
(
[id] => 3
[name] => Corporate
[deleted_at] =>
[created_at] => 2014-11-06 00:49:39
[updated_at] => 2014-11-06 00:49:39
)
[territory] => stdClass Object
(
[id] => 2
[name] => Corporate
[deleted_at] =>
[created_at] => 2014-11-06 00:49:39
[updated_at] => 2014-11-06 00:49:39
)
[manager] => stdClass Object
(
[id] => 2
[email] => test#tester.com
[active] => 1
[last_login] =>
[created_at] => 2014-11-06 00:49:39
[updated_at] =>
[deleted_at] =>
)
)
)
My 'manager' method in the Lot class has the following relationship:
return $this -> belongsTo('User');
My User class has the following method on it:
public function profile(){
return $this -> hasOne('Profile');
}
Now, finally for my question :) Is it possible to eager load the Profile for this user through the eager loading I'm doing on my Lot class in the first code snip? What I'm trying to accomplish is to have my 'manager' object in the return look something like this:
[manager] => stdClass Object
(
[id] => 2
[email] => test#tester.com
[active] => 1
[last_login] =>
[created_at] => 2014-11-06 00:49:39
[updated_at] =>
[deleted_at] =>
[profile] => stdClass Object
(
[id] => 5
[first_name] => Test
[last_name] => Tester
...
)
)

Use "dot" notation to eager load nested relationships:
return Lot::with(array('region', 'territory', 'manager.profile'))->get();

Related

Laravel belongsToMany.belongsTo nested relationship query where child column = parent id

I have two models (and database tables with Laravel naming conventions) and I am trying to Eager Load the Collections with Equipment and Equipment.collection_meters where collection_meter equals Collection.id.
How can I access the collections.id in the child relationship $query->with?
or
How can I access the equipment.pivot.collection_id in the child relationship $query->with?
Here are my models ...
class Collection extends Model {
public function equipment() {
return $this->belongsToMany(Equipment::class, 'collection_equipment', 'collection_id', 'equipment_id')->withTimestamps();
}
}
class CollectionMeter extends Model {
public function equipment() {
return $this->belongsTo(Equipment::class);
}
}
class Equipment extends Model {
public function collection_meters() {
return $this->hasMany(CollectionMeter::class)->latest(); // ->currentStatus('active')
}
}
equipment_id column is in the collection_meters table
Here is the code I have tried but I just can't seem to get the Collection.id in the child relationship query. If I leave out the child $query->where, I get ALL of the equipment.equipment_meters.
$collections = Collection::with([
'equipment',
'equipment.collection_meters' => function($query) {
$query->where('collection_meters.collection_id', '=', 'collections.id');
}
])
->get();
I have tried these in place of 'collections.id' with the same result
'equipment.pivot.collection_id'
'equipment.collection_id'
I do see this in the [equipment] => Array, can I access [collection_id] => 2 in the $query->where somehow?
[pivot] => Array
(
[collection_id] => 2
[equipment_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-17 00:17:00
)
Results without the $query->with ([collection_meters] has ALL)
Array
(
[0] => Array
(
[id] => 2
[name] => Name of Collection
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[date_time] => 2019-09-16 04:41:26
[reconciliation_id] =>
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-16 23:44:43
[updated_at] => 2019-09-18 17:32:53
[equipment] => Array
(
[0] => Array
(
[id] => 1
[name] => Name of Equipment
[ims_identifier] => AFA-64
[operator_identifier] => 073076-01
[equipment_model_id] => 1
[operator_id] => 1
[location_id] => 4
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-07-17 13:17:28
[updated_at] => 2019-08-14 00:04:07
[pivot] => Array
(
[collection_id] => 2
[equipment_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-17 00:17:00
)
[collection_meters] => Array
(
[0] => Array
(
[id] => 17
[equipment_meter_id] => 1
[value] => 0.25
[gross] => 25.00
[refund] => 2.00
[test] => 2.00
[reading_start] => 72985
[reading_end] => 73085
[collection_id] => 3
[equipment_id] => 1
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-17 18:23:41
[updated_at] => 2019-09-17 22:56:50
)
[1] => Array
(
[id] => 9
[equipment_meter_id] => 1
[value] => 0.25
[gross] => 24.00
[refund] => 2.00
[test] => 1.00
[reading_start] => 72885
[reading_end] => 72985
[collection_id] => 2
[equipment_id] => 1
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-18 17:32:53
)
[2] => Array
(
[id] => 1
[equipment_meter_id] => 1
[value] => 0.25
[gross] => 282.50
[refund] => 3.26
[test] => 0.00
[reading_start] => 71755
[reading_end] => 72885
[collection_id] => 1
[equipment_id] => 1
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-11 22:38:31
[updated_at] => 2019-09-11 22:38:31
)
)
)
Results with the $query->with ([collection_meters] is empty)
Array
(
[0] => Array
(
[id] => 2
[name] => Name of Collection
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[date_time] => 2019-09-16 04:41:26
[reconciliation_id] =>
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-16 23:44:43
[updated_at] => 2019-09-18 17:32:53
[equipment] => Array
(
[0] => Array
(
[id] => 1
[name] => Name of Equipment
[ims_identifier] => AFA-64
[operator_identifier] => 073076-01
[equipment_model_id] => 1
[operator_id] => 1
[location_id] => 4
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-07-17 13:17:28
[updated_at] => 2019-08-14 00:04:07
[pivot] => Array
(
[collection_id] => 2
[equipment_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-17 00:17:00
)
[collection_meters] => Array
(
)
) ...
Results expected ([collection_meters] has [id] => 2 equals to [collection_id] => 2)
Array
(
[0] => Array
(
[id] => 2
[name] => Name of Collection
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[date_time] => 2019-09-16 04:41:26
[reconciliation_id] =>
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-16 23:44:43
[updated_at] => 2019-09-18 17:32:53
[equipment] => Array
(
[0] => Array
(
[id] => 1
[name] => Name of Equipment
[ims_identifier] => AFA-64
[operator_identifier] => 073076-01
[equipment_model_id] => 1
[operator_id] => 1
[location_id] => 4
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-07-17 13:17:28
[updated_at] => 2019-08-14 00:04:07
[pivot] => Array
(
[collection_id] => 2
[equipment_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-17 00:17:00
)
[collection_meters] => Array
(
[0] => Array
(
[id] => 9
[equipment_meter_id] => 1
[value] => 0.25
[gross] => 24.00
[refund] => 2.00
[test] => 1.00
[reading_start] => 72885
[reading_end] => 72985
[collection_id] => 2
[equipment_id] => 1
[operator_id] => 1
[location_id] => 4
[account_id] => 1
[created_by_id] => 1
[updated_by_id] => 1
[created_at] => 2019-09-17 00:17:00
[updated_at] => 2019-09-18 17:32:53
)
)
)
What kind of relationship are you using? "Many to Many" or "One to Many"? If you have three tables, one of them "pivot", you must define in both Models the relationship as belongsToMany:
class Collection extends Model {
public function equipments() {
return $this->belongsToMany(Equipment::class, 'collection_equipment',
'collection_id', 'equipment_id')->withTimestamps();
}
}
What is the name of the pivot table? collection_equipment ? How is your structure?
collections => collection_equipment <= equipments
class Equipment extends Model {
public function collections() {
return $this->belongsToMany(Collection::class, 'collection_equipment',
'equipment_id', 'collection_id')->withTimestamps();
}
}
Where is the collection_meters column? In the equipments table?
Then, if you want to get nested table information, just:
$collections = Collection::with('equipments')->get();
But sometimes you want to get information using a Pivot Model:
class CollectionEquipment extends Model {
protected $table = 'collection_equipment';
public function collection() {
return $this->belongsTo(Collection::class, 'collection_id');
}
public function equipment() {
return $this->belongsTo(Equipment::class, 'equipment_id');
}
}
Then, just:
$collections = CollectionEquipment::with(['equipment', 'collection'])
->get();
Since you have already defined collection_id and equipment_id in all Model classes, you don't need to map them in your query as you would using 'join'

How to insert multiples arrays with multiples values in laravel by query builder

lets suppose i want to insert an array which container following data;
here is an array,
Array
(
[0] => 1
[1] => ::1
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 0
[1] => 1
)
[4] => 2018-10-11
[5] => 2018-10-31
[6] => 0
[7] => 0
[8] => 2018-10-11
[9] => 10:38:36
)
but i am stuck how to insert this array with single foreach loop
and i am using this code but not working for multi arrays with different values
$count_row = 0;
foreach($products as $row){
$count_row ++;
//Set Field data according to table column
$data = array(
'user_id' => $user_id,
'ip_address' => $ip_address,
'product_id' => $row,
'page_id' => $row[$count_row],
'start_date' => $start_date,
'end_date' => $end_date,
'type' => $type,
'status' => $status,
'created_date' => $created_date,
'created_time' => $created_time,
);
//Query For Inserting Data
$query = DB::table('tbl_product_advertisements')
->insertGetId($data);
}
i found a solution, for inserting this type of array we have to use 2 loops,

how to access the array inside the array in codeigniter?

I am new to CI . I had a function to access a list of data as category. The function was like this:
public function get_category_tree()
{
$this->ci->db->where('is_display','1');
$query = $this->ci->db->get('product_categories');
if ($query->num_rows() > 0)
{
foreach($query->result() as $cat)
{
if($cat->parent_id=='0'){
//category
$categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => '');
}else{
//subcategory;
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
}
}
return $categories_arr;
}
return false;
}
this function was defined in general library and i accessed it from the controller like this:
$this->data['category_tree'] = $this->general->get_category_tree();
And this used to give me this result:
Array
(
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] =>
)
Array
(
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
(
[0] => Array
(
[id] => 78
[parent_id] => 32
[name] => Africa
)
[1] => Array
(
[id] => 79
[parent_id] => 32
[name] => Asia
)
[2] => Array
(
[id] => 80
[parent_id] => 32
[name] => Australia
)
[3] => Array
(
[id] => 81
[parent_id] => 32
[name] => Br Comm Other
)
[4] => Array
(
[id] => 82
[parent_id] => 32
[name] => Canada
)
[5] => Array
(
[id] => 83
[parent_id] => 32
[name] => Europe
)
[6] => Array
(
[id] => 84
[parent_id] => 32
[name] => Latin America
)
[7] => Array
(
[id] => 85
[parent_id] => 32
[name] => Middle East
)
[8] => Array
(
[id] => 86
[parent_id] => 32
[name] => Publications
)
[9] => Array
(
[id] => 87
[parent_id] => 32
[name] => Topical & Specialty
)
[10] => Array
(
[id] => 88
[parent_id] => 32
[name] => UK (Great Britain)
)
[11] => Array
(
[id] => 89
[parent_id] => 32
[name] => United States
)
[12] => Array
(
[id] => 90
[parent_id] => 32
[name] => Worldwide
)
)
)
Now i upgraded my php to 7+, now the line
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
did not work and gave php error and i simply removed the [] and it became
$categories_arr[$cat->parent_id]['subcat'] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
Now when i print_it it gives me this result:
Array
(
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] =>
)
Array
(
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
(
[id] => 90
[parent_id] => 32
[name] => Worldwide
)
)
Only the last array of the sub array. How can i solve it. I searched a lot about the upgrade and fix for it but couldn't garb anything useful. Can anyone please help. Thanks in advance
Because you define subcat as string by 'subcat' => '' in defining $categories_arr[$cat->id]. Use this:
public function get_category_tree()
{
$this->ci->db->where('is_display','1');
$query = $this->ci->db->get('product_categories');
if ($query->num_rows() > 0)
{
foreach($query->result() as $cat)
{
if($cat->parent_id=='0'){
//category
$categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => array());
}else{
//subcategory;
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
}
}
return $categories_arr;
}
return false;
}

How to get all order details including payment custmer and shipping details from order id Magento

I need to get all order details which includes all payment details, customer details, and shipping details or say all details of a order.
For this I have use a event in my config file
checkout_onepage_controller_success_action
and the function in my observer file is
public function getorderrealid($observer) {
print_r($observer->getData());
}
the function triggers properly, but it returns the following array
Array
(
[event] => Varien_Event Object
(
[_observers:protected] => Varien_Event_Observer_Collection Object
(
[_observers:protected] => Array
(
)
)
[_data:protected] => Array
(
[order_ids] => Array
(
[0] => 66
)
[name] => checkout_onepage_controller_success_action
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
[order_ids] => Array
(
[0] => 66
)
)
It gives only order id.
Please suggest how I can get all details of an order.
$order = Mage::getModel('sales/order')->load($orderid);
$orderData = $order->getData();
print_r($orderData);
EDIT
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
For credit card information
CC Last 4 number: $order->getPayment()->getCcLast4()
$order->getPayment()->getCcExpMonth();
$order->getPayment()->getCcExpYear();
when you print $orderData you can get following information like
Array
(
[entity_id] => 45
[state] => processing
[status] => pending
[coupon_code] => stickytest
[protect_code] => 1036b0
[shipping_description] => Voucher code discount
[is_virtual] => 0
[store_id] => 1
[customer_id] =>
[base_discount_amount] => -195.0000
[base_discount_canceled] =>
[base_discount_invoiced] =>
[base_discount_refunded] =>
[base_grand_total] => 0.0000
[base_shipping_amount] => 0.0000
[base_shipping_canceled] =>
[base_shipping_invoiced] =>
[base_shipping_refunded] =>
[base_shipping_tax_amount] => 0.0000
[base_shipping_tax_refunded] =>
[base_subtotal] => 177.2700
[base_subtotal_canceled] =>
[base_subtotal_invoiced] =>
[base_subtotal_refunded] =>
[base_tax_amount] => 0.0000
[base_tax_canceled] =>
[base_tax_invoiced] =>
[base_tax_refunded] =>
[base_to_global_rate] => 1.0000
[base_to_order_rate] => 1.0000
[base_total_canceled] =>
[base_total_invoiced] =>
[base_total_invoiced_cost] =>
[base_total_offline_refunded] =>
[base_total_online_refunded] =>
[base_total_paid] =>
[base_total_qty_ordered] =>
[base_total_refunded] =>
[discount_amount] => -195.0000
[discount_canceled] =>
[discount_invoiced] =>
[discount_refunded] =>
[grand_total] => 0.0000
[shipping_amount] => 0.0000
[shipping_canceled] =>
[shipping_invoiced] =>
[shipping_refunded] =>
[shipping_tax_amount] => 0.0000
[shipping_tax_refunded] =>
[store_to_base_rate] => 1.0000
[store_to_order_rate] => 1.0000
[subtotal] => 177.2700
[subtotal_canceled] =>
[subtotal_invoiced] =>
[subtotal_refunded] =>
[tax_amount] => 0.0000
[tax_canceled] =>
[tax_invoiced] =>
[tax_refunded] =>
[total_canceled] =>
[total_invoiced] =>
[total_offline_refunded] =>
[total_online_refunded] =>
[total_paid] =>
[total_qty_ordered] => 1.0000
[total_refunded] =>
[can_ship_partially] =>
[can_ship_partially_item] =>
[customer_is_guest] => 1
[customer_note_notify] => 1
[billing_address_id] => 89
[customer_group_id] => 0
[edit_increment] =>
[email_sent] => 1
[forced_shipment_with_invoice] =>
[gift_message_id] =>
[payment_auth_expiration] =>
[paypal_ipn_customer_notified] =>
[quote_address_id] =>
[quote_id] => 215
[shipping_address_id] => 90
[adjustment_negative] =>
[adjustment_positive] =>
[base_adjustment_negative] =>
[base_adjustment_positive] =>
[base_shipping_discount_amount] => 0.0000
[base_subtotal_incl_tax] => 195.0000
[base_total_due] =>
[payment_authorization_amount] =>
[shipping_discount_amount] => 0.0000
[subtotal_incl_tax] => 195.0000
[total_due] =>
[weight] => 1.0000
[customer_dob] =>
[increment_id] => 100000034
[applied_rule_ids] => 4
[base_currency_code] => AUD
[customer_email] => someone#somewhere.com
[customer_firstname] => billing[firstname]
[customer_lastname] => billing[lastname]
[customer_middlename] =>
[customer_prefix] =>
[customer_suffix] =>
[customer_taxvat] =>
[discount_description] => stickytest
[ext_customer_id] =>
[ext_order_id] =>
[global_currency_code] => AUD
[hold_before_state] =>
[hold_before_status] =>
[order_currency_code] => AUD
[original_increment_id] =>
[relation_child_id] =>
[relation_child_real_id] =>
[relation_parent_id] =>
[relation_parent_real_id] =>
[remote_ip] => 192.168.0.18
[shipping_method] => matrixrate_matrixrate_free
[store_currency_code] => AUD
[store_name] => Main Website
Main Store
English
[x_forwarded_for] =>
[customer_note] =>
[created_at] => 2012-11-19 10:53:11
[updated_at] => 2012-11-19 10:53:12
[total_item_count] => 1
[customer_gender] =>
[base_custbalance_amount] =>
[currency_base_id] =>
[currency_code] =>
[currency_rate] =>
[custbalance_amount] =>
[is_hold] =>
[is_multi_payment] =>
[real_order_id] =>
[tax_percent] =>
[tracking_numbers] =>
[hidden_tax_amount] => 17.7300
[base_hidden_tax_amount] => 17.7300
[shipping_hidden_tax_amount] => 0.0000
[base_shipping_hidden_tax_amnt] => 0.0000
[hidden_tax_invoiced] =>
[base_hidden_tax_invoiced] =>
[hidden_tax_refunded] =>
[base_hidden_tax_refunded] =>
[shipping_incl_tax] => 0.0000
[base_shipping_incl_tax] => 0.0000
[onestepcheckout_customercomment] => onestepcheckout_comments
[onestepcheckout_customerfeedback] => Google
[payment_authorization_expiration] =>
[forced_do_shipment_with_invoice] =>
[base_shipping_hidden_tax_amount] => 0.0000
)
Let me know if you have any query

Magento, populating checkboxes fields on an admin edit form

I have a form page where I am using
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
to create a list of checkboxes.
The problem is I can't figure out how to get them to populate when editing. Can anyone tell me how to do this?
I am using the checkboxes type so they display as a list instead of in separate rows in the form. If there is a way to create them as separate fields but all in one row I would love to know how.
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Then checkboxes with values "1" and "5" will be checked. For more details you can check lib/Varien/Data/Form/Element/Checkboxes.php
I also attached code or you can follow below link for more help
http://pastebin.com/hKMmryE9
Magento, populating checkboxes fields on an admin edit form
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Little improved and verified:
$fieldset->addField('payment_methods', 'checkboxes', array('label' => 'Payment Methods', 'name' => 'payment_methods[]',
'values' => array(
array('value'=>'1', 'label'=>'Cash'),
array('value'=>'2', 'label'=>'Paypal'),
array('value'=>'3', 'label'=>'Authorize.Net'),
array('value'=>'4', 'label'=>'Square'),
),
'required' => true,
'checked' => array('1','4'),
'disabled' => array('1'), ////if you want
));
Create $array like below
Array
(
[0] => Array
(
[value] => 1
[label] => Value 1
)
[1] => Array
(
[value] => 2
[label] => Value 2
)
[2] => Array
(
[value] => 3
[label] => Value 3
)
[3] => Array
(
[value] => 4
[label] => Value 4
)
[4] => Array
(
[value] => 5
[label] => Value 5
)
)
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => 'Select Value',
'name' => 'checkboxes[]',
'values' => $array,
'onclick' => "",
'onchange' => "",
'disabled' => false,
'after_element_html' => '',
'tabindex' => 1
));

Resources