How can I Paginate data which are in array? - arrays

I am using Laravel 5.7. I construcet an array like below:
$games = GameResource::collection(Game::all());
$clientGames = array();
foreach ($games as $game) {
if (!$game->user->inRole('admin')) {
array_push($clientGames, $game);
}
}
How can I paginate this array in Laravel?

I used below way to solve my question:
$per_page = !empty($_GET['per_page']) ? $_GET['per_page'] : 10;
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$clientGamesCollection = collect($clientGames);
$currentPageItems = $clientGamesCollection->slice(($currentPage * $per_page) - $per_page, $per_page)->all();
$paginatedItems= new LengthAwarePaginator($currentPageItems , count($clientGamesCollection), $per_page);
$paginatedItems->setPath($request->url());
$pagination = $paginatedItems;

Related

laravel array sort by

data comes in fine. what i want to do is to sort the sizes sort by key 'size_piece' from smallest to largest. can you help me
Controller code;
foreach ($cco as $order_item){
foreach ($order_item->orderPiece as $piece_item){
$order_piece += $piece_item->piece;
$cut_order['cutting_piece'][$order_item->collectionColor->color_name]['size_piece'][$piece_item->CollectionColorSizeBarcode->size] = $piece_item->piece;
$cut_order['cutting_piece'][$order_item->collectionColor->color_name]['total_piece'] += $piece_item->piece;
}
$cut_order['order'][$order_item->id] = $order_item;
$cut_order['order'][$order_item->id]['total_piece'] = $order_piece;
$order_piece = 0;
}
return dd($cut_order);
Screen output like this;
If it there is no way to sort them before insertion.
The solution is to map your array:
use Illuminate\Support\Arr;
$mapped = Arr::map($cut_order['cutting_piece'], function ($value, $key) {
return Arr::set($value, 'size_piece', Arr::sort(Arr::get($value, 'size_piece', []));
});
$cut_order['cutting_piece'] = $mapped;
Documentation : Laravel Helper Array Map

Multiple if statement to update the filed same model or change the records to another model using Cakephp

I am getting array value from the request and my first if statement works great to me, in the case of second if statement its migrating only one row to my salereturn table but i want all the record in the request to be migrate to the salereturn table.
if ($this->request->is('post')) {
$Productsalesrec = !empty($this->request->data['Productsales']) ? $this->request->data['Productsales'] : "";
if (!empty($Productsalesrec)) {
foreach ($Productsalesrec as $Productsales) {
if ($Productsales['status'] == 'MoveToShipment') {
$this->Productsales->id = $Productsales['id'];
$this->request->data['Productsales']['status'] = $Productsales['status'];
$this->Productsales->save($this->request->data);
}
if ($Productsales['status'] == 'Returned') {
$productsalesretArr = array();
$productsalesre = $this->Productsales->find('all', array('conditions' => array('Productsales.product_sales_slno' => $id)));
$this->request->data['Salesreturn']['sales_order_date'] = $productsalesre['Productsales']['sales_order_date'];
$this->request->data['Salesreturn']['product_sale_id'] = $productsalesre['Productsales']['id'];
$this->request->data['Salesreturn']['sales_date'] = $productsalesre['Productsales']['expected_delivery_date'];
$this->request->data['Salesreturn']['product_sales_slno'] = $productsalesre['Productsales']['product_sales_slno'];
$this->request->data['Salesreturn']['price_per_unit_order'] = $productsalesre['Productsales']['sales_price_per_unit_order'];
$this->request->data['Salesreturn']['total_amount'] = $productsalesre['Productsales']['sales_price_per_unit_order'] * $Productsales['tot_unit'];
$this->request->data['Salesreturn']['total_unit'] = $Productsales['tot_unit'];
$this->request->data['Salesreturn']['product_id'] = $Productsales['product_id'];
$this->request->data['Salesreturn']['amount_returned'] = 0;
$this->request->data['Salesreturn']['status'] = 'Returned';
$this->request->data['Salesreturn']['payment_method'] = 'Cash on Delivery';
$this->request->data['Salesreturn']['created_date'] = date('Y-m-d H:i:s');
$this->request->data['Salesreturn']['created_by'] = $this->Auth->user('id');
$this->Salesreturn->save($this->request->data['Salesreturn']);
if ($Productsales['total_unit'] == $Productsales['tot_unit']) {
$this->Productsales->delete($Productsales['id']);
} elseif ($Productsales['total_unit'] >= $Productsales['tot_unit']) {
$this->Productsales->id = $Productsales['id'];
$this->request->data['Productsales']['total_unit'] = $Productsales['total_unit'] - $Productsales['tot_unit'];
$this->Productsales->save($this->request->data);
}
$prodtype = $this->Producttype->find('first', array('conditions' => array('Producttype.id' => $productsalesre['Productsales']['product_type_id'])));
$this->Producttype->id = $prodtype['Producttype']['id'];
$prodquantity = $prodtype['Producttype']['quantity'] + ($Productsales['total_unit'] - $Productsales['tot_unit']);
$prodtotstck = $prodtype['Producttype']['total_unit_stock'] + ($Productsales['total_unit'] - $Productsales['tot_unit']);
$this->Producttype->saveField('total_unit_stock', $prodtotstck);
$this->Producttype->saveField('quantity', $prodquantity);
}
}
$this->redirect(array('action' => 'index'));
}
}
I'm not sure to really understand the problem...
Do you mean this part of code hasn't the wanted behaviour ?
if($Productsales['status'] == 'Returned') {
$this->request->data['Salesreturn']['sales_order_date']=$productsalesre['Productsales']['sales_order_date'];
...
$this->Salesreturn->save($this->request->data['Salesreturn']);
}
Does your condition " $Productsales['status'] == 'Returned' " is the problem ?
may be only one row of your post array has this status.

cakephp looping only save first row of data, others row not saving

Looping to save each column field picture1pic6:
Please help, i using cake php version 2.0, try to do looping to save each column, but only the first row data save, the data should be save like attached picture1.Hope someone can help, i tried to figure out 2 weeks. V_COUNT i hardcode the number to 23.
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
$this->IPI->setSource($table_name);
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$this->request->data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$this->request->data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$this->request->data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$this->request->data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->create();
$this->IPI->save($this->request->data);
}
}
try this
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
$data = array();
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->setSource($table_name);
$this->IPI->create();
$this->IPI->save($data);
}
}

