Laravel: Add Pagination to Json Response - php

I have an array for showing users' contacts list to each other.
I want to add ->paginate(10) features for json response but I could not find where I must put it.
My index function:
public function index()
{
$contacts = [];
$user = request()->user();
// Loop through the contacts and format each one
Contact::for($user->id)->get()->each(function ($contact) use ($user, &$contacts) {
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
});
return response()->json($contacts);
}

You can create a collection for the contact and use LenfthAwarePaginator
class ContactResourceCollection extends ResourceCollection
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
$response = [
'data' => $this->collection,
];
if($this->resource instanceof LengthAwarePaginator)
{
$response['pagination'] = [
'total' => $this->resource->total(),
'lastPage' => $this->resource->lastPage(),
'perPage' => $this->resource->perPage(),
'currentPage' => $this->resource->currentPage(),
'nextPageUrl' => $this->resource->nextPageUrl(),
'previousPageUrl' => $this->resource->previousPageUrl(),
];
}
return $response;
}
}
In the controller method add this line:
return new UserResourceCollection($users);

Here is the total code
$contacts = Contact::where('user_id', $user->id)->paginate(12);
if($contacts->count()){
$pageIndex = array();
$lastPage = $contacts->lastPage();
$user = request()->user();
for($i= 2; $i<=$lastPage; $i++){
array_push($pageIndex, $i);
}
return response()->json([
'contacts' => $contacts->map(function ($contact) use ($user) {
if($contact->user1_id === $user->id){
return [
'friend' => $contact->user2,
'room' => $contact->room,
];
} else {
return [
'friend' => $contact->user1,
'room' => $contact->room,
];
}
})->toArray(),
'per_page' => $contacts->perPage(),
'on_first_page' => $contacts->onFirstPage(),
'last_page' => $contacts->lastPage(),
'first_page_url' => $contacts->url(1),
'next_page_url' => $contacts->nextPageUrl(),
'prev_page_url' => $contacts->previousPageUrl(),
'last_page_url' => $contacts->url($contacts->lastPage()),
'total' => $contacts->total(),
'pageIndexArray' => $pageIndex,
'errors' => false,
]);
} else {
// Do Nothing
}
Call
GET 'URL?page='+Page_index to get the response in JS (AJAX)

I am not sure but try : replace get() to paginate(10)
Contact::for($user->id)->paginate(10)->each(function ($contact) use ($user, &$contacts) {
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
});

Can you change the query into:
$contacts = Contact::for($user->id)->paginate(10);
Then after this query you can use for loop for $contact;
foreach ($contacts as $key => $contact)
{
$friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
$contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
}
Paginate first before get into loop/each.

Related

Laravel UPDATE query with multiple where conditions not working

I am fed up. Simple update method not working. It should update record matching the given item_id,restaurant_id and avil_date.
public function updateWeeklyItem(){
$item_id=request('item_id');
$restaurant_id=request('restaurant_id');
$avil_date=request('avil_date');
weekly_item::where(function($query) use ($item_id, $restaurant_id,$avil_date){
$query->where('item_id', $item_id)
->where('restaurant_id', $restaurant_id)
>where('avil_date', $avil_date);
})->update ([
'start_time'=>request('start_time'),
'end_time'=>request('end_time'),
'tiffin-switch'=>request('tiffin-switch'),
'lunch-switch'=>request('lunch-switch'),
'snacks-switch'=>request('snacks-switch'),
'dinner-switch'=>request('dinner-switch'),
'special-switch'=>request('special-switch'),
'extend-avil'=>request('extend-avil'),
'unit'=>request('unit'),
'quantity'=>request('quantity')
]);
}
Use This Code:
public function updateWeeklyItem(Request $request) {
$item_id = $request->item_id;
$restaurant_id = $request->restaurant_id;
$avil_date = $request->avil_date;
$item = weekly_item::where(function($query) use ($item_id, $restaurant_id,$avil_date) {
$query->where('item_id', $item_id)
->where('restaurant_id', $restaurant_id)
->where('avil_date', $avil_date);
})->first();
$item_array = (
'start_time' => $request->start_time,
'end_time' => $request->end_time,
'tiffin-switch' => $request->tiffin-switch,
'lunch-switch' => $request->lunch-switch,
'snacks-switch' => $request->snacks-switch,
'dinner-switch' => $request->dinner-switch,
'special-switch' => $request->special-switch,
'extend-avil' => $request->extend-avil,
'unit' => $request->unit,
'quantity' => $request->quantity
);
$item->update($item_array);
return $item;
}

