Laravel chunk array returns empty - php

I've just started laravel and when I try to get values from database it's fine. But when I try to return the values, it's just empty. Here's the code:
public function index()
{
$collection = [];
Leg::chunk(200, function ($data) {
foreach ($data as $val) {
$collection[] = [
'fixture_id' => $val->fixture_id,
'ops_leg_id' => $val->ops_leg_id,
'designator' => $val->designator,
'operator' => $val->operator,
'flight_number' => $val->flight_number,
'dep_airport' => $val->dep_airport,
'arr_airport' => $val->arr_airport,
'ac_registration' => $val->ac_registration,
'ac_code' => $val->ac_code,
'sta_at' => $val->sta_at,
'std_at' => $val->std_at,
'ata_at' => $val->ata_at,
'atd_at' => $val->atd_at,
'deleted' => $val->deleted
];
}
});
return array($collection);
}
Edit: My output of the webpage is just [[]]

The inline function does not return anything
public function index()
{
Leg::chunk(200, function ($data) {
$collection = [];
foreach ($data as $val) {
$collection[] = [
'fixture_id' => $val->fixture_id,
'ops_leg_id' => $val->ops_leg_id,
'designator' => $val->designator,
'operator' => $val->operator,
'flight_number' => $val->flight_number,
'dep_airport' => $val->dep_airport,
'arr_airport' => $val->arr_airport,
'ac_registration' => $val->ac_registration,
'ac_code' => $val->ac_code,
'sta_at' => $val->sta_at,
'std_at' => $val->std_at,
'ata_at' => $val->ata_at,
'atd_at' => $val->atd_at,
'deleted' => $val->deleted
];
}
return array($collection);
});
}

Related

PHP Foreach only get one data

i want to loop all the data i call but that runs only one data
I use Codeigniter framework
Model:
$this->db->where('group_user', 'upt_perpus')->order_by('idmodul', 'DESC')->get('menu')->result_array();
Controller:
$data = array(
'about' => array(
'sejarah' => 'Sejarah',
'visi_misi' => 'Visi, Misi & Tujuan',
'struktur' => 'Struktur Organisasi'
),
'services' => array(),
'peraturan' => array(
'umum' => 'Umum',
'khusus' => 'Khusus',
'pelayanan_perpus' => 'Pelayanan Perpustakaan'
)
);
$konten = $this->kontens->getPerpus();
foreach ($konten as $key) {
$data['services'] = array(
$key['slug'] => $key['judul']
);
}
return $data;
If you want an associative array in $data['services'], you need to assign to a key of the array.
foreach ($konten as $key) {
$data['services'][$key['slug']] = $key['judul'];
}

Laravel Table Filter