How to save custom datetime in cakephp 3 for created & modified field?

I'm trying to save the following data. But it's not saving the datetime fields.
Created is saving current datetime in cakephp way & the others are null.
How can I save custom datetime?
Thanks in advance
foreach ($passwords as $key => $value ) {
$data['id'] = $collection_id;
$data['name'] = $value['password'];
$data['collection_id'] = $collection_id;
$data['photo_count'] = $value['photos_count'];
$data['download_count'] = $value['downloaded_users_count'];
$data['created'] = $value['created'];
$data['modified'] = $value['modified'];
$data['expires_at'] = $value['expires_at'];
$saveData[] = $data;
}
$passwords = $this->Passwords->find('all')->toArray();
$patched = $this->Passwords->patchEntities($passwords, $saveData);
foreach ($patched as $entity) {
$this->Passwords->save($entity);
}
I've fixed the Issue. Just have to use as follows-
$data['created'] = new Time($value['created']);
$data['expires_at'] = new Time($value['expires_at']);
And to do so, have to use this.
use Cake\I18n\Time;

Using read() in cakephp to retrieve row with array of data

I want to know if it is possible to retrieve a row from the database using something similar to the following:
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
$this->Place->set($place);
$place_found = $this->Place->read();
}
Is there some way I can preset the data in the Place model using the array and then use Place read. I'm looking for something simple like the usual:
$this->Place->id = 7;
$place_found = $this->Place->Read();
I have also tried doing this:
$this->Place->city = blah;
$this->Place->area = foo; etc....
$place_found = $this->Place->read();
However, that also does not work.
Haven't you ever used find()?! read() only fetches a row with the ID passed.
$place_found = $this->Place->find('first', array(
'conditions' => array(
'Place.city' => $city,
'Place.area' => $area
// etc
)
));
If you need to build the conditions manually you can create a conditions array to pass like so:
$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
$placeConditions['area'] = $area;
}
$places = $this->Place->find('first', array('conditions' => $placeConditions));
I suggest you read the page I linked, you will soon find out there is never a reason to use the read() method.
In model you could do:
$this->id = 3; //place id
$this->Place->read();
Hope it helps
I think this approach is what you're looking for(with your code):
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
//$this->Place->set($place); //don't need it here I think
$place_found = $this->Place->find('all',array('conditions'=>$place));
}
You'll have to use "find()", not "read()", but - it's almost as simple as your example code and should work. (also, there's a shortcut for the array_push() I believe, but - I like to use this for readability - personal preference):
if (!empty($this->params['form'])) {
$conditions = array();
array_push($conditions, array('Place.city' => $this->params['form']['city']);
array_push($conditions, array('Place.state' => $this->params['form']['state']);
array_push($conditions, array('Place.country' => $this->params['form']['country']);
array_push($conditions, array('Place.iso' => $this->params['form']['iso']);
$this->Place->set($place);
$place_found = $this->Place->find('all', array('conditions'=>$conditions));
}

Resources