Saving issue in laravel 5.4 - laravel

I'm trying to save multipliable ids in the database using loop but it only save the last id 3 times so I made for loop to test it
for ($i = 0; $i < 10; $i ++)
{
$this->product_id = $i;
$this->shop_name = $shop;
$this->save();
}
it saves number 9 three times and stops ????
This issue never happened before

You need to create a new instance with each iteration, for example:
for ($i = 0; $i < 10; $i++) {
$this->create(['product_id' => $i, 'shop_name' => $shop]);
}
Your code updates a single instance multiple times.

Related

Backpack for Laravel charts, append sum to chart

As I'm trying to implement charts into Backpack for Laravel, I been stuck for a few hours on this problem. The following script gets the number of users created for each day and appends them to an array that is then shown on the charts.
for ($days_backwards = 7; $days_backwards >= 0; $days_backwards--) {
// Could also be an array_push if using an array rather than a collection.
$users = Users::whereDate('created_at', today()->subDays($days_backwards))->count();
$user[] = $users;
}
Every iteration of the loop adds a number to the array (or is it a collection??) so something like [2,5,10,9,...].
I would rather like to get the total amount of users that ever registered, incrementally for each day, so that the result would be someting like [2,7,17,26,...].
I figured I could add each iteration with array_sum() but it's not working. Is it an array anyways? Is there a way to append to this list the sum to its previous?
Well I kind of figured out!
$sum = 0;
for ($days_backwards = 7; $days_backwards >= 0; $days_backwards--) {
$users = Users::whereDate('created_at', today()->subDays($days_backwards))->count();
$sum = $sum + $users;
$user[] = $sum;
}
It works!

see value according to the array

I do a select and get several results in array but I need to get the correct value for each step and set up a condition.
$step = DB::table('records')->where('id_user',$userId)->get();
for($i = 0; $i < count($step); $i++)
{
echo $step[$i]->id_step;
}
Id_step returns me values for each step where on the blade I need to get and see if id_step = 1 is true id_step = 2 is true.
This for is returning me only one value and it has 3 records in the table.
First of all. After a select you get an instance of Eloquent\Collection
Not an array.
So that said to loop do this:
$steps = DB::table('records')->where('id_user',$userId)->get();
foreach($steps as $row) {
echo $row;
}
Since you are familiar with arrays do this:
$steps->toArray();
Now your result is an array
Working insert in view this code.
#for($i = 0; $i < count($step); $i++)
#if($step[$i]->id_step == 2)
working
#else
not working
#endif
#endfor

generate more than one random code in laravel and save it to database

I'm a newbie in laravel. I have to generate multiple random characters at once in my laravel project and then save them to database. Any example or advise that quite easy to understand? thank you
A for loop may be appropriate for you in this case. If you don't have an object for the codes, you can do something like this:
$total_wanted = 100;
for ( $i = 0; $i < $total_wanted; $i++ ) {
DB::table('codes')->insert(['code' => uniqid("prefix")]);
}
If you do have a Code object, you can do it like this instead:
$total_wanted = 100;
for ( $i = 0; $i < $total_wanted; $i++ ) {
$code = new Code;
$code->code = uniqid("prefix");
$code->save();
}
echo uniqid('code_', true);
You generate a random, unique string based on current timestamp, so it can not be a duplicate.
Then simple store it in the database as a varchar
More about this function here

how can I run for loop 4 times

I have an array that I have counted and it equals 4. That is what it should equal. Now I want the foreach loop inside the for loop to only run 4 times. As it stands, I am getting to many results. Below is my latest attempt that does not work.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i <= $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
$i++;
}
the networks array is populated from a WordPress widget that has a repeating field section. The section allows the user to set social media icons. I currently have 4 social media icons set. On the front end, the page shows every social media icon available even though there are only 4 set. So I am trying to get the nested foreach loop to only run 4 times.
Currently your loop is executing 5 times..and you want it to run only 4 times... So Change this condition...for($i = 0; $i <= $size; $i++) to either for($i = 0; $i <$size; $i++) or for($i = 1; $i <= $size; $i++) like i.e.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i < $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
}
I have something more interesting for you..
it's a widget parent with children!
You can change field to load new fields from other classes
here's the link:
https://github.com/hishamdalal/parent_widget

Eloquent Laravel : creating a counter with foreach loop

using Laravel's Eloquent, I'm trying to add an incremental counter to certain rows based on their 'product_id'.
I thought I could do it like this :
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC');
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
}
return $foos->get();
But this is of no effect on my data, custom_counter column doesn't change (but I get no error).
I tried to add $foo->save() within my loop with no effect.
Get first all record and then modify single record using save method like this:
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC')->get(); // Get All records
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
$foo->save(); // Update each record
}
Good Luck !!!
To actually update the database, I reckon you need to persist each data object foo individually not the collection foos as a whole.
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC');
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
$foo->save(); // Persisting data here
}

Resources