Laravel - #each get current iteration - laravel

I am using the #each blade directive to loop through an array of query results and render them (in this case, the #each directive tends to be a bit of a better idea than a foreach loop since the HTML that will be renders changes depending on the result of the query). This all works fine, but I have the issue of trying to get the current index of the query itself.
In other words, using the #each directive, I need to be able to access the key (or index) of the current loop in the partial that is being rendered.
If you have anything that you need to know, just ask.
Thank you for reading!

Looking at the source code that's run for the #each directive yields the following:
Illuminate\View\Factory.php
/**
* Get the rendered contents of a partial from a loop.
*
* #param string $view
* #param array $data
* #param string $iterator
* #param string $empty
* #return string
*/
public function renderEach($view, $data, $iterator, $empty = 'raw|')
{
$result = '';
// If is actually data in the array, we will loop through the data and append
// an instance of the partial view to the final result HTML passing in the
// iterated value of this data array, allowing the views to access them.
if (count($data) > 0) {
foreach ($data as $key => $value) {
$data = ['key' => $key, $iterator => $value];
$result .= $this->make($view, $data)->render();
}
}
// If there is no data in the array, we will render the contents of the empty
// view. Alternatively, the "empty view" could be a raw string that begins
// with "raw|" for convenience and to let this know that it is a string.
else {
if (Str::startsWith($empty, 'raw|')) {
$result = substr($empty, 4);
} else {
$result = $this->make($empty)->render();
}
}
return $result;
}
If you notice on these lines:
$data = ['key' => $key, $iterator => $value];
$result .= $this->make($view, $data)->render();
A variable called $key is automatically passed to the rendered view. That should contain the current index of your array loop.

Related

pass an index to the method reduce() laravel

I have this function created to show a piechart but I need each element to show a different color. For this I have created an array with all the colors and I need in each iteration of the reduce() method to have an index to access the colors[i]. I have tried this way and it does not work. Any suggestion?
$i = 0;
$pieChartModel = $options->groupBy('survey_options_id')
->reduce(function (PieChartModel $pieChartModel, $data) use ($i) {
$type = $data->first()->survey_options_id;
$value = $data->sum('value');
// $color = "#" . substr(md5(rand()), 0, 6);
$NameOption = Survey_options::where('id', $type)->pluck('name');
return $pieChartModel->addSlice($NameOption, $value, $this->colors[$i]->hexa);
$i++;
}, (new PieChartModel())->setAnimated($this->firstRun)->setDataLabelsEnabled(true));
A problem I'm seeing in your code is how you're incrementing $i AFTER a return statement.
After taking a look at the source code for the reduce() function
/**
* Reduce the collection to a single value.
*
* #param callable $callback
* #param mixed $initial
* #return mixed
*/
public function reduce(callable $callback, $initial = null)
{
$result = $initial;
foreach ($this as $key => $value) {
$result = $callback($result, $value, $key);
}
return $result;
}
You should be able to use the key (or index) in the callback.
->reduce(function ($carry, $item, $key) { ... }, $initial)
->reduce(function (PieChartModel $pieChartModel, $data, $i) {
...
}, (new PieChartModel())->setAnimated($this->firstRun)->setDataLabelsEnabled(true))

save array in controller laravel