So I have website based on Laravel framework. I have table and the filter for table to filter the items - http://prntscr.com/ccht0f.
When I filter them by anything all works good, but when I change the page filtering disapers from session. http://prntscr.com/cchutt - http://prntscr.com/cchuyo. Looks like pagination destroys all sessions.
My code:
public function filters(Request $request)
{
$filter_item_acquisition_forms = [null];
$filter_collections = [null];
$filter_origin_dates = [null];
$order_by = request()->query('order_by', 'id');
$dir = request()->query('dir', 'desc');
$storage_items = StorageItem::with(
'authorClassification',
'placeClassification',
'classificationValueOrigin',
'classificationValueOriginDate',
'classificationValueItemAcquisitionForm',
'classificationValueCollection',
'classificationValuesMaterials',
'classificationValuesTechnique',
'employeeClassificationsRestorers',
'userLastUpdatedBy.employeeClassification',
'userResponsibleUser.employeeClassification',
'attachments'
);
$storage_items = $storage_items->id($request->filter_id)
->acceptanceId($request->filter_acceptance_id)
->acceptanceNumber($request->filter_acceptance_number)
->acceptanceDate($request->filter_acceptance_date)
->mainInventoryNumber($request->filter_main_inventory_number_from, $request->filter_main_inventory_number_to)
->scientificInventoryNumber($request->filter_scientific_inventory_number)
->imageInventoryNumber($request->filter_image_inventory_number)
->itemCosts($request->filter_item_costs_from, $request->filter_item_costs_to)
->originDate($request->filter_origin_date)
->updatedAt($request->filter_updated_at_from, $request->filter_updated_at_to)
->addingDate($request->filter_added_from, $request->filter_added_to)
->itemAcquisitionForm($request->filter_item_acquisition_form)
->collection($request->filter_collection)
->employee($request->filter_employee)
->isInExposition($request->filter_is_in_exposition)
->isRestored($request->filter_is_restored)
->haveAttachments($request->filter_have_attachments)
->haveNotCosts($request->filter_have_not_costs)
->searchInField($request->filter_search_in_field, $request->filter_word_or_phrase);
$storage_items = $storage_items->orderBy($order_by, $dir)->paginate(20);
foreach ($storage_items as $storage_item) {
if ($storage_item->classificationValueItemAcquisitionForm != null) {
$filter_item_acquisition_forms[$storage_item->classificationValueItemAcquisitionForm->id] = $storage_item->classificationValueItemAcquisitionForm->value;
}
if ($storage_item->classificationValueCollection != null) {
$filter_collections[$storage_item->classificationValueCollection->id] = $storage_item->classificationValueCollection->value;
}
if (!is_null($storage_item->classificationValueOriginDate)) {
$filter_origin_date[$storage_item->classificationValueOriginDate->id] = $storage_item->classificationValueOriginDate->value;
}
}
$request->flash();
return view('panel.storage_items.filters')->with([
'storage_items' => $storage_items,
'current_order_query' => compact('order_by', 'dir'),
'employees' => [null] + EmployeeClassification::lists('employee', 'id')->toArray(),
'filter_what_to_look_for' => [
'storage-items' => trans('fields.storage_items'),
'storage-item-acceptances' => trans('fields.storage_items_acceptances'),
'archive-objects' => trans('fields.archive_objects'),
'archive-documents' => trans('fields.archive_documents'),
'archive-docs-acceptance-acts' => trans('fields.archive_documents_acceptance_acts')
],
'filter_item_acquisition_forms' => $filter_item_acquisition_forms,
'filter_collections' => [null] + Classification::findOrFail(2)->classificationValues()->lists('value', 'id')->toArray(),
//'filter_collections' => $filter_collections,
'filter_origin_dates' => $filter_origin_dates,
'filter_search_in_field' => [
trans('fields.storage_items') => [
'item_name' => trans('fields.item_name'),
'author' => trans('fields.author'),
'about_the_author' => trans('fields.about_author'),
'item_description' => trans('fields.item_description'),
'content_description' => trans('fields.content_description'),
'item_history' => trans('fields.item_history'),
'branding_notes' => trans('fields.branding_notes'),
'damage_characteristics' => trans('fields.damage_characteristics'),
'published' => trans('fields.published'),
'exhibited' => trans('fields.exhibited'),
'inquiries' => trans('fields.inquiries')
],
trans_choice('fields.attachments', 0) => [
'attachment_name' => trans('fields.name'),
'attachment_description' => trans('fields.description')
]
]
]);
}
So I fixed it myself.
Solution:
I got the collection ID from request and stored it in variable.
$filter_collection = $request->filter_collection;
Passed variable to the view
->with('filter_collection', $filter_collection)
And changed my pagination to pass collection ID to next page
{{ $storage_items->appends(['filter_collection' => $filter_collection])->links() }}

Unable to insert data into database using codeigniter