PHP end() function not getting end Array item and object key in Laravel

In my Laravel project, I've got a job set up which runs and attempts to notify a user based on their threshold and chosen alert metrics. I'm using the php end() method to get the last item in an array and then attempting to get whatever metric the user has chosen.
However, upon dumping the data, this isn't returning the last array item, it's returning every item and I'm not sure why?
When I dump my data, I'm getting this format instead of the last item in the array:
[2021-04-13 13:30:45] production.DEBUG: array (
0 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '2190',
'period_from' => '2021-04-13 00:00:00',
'period_to' => '2021-04-13 13:30:02',
'created_at' => '2021-04-13 13:30:06',
),
1 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '5184',
'period_from' => '2021-04-12 00:00:00',
'period_to' => '2021-04-12 23:57:02',
'created_at' => '2021-04-12 23:57:07',
),
2 =>
(object) array(
'event_category' => 'My Category',
'event_action' => 'My Event',
'event_count' => '3820',
'period_from' => '2021-04-11 00:00:00',
'period_to' => '2021-04-11 23:57:02',
'created_at' => '2021-04-11 23:57:07',
),
)
I should just be seeing the last item, amongst all of my code, the following is of significant value here:
/**
* Notify if data meets threshold & alert rules
*
* #return void
*/
public function notifyAlertThreshold($alerts, $data)
{
$newestDataPart = end($data) ?? null;
// alerts for data source
foreach ($alerts as $key => $alert) {
Log::debug($newestDataPart);
$metric = !isset($newestDataPart->{$alert->metric}) ? $newestDataPart : $newestDataPart->{$alert->metric};
}
}
In context, here's some mode of the code, but the primary question here, is why is my end() method not returning the last item?
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$filters = json_decode($this->report->discovery_filters, true);
$this->reportStatus = 'complete';
$data = [];
foreach ($filters as $findableKey => $findable) {
/*
** If there are datasets on the findable objec, then we assume
** that we can build up a chart or some data structure.
*/
if (isset($findable['datasets'])) {
$pushableDatasets = [];
foreach ($findable['datasets'] as $datasetKey => $dataset) {
// query data
if (isset($dataset['query'])) {
$chartLabel = $findable['name'] ?? 'Untitled Chart';
$this->setDynamicChartOptions($chartLabel);
$additionFromField = $dataset['query']['additionFromField'] ?? '';
$resultData = [];
if ($dataset['query']['prefersConversionCalculation'] == 'yes') {
$totals = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['calculateConversionFromTotals'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$goal = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['calculateConversionByGoal'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$totals = $totals->flatten();
$goal = $goal->flatten();
$totalsGrouped = $this->groupData(
$totals,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
$goalsGrouped = $this->groupData(
$goal,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
$totalsGroupedFlattened = $totalsGrouped->flatten();
$goalsGroupedFlattened = $goalsGrouped->flatten();
$resultData = $this->getStructure($findable, $datasetKey, $goalsGroupedFlattened, $totalsGroupedFlattened);
array_push($pushableDatasets, $resultData);
} else {
$res = DB::table($dataset['query']['table'])
->select($dataset['query']['columns'])
->where($dataset['query']['filterBy'])
->orderBy($dataset['query']['orderBy']['field'], $dataset['query']['orderBy']['direction'])
->get()
->chunk(100);
$res = $res->flatten();
if (isset($dataset['query']['useGrouping']) && $dataset['query']['useGrouping'] == 'yes') {
$results = $this->groupData(
$res,
$dataset['query']['groupBy'],
$dataset['query']['groupByFormat'],
$additionFromField
);
// if we're using an addition function our array is already flattened
if (!empty($additionFromField)) {
$resultData = $results;
} else {
$resultData = $results->flatten();
}
array_push($pushableDatasets, $this->getStructure($findable, $datasetKey, $resultData));
}
}
$dataForAlerts = $resultData;
if ($dataset['query']['prefersConversionCalculation'] == 'yes') {
$dataForAlerts = $dataForAlerts['data'];
}
// alerting
$alerts = $this->getAlertThresholds($dataset['query']['table']);
$this->notifyAlertThreshold($alerts, $dataForAlerts);
}
}
$findable['datasets'] = $pushableDatasets;
}
array_push($data, $findable);
}
// no data or it's empty
if (!isset($data) || empty($data)) {
$this->reportStatus = 'error';
}
// create our report data entry
$this->updateReportData(false, $data);
}

How can I get get params in url via slashes in Yii2?

I want to build url with get params via slashes.
For exapmle, I want this code:
Url::to(['/controller/action',
'filter' => 'filter,
'param1' => 'value1',
'param2' => 'value2' ...]);
to generate url:
//path.to.site/controller/action/filter/param1/value1/param2/value2...
Is there a way to make this via defaut urlManager?
Sure:
'urlManager' => [
'rules' => [
'<controller>/<action>/<filter>/param1/<param1>/param2/<param2>' => '<controller>/<action>',
],
],
Then Url::to will return following result:
Url::to([
'/controller/action',
'filter' => 'some-filter',
'param1' => 'value1',
'param2' => 'value2'
]);
// Result: http://base.url/controller/action/some-filter/param1/value1/param2/value2
use Yii;
use yii\web\UrlRuleInterface;
class SearchUrlRule implements UrlRuleInterface
{
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
if (strpos($pathInfo, 'somepath') !== 0) {
return false;
}
$parameters = explode('/', $pathInfo);
if ($parameters[1] !== 'f') {
return false;
}
$route = 'controller/action';
$params = [
'filter' => 'f'
];
$count = count($parameters);
$i = 2;
var_dump($parameters);
while (($i + 1) < $count) {
$params[$parameters[$i]] = $parameters[$i + 1];
$i = $i + 2;
}
Yii::trace("Request parsed with URL rule: action/controller", __METHOD__);
return [$route, $params];
}
public function createUrl($manager, $route, $params)
{
if ($route !== 'controller/action') {
return false;
}
if ($params['filter'] !== 'f') {
return false;
}
unset($params['filter']);
//If a parameter is defined and not empty - add it to the URL
$url = 'somepath/f/';
foreach ($params as $k => $param) {
$url .= "$k/" . $param;
}
return $url;
}
}
Here is code, that solved my problem. And in config:
'rules' => [
...
['class' => 'frontend\components\SearchUrlRule'],
...

Laravel Argument 1 passed to

I'm trying to do my coding, but I ran into this issue. Thing is, I did it exactly the same like my first code, but it's working there.
ErrorException in CoinflipController.php line 115: Argument 1 passed
to App\Http\Controllers\CoinflipController::CoinflipToJson() must be
an instance of App\Models\Game\Coinflip\Coinflip, instance of
Illuminate\Database\Eloquent\Collection given, called in
C:\xampp\htdocs\site\app\Http\Controllers\CoinflipController.php on
line 104 and defined
Coinflip File
<?php
namespace App\Models\Game\Coinflip;
use Illuminate\Database\Eloquent\Model;
class Coinflip extends Model {
const STATUS_ACTIVE = 0;
const STATUS_ROLLING = 1;
const STATUS_ENDED = 2;
protected $table = 'coinflip';
protected $fillable = [
'status',
'winner_steam_id',
'winner_probability',
'winner_value',
'hash',
'ticket',
'seed',
'player1',
'player1_side',
];
protected $dates = [ 'draw_at' ];
protected $casts = [
'winner_steam_id' => 'string',
'winner_probability' => 'float',
'winner_value' => 'float',
'ticket' => 'double'
];
public function entry(){
return $this->hasMany( 'App\Models\Game\Coinflip\CoinflipEntry', 'coinflip_id' );
}
public function winner(){
return $this->hasOne( 'App\User', 'steam_id', 'winner_steam_id' );
}
public function getCommissionValue(){
$val = 0;
foreach( $this->entry as $entry ){
foreach( $entry->item as $item ){
if ( $item->status == CoinflipEntryItem::STATUS_COMMISIONED )
$val += (float)$item->price;
}
}
return $val;
}
}
CoinflipToJson Function (From 1st line is the Error)
public function CoinflipToJson( Coinflip $coinflip, $showExtra = false ){
$canDeposit1 = $coinflip->value * 0.10 + $coinflip->value;
$canDeposit2 = $coinflip->value - $coinflip->value * 0.10;
$data = [
'id' => $coinflip->id,
'hash' => $coinflip->hash,
'gameValue' => $coinflip->value,
'canDeposit1' => $canDeposit1,
'canDeposit2' => $canDeposit2,
'skinValue' => 0,
'skinCount' => 0,
'timeStart' => $coinflip->created_at->getTimestamp(),
'timeEnd' => $coinflip->draw_at ? $jackpot->draw_at->getTimestamp() : 0,
'timeEndIn' => $coinflip->draw_at ? $jackpot->draw_at->getTimestamp() - time() : -1,
'timeMax' => Setting::of('JackpotExtendTime', 15)->getValue(),
'entry' => []
];
if ( $showExtra ){
$data['winningPercentage'] = $coinflip->ticket;
$data['winnerId'] = $coinflip->winner_steam_id;
$data['secret'] = $coinflip->seed;
}
foreach( $coinflip->entry as $entry ){
$entryData = $this->entryToJson( $entry );
$data['entry'][] = $entryData;
$data['skinValue'] += $entryData['skinValue'];
$data['skinCount'] += $entryData['skinCount'];
}
return $data;
}
Code where I am calling it (Line 5)
public function current( Request $request ){
$coinflip = $this->getCurrentGame();
if($coinflip){
$data = [
'current' => $this->CoinflipToJson($coinflip)
];
return response()->json($data);
} else return response()->json(['error' => 'No Games']);
}
getCurrent Game Function
public function getCurrentGame(){
$coinflip = Coinflip::where('status', Coinflip::STATUS_ACTIVE)->get();
return $coinflip;
}
In your getCurrentGame() method, the get() method always returns a Collection, even if there was only one record. If you query will only ever return one record, you can just change get() to first(), and it will return the record instance, instead of a Collection.

Empty result in second level of kartik depdrop in Yii2

Here am trying Dependent dropdown using kartik depdrop yii2 extension . the process of this dependent dropdown is, if i select a productname it will show me the dependent batchno, then if select a batchno, it will show the dependent itemid.
Actually first level is working perfectly, if i select a productname, it will show me batchno, this action is working perfectly, but the problem on the second level. if i select a batchno it need to show me a itemid, this action is not working
And am getting error as this image -
Controller
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = Productbatch::getBatchNo($cat_id);
echo Json::encode($out);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
//echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
public function actionProd() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$ids = $_POST['depdrop_parents'];
$cat_id = empty($ids[0]) ? null : $ids[0];
$subcat_id = empty($ids[1]) ? null : $ids[1];
if ($cat_id != null) {
$data = Productbatch::getItemid($cat_id, $subcat_id);
/**
* the getProdList function will query the database based on the
* cat_id and sub_cat_id and return an array like below:
* [
* 'out'=>[
* ['id'=>'<prod-id-1>', 'name'=>'<prod-name1>'],
* ['id'=>'<prod_id_2>', 'name'=>'<prod-name2>']
* ],
* 'selected'=>'<prod-id-1>'
* ]
*/
echo Json::encode($out);
//echo Json::encode(['output'=>$out, 'selected'=>$data['selected']]);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
_form
<?= $form->field($model, 'productname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Product Name', 'id' => 'cat-id'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['cat-id'],
'placeholder'=>'Select BatchNo',
'url'=>Url::to(['/production/productbatch/subcat'])
]
]); ?>
<?= $form->field($model, 'itemid')->widget(DepDrop::classname(), [
'pluginOptions'=>[
'depends'=>['cat-id', 'subcat-id'],
'placeholder'=>'Select ItemId',
'url'=>Url::to(['/production/productbatch/prod'])
]
]); ?>
Model
public static function getBatchNo($cat_id)
{
$out = [];
$data = Productbatch::find()
->where(['productname' => $cat_id])
->asArray()
->all();
foreach ($data as $dat) {
$out[] = ['id' => $dat['itemid'], 'name' => $dat['batchno']];
}
return $output = [
'output' => $out,
'selected' => ''
];
}
public static function getItemid($cat_id, $subcat_id)
{
$out = [];
$data = Productbatch::find()
->where(['productname' => $cat_id])
->andWhere(['batchno' => $subcat_id])
->asArray()
->all();
$selected = '';
foreach ($data as $dat => $datas) {
$out[] = ['id' => $datas['itemid'], 'name' => $datas['itemid']];
if($dat == 0){
$aux = $datas['itemid'];
}
($datas['productname'] == $cat_id) ? $selected = $cat_id : $selected = $aux;
}
return $output = [
'output' => $out,
'selected' => $selected
];
}
You need do like this in your controller. You have to create new action in controller .
public function actionState() {
$country_id = $_POST['depdrop_parents'][0];
$state = State::find()->where(['country_id' => $country_id])->all();
$all_state = array();
$i = 0;
foreach ($state as $value) {
$all_state[$i]['id'] = $value['state_id'];
$all_state[$i]['name'] = $value['state_name'];
$i++;
}
echo Json::encode(['output' => $all_state, 'selected' => '']);
return;
}
i also suffered from this problem but finally I solved this issue..

Categories