ErrorException
Array to string conversion
$presocio = new Presocio;
$presocio->prestamo_id = $request->prestamo_id;
$presocio->ncuota = $request->ncuota;
$presocio->montopag = $request->montopag;
$presocio->fechapag = $request->fechapag;
$presocio->save();
In the end I managed to make it work like this, it works perfectly.
it can be done in different ways, example with ::create ::insert
$prestamo = new Prestamo;
$prestamo->socio_id = $request->socio_id;
$prestamo->monto = $request->monto;
$prestamo->cuotas = $request->cuotas;
$prestamo->alias = $request->alias;
$prestamo->save();
$idprestamo = $prestamo->id;
if (count($request->ncuota) > 0) {
foreach ($request->ncuota as $item => $v) {
$presocio = new Presocio;
$presocio->fill(
array(
'prestamo_id' => $idprestamo,
'ncuota' => $request->ncuota[$item],
'montopag' => $request->montopag[$item],
'fechapag' => $request->fechapag[$item],
)
);
$presocio->save();
}
}
toast('Pago Programados Registrado', 'success');
return redirect('prestamo');
Update since we now have the form supplied. You are using form names such as ncuota[] instead of ncuota which makes it an array. Are you able to make more than 1 Preseocio? if this is the case you want to loop over the items in the controller.
for ($i = 0; $i < count($request->ncuota); $i++)
{
Presocio::create([
'prestamo_id' => $request->prestamo_id[$i],
'ncuota' => $request->ncuota[$i],
'montopag' => $request->montopag[$i],
'fechapag' => $request->fechapag[$i],
]);
}
Otherwise just remove the [] off the end of the form names.
class Presocio
{
...
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'prestamo_id',
'ncuota',
'montopag',
'fechapag',
];
...
}
Presocio::create($request->all());
Now, Thats not the issue. That is just a bit of house keeping.
Your issue is that one of your request fields is an Array. Which ever one it is you will need to convert it to a JSON object or find a better way of storing it.
If you dont care and want to keep it as an array, modify the database field to be a jsonb field.
Try This create method
remove your all code and write only this code in your store method
$input = $request->all();
Presocio::create($input);
You can do that like:
for($i=0; $i < count($request->input('prestamo_id', 'ncuota', 'montopag', 'fechapag')); $i++) {
$presocio = new Presocio;
$presocio->prestamo_id = $request->prestamo_id[$i];
$presocio->ncuota = $request->ncuota[$i];
$presocio->montopag = $request->montopag[$i];
$presocio->fechapag = $request->fechapag[$i];
$presocio->save();
}

Getting the next object in a collection in Laravel