I want to insert data into my database. I am using codeigniter framework to build my app.
When I click on submit button,It don't give any error just reload the same page and data in not inserted in database.
Following my code to insert data into database -
public function addSale($saleDetails = array(), $items = array(), $warehouse_id)
{
foreach($items as $data){
$product_id = $data['product_id'];
$product_quantity = $data['quantity'];
$this->updateProductQuantity($product_id, $warehouse_id, $product_quantity);
}
// sale data
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
if($this->db->insert('sales', $saleData)) {
$sale_id = $this->db->insert_id();
$addOn = array('sale_id' => $sale_id);
end($addOn);
foreach ( $items as &$var ) {
$var = array_merge($addOn, $var);
}
if($this->db->insert_batch('sale_items', $items)) {
return true;
}
}
return false;
}
Based on https://ellislab.com/codeigniter/user-guide/database/examples.html , it seems to me, that you are missing a line -> $this->db->insert('sales', $saleData); . You do have it inside an if statement, but if statement will not execute it as a code, but check if it returns true.
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
$this->db->insert('sales', $saleData);
This should work. You can also check if it succeeds like this ->
return ($this->db->affected_rows() != 1) ? false : true;

CodeIgniter: Populating two tables through two arrays from the same form?

I tried doing the following and surprisingly it only populates the first table (new_guest) while writing only zero values to the other (new_reservation). How can I properly configure this?
Controller:
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_guests($data_guest);
$this->reservations_model->add_reservations($data_reservation);
$this->confirm_check_in();
}
Model:
class Reservations_model extends CI_Model {
function add_guests($data_guest) {
$this->db->insert('new_guest', $data_guest);
return;
}
function add_reservations($data_reservation) {
$this->db->insert('new_reservation', $data_reservation);
return;
}
}
it will be good if you use transaction. if data in both tables must be entered.
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_all_guests($data_guest,$data_reservation);
$this->confirm_check_in();
}
in model
function add_all_guests($data_guest,$data_reservation) {
$this->db->trans_begin();
$this->db->insert('new_guest', $data_guest);
$this->db->insert('new_reservation', $data_reservation);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return false;
}
else
{
$this->db->trans_commit();
return true;
}
}

How retrieve specific duplicate array values with PHP

$array = array(
array(
'id' => 1,
'name' => 'John Doe',
'upline' => 0
),
array(
'id' => 2,
'name' => 'Jerry Maxwell',
'upline' => 1
),
array(
'id' => 3,
'name' => 'Roseann Solano',
'upline' => 1
),
array(
'id' => 4,
'name' => 'Joshua Doe',
'upline' => 1
),
array(
'id' => 5,
'name' => 'Ford Maxwell',
'upline' => 1
),
array(
'id' => 6,
'name' => 'Ryan Solano',
'upline' => 1
),
array(
'id' =>7,
'name' => 'John Mayer',
'upline' => 3
),
);
I want to make a function like:
function get_downline($userid,$users_array){
}
Then i want to return an array of all the user's upline key with the value as $userid. I hope anyone can help. Please please...
You could do it with a simple loop, but let's use this opportunity to demonstrate PHP 5.3 anonymous functions:
function get_downline($id, array $array) {
return array_filter($array, function ($i) use ($id) { return $i['upline'] == $id; });
}
BTW, I have no idea if this is what you want, since your question isn't very clear.
If you need do search thru yours array by $id:
foreach($array as $value)
{
$user_id = $value["id"];
$userName = $value["name"];
$some_key++;
$users_array[$user_id] = array("name" => $userName, "upline" => '1');
}
function get_downline($user_id, $users_array){
foreach($users_array as $key => $value)
{
if($key == $user_id)
{
echo $value["name"];
...do something else.....
}
}
}
or to search by 'upline':
function get_downline($search_upline, $users_array){
foreach($users_array as $key => $value)
{
$user_upline = $value["upline"];
if($user_upline == $search_upline)
{
echo $value["name"];
...do something else.....
}
}
}
Code :
function get_downline($userid,$users_array)
{
$result = array();
foreach ($users_array as $user)
{
if ($user['id']==$userid)
$result[] = $user['upline'];
}
return result;
}
?>
Example Usage :
get_downline(4,$array);

Categories