I have mutliple Chapters that belong to a Module.
On a chapter page I want to check if I am on the last one in the module, but I'm getting a bit stuck.
// inside Chapter model.
// The $module var is a made by something like Module::with('chapters')->find(1);
public function getNext($module){
// Convert to array so we can call some of
// the array functions to navigate the array
$chapters = $module->chapters->keyBy('id')->toArray();
// get the last element in the array
end($chapters);
// If the last element's key is the same as this one,
// there is no "next" link
if(key($chapters) == $this->id){
return false;
}
// So there must be a next link. First, reset internal array pointer
reset($chapters);
// Advance it to the current item
while (key($chapters) !== $this->id) next($chapters);
// Go one further, returning the next item in the array
next($chapters);
// current() is now the next chapter
return current($chapters);
}
Cool! So this lets me know if there is a next chapter and even returns it as an array with all of its data. But I'm getting into massive problems. The Chapter has a few other methods on it which I can't call on the 'next' element as its an array, not an object any more.
// Chapter.php
public function url(){
return url('chapter/' . $this->id);
}
$module = Module::with('chapters')->find(1);
$chapter = Chapter::find(1);
$next = $chapter->getNext($module);
if( $next )
echo $next->url();
This gives me (obviously)
Call to a member function url() on array
So I need to rewrite this function, but I can't work out how to get the next object in a Laravel collection.
public function getNext($module){
$last = $module->chapters->last();
// If the last element's key is the same as this one,
// there is no "next" link
if($last->id == $this->id){
return false;
}
....
How can I traverse the collection to get the next Chapter as an object?
After a little bit I have worked out my own solution:
public function getNext($module){
$last = $module->chapters->last();
// If the last element's key is the same as this one,
// there is no "next" link
if($last->id == $this->id){
return false;
}
$current_order = $this->order;
$filtered = $module->chapters->filter(function ($item) use ($current_order) {
return $item->order > $current_order;
});
return $filtered->first();
}
Open to any other neater ways of doing it though! Thanks
You can create collection macros as:
Collection::macro('previous', function ($key, $value = null) {
if (func_num_args() == 1) $value = $key; $key = 'id';
return $this->get($this->searchAfterValues($key, $value) - 1);
});
Collection::macro('next', function ($key, $value = null) {
if (func_num_args() == 1) $value = $key; $key = 'id';
return $this->get($this->searchAfterValues($key, $value) + 1);
});
Collection::macro('searchAfterValues', function ($key, $value) {
return $this->values()->search(function ($item, $k) use ($key, $value) {
return data_get($item, $key) == $value;
});
});
Then you can use it as:
$module = Module::with('chapters')->find(1);
$chapter = Chapter::find(1);
$next = $module->chapters->next($chapter->id)
// or
$next = $module->chapters->next('id', $chapter->id)

Laravel change pagination data

My Laravel pagination output is like laravel pagination used to be, but I need to change the data array for each object.
My output is:
As you can see, the data object has 2 items, which I need to change.
My code is:
$items = $this->items()
->where('position', '=', null)
->paginate(15);
Which returns the user items with pivot table, but I don't like the way the pivot table is shown in the JSON, so I decided to change the items and organize each item with the pivot before the item.
For this purpose, I tried to use foreach
foreach ($items->data as $item)
{
}
which giving my an error, for a reason I don't know:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$data"
status_code: 500
Any help?
The paginator's items is a collection. You can grab it and transform the data like so:
$paginator = $this->items()->where('position', '=', null)->paginate(15);
$paginator->getCollection()->transform(function ($value) {
// Your code here
return $value;
});
If you are familiar with tap helper here is the snippet that does exact same.
$paginator = tap($this->items()->where('position', '=', null)->paginate(15),function($paginatedInstance){
return $paginatedInstance->getCollection()->transform(function ($value) {
return $value;
});
});
We can't chain method getCollection to paginator instance because AbstractPaginator will return paginator's underlying collection. so the paginator instance will be transformed to Collection. So fix that we can use tap helper.
If you'd like to keep items paginated:
$itemsPaginated = $this->items()
->paginate(15);
$itemsTransformed = $itemsPaginated
->getCollection()
->map(function($item) {
return [
'id' => $item->id,
];
})->toArray();
$itemsTransformedAndPaginated = new \Illuminate\Pagination\LengthAwarePaginator(
$itemsTransformed,
$itemsPaginated->total(),
$itemsPaginated->perPage(),
$itemsPaginated->currentPage(), [
'path' => \Request::url(),
'query' => [
'page' => $itemsPaginated->currentPage()
]
]
);
There is a setCollection method for such purpose.
$items = Model::paginate(10);
$updatedItems = $items->getCollection();
// data manipulation
// ...
$items->setCollection($updateItems);
From the source code of /Illuminate/Pagination/AbstractPaginator.php
/**
* Set the paginator's underlying collection.
*
* #param \Illuminate\Support\Collection $collection
* #return $this
*/
public function setCollection(Collection $collection)
{
$this->items = $collection;
return $this;
}
Source
I could make shorter way. This returns edited $array instead of simple $paginated. This example modify file names.
This doc was useful for me.
$paginated=$query->paginate(12);
$array=$paginated->toArray();
foreach ($array['data'] as $r=>$record) {
$array['data'][$r]->gif=$array['data'][$r]->gif.".gif";
}
return $array;
Sample Example :
$franchiseData=[ 'id'=>1 ,'name'=>'PAnkaj'];
$models = $bookingsQuery->paginate(10);
$models->setCollection(collect($franchiseData));
return $models;
Note that $models->setCollection(collect($franchiseData)); you have to use collect() else you will get error.
getCollection
is one way to get the items. Another way is to use this
For ex- Assuming, user doesn't have name param and only have first_name and last_name
$userPaginatedData = User::paginate(15);
$users = $userPaginatedData->items();
foreach($users as $user) {
$user->name = $user->first_name . ' ' . $user->last_name;
}
return $userPaginatedData;
Now in the data key, you would see that each user has name param with it.
Laravel 8.9.0 has added the through method to AbstractPaginator.
It transforms each item in the slice of items using a callback, and keeps the items paginated.
$paginator = $this->items()->where('position', '=', null)->paginate(15);
$paginator->through(function ($value) {
// Your code here
return $value;
});
The source code:
/**
* Transform each item in the slice of items using a callback.
*
* #param callable $callback
* #return $this
*/
public function through(callable $callback)
{
$this->items->transform($callback);
return $this;
}
-Laravel 5.4
// example update column "photo"
// from "/path/to/photo.png"
// to "abc.com/path/to/photo.png"
foreach ($items as $item)
{
$path = $item->photo;
// Remove
$item->offsetUnset("photo");
// Set
$item->offsetSet("photo", url($path));
}
Laravel AbstractPaginator has methods setCollection() and getCollection()
<?php
$itemsPaginated = $this->items()->paginate(15);
$itemsPaginated->setCollection(
$itemsPaginated->getCollection()->transform(function ($item) {
// Your code here
return $item;
})
)
This is your paginated items...
$items = $this->items()
->where('position', '=', null)
->paginate(15);
I am using Laravel 8, can simply use each
$items->each(function ($item) {
// your code here
$item->custom_data = calcSomeData(); // sample
});
It does the same as this...
$items->getCollection()->transform(function ($item) {
// your code here
$item->custom_data = calcSomeData(); // sample
});
<?php
$itemsPaginated = $this->items()->paginate(15);
$itemsPaginated = json_encode($itemsPaginated);
foreach ($itemsPaginated->data as $key => $item) {
$results->data[$key]; //Modify
}
$itemsPaginated = json_encode($results);
you have to use below code in your blade
{!! $items->render() !!}
Ignore the pagination in laravel and hit the normal data
foreach ($items as $item)
{
}

joomla tag form loads whole list from db

I noticed that joomla tag input field is quite stupid. It loads everything from db, in this case 9K tags. Obviously ui becomes so slow.
Any ideas how to fix it? it seems there is already an ajax functionality present, so why not rely on that completely? J ways are crazy.
1 idea is to modify getOption method, and load only the tags that are related to current article editor is editing.
But in this context I don't seem to have article id.
Any ideas how to solve situation? I'm sure some of you've run into this :S
/**
* Method to get a list of tags
*
* #return array The field option objects.
*
* #since 3.1
*/
protected function getOptions()
{
$published = $this->element['published']? $this->element['published'] : array(0,1);
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('DISTINCT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft')
->from('#__tags AS a')
->join('LEFT', $db->qn('#__tags') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
// Filter language
if (!empty($this->element['language']))
{
$query->where('a.language = ' . $db->q($this->element['language']));
}
$query->where($db->qn('a.lft') . ' > 0');
// Filter on the published state
if (is_numeric($published))
{
$query->where('a.published = ' . (int) $published);
}
elseif (is_array($published))
{
JArrayHelper::toInteger($published);
$query->where('a.published IN (' . implode(',', $published) . ')');
}
$query->order('a.lft ASC');
// Get the options.
$db->setQuery($query);
try
{
$options = $db->loadObjectList();
}
catch (RuntimeException $e)
{
return false;
}
// Block the possibility to set a tag as it own parent
if ($this->form->getName() == 'com_tags.tag')
{
$id = (int) $this->form->getValue('id', 0);
foreach ($options as $option)
{
if ($option->value == $id)
{
$option->disable = true;
}
}
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
// Prepare nested data
if ($this->isNested())
{
$this->prepareOptionsNested($options);
}
else
{
$options = JHelperTags::convertPathsToNames($options);
}
return $options;
}
So I've modified list that gets preloaded, only to load tags that are present in the article (saved as belonging to article). Autocomplete still works with ajax so no loss of functionality there.

Resources