Laravel Nova create same record multiple times - php

I am stuck with a issue on Laravel. I like to insert a records multiple times.
The user can add a amount, lets say 3. Than the insert query has to run 3 times.
I was able to make a observer but when i loop through there, it will still add one.
See the code below:
public function creating(CardOrder $cardOrder)
{
if($amount = $cardOrder->amount) {
unset($cardOrder->amount);
for($i = 0; $i < $amount; $i ++) {
$cardOrder->entity_id = 1234;
$cardOrder->group_id = 'test';
}
}
}
Is there a way to do that, and do i need a observer to accomplish that?
Thank you in advance.

Just added them $cardOrder = new CardOrder;
public function creating(CardOrder $cardOrder)
{
if($amount = $cardOrder->amount) {
unset($cardOrder->amount);
$cardOrder = new CardOrder;
for($i = 0; $i < $amount; $i ++) {
$cardOrder->entity_id = 1234;
$cardOrder->group_id = 'test';
$cardOrder->save();
}
}
}

Related

mysql query id as variable not working

I want to update my MySQL table. When I type the ID as a number works, but when using a variable instead, it does not work.
What I am trying to do is order elements of an html table by column.
I have e.g. 4 Columns:
$colname = array("Column1", "Column2", "Column3", "Column4");
I get the IDs of the elements already sorted from the URL variable:
$strTaskIds = $_GET["taskIds"];
// for example: $strTaskIds = "3;1;32_4;5_6;36_34;7"
Now I split the string into a 2D-Array and update the MySQL table:
$arrTaskIds = explode("_", $strTaskIds);
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
}
if($conW->query($sql) === TRUE) {
$error = 0;
} else {
$error = 1;
}
}
When I write a number E.G 7 instead of the variable $arrIdsPerCol[$j] it works.
Writing (int)$arrIdsPerCol[$j] does not work either.
The reason i gave you no error message is that there was none. It just looked like the MySQL table is not updating.
After starring at my code for quite a long time a found the problem.
I placed the query() code in the outer loop. But i needed it in the inner loop.
Problem solved:
$arrTaskIds = explode("_", $strTaskIds);
$error = 0;
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
if($conW->query($sql) === TRUE) {
} else {
$error = 1;
}
}
}

I am having problems with the output of my array

I have a class that I will later break up into multiple classes creating a BlackJack card game. I have the the decks created and accounted for correctly. I am having a problem with the output of my player array in my method "deal". The output only displays one player but my array of outputs should be returning two. The amount of cards that are missing from my deck also indicate that I am missing 4 cards, which makes sense since I have 2 players being dealt 2 cards. Can someone please help me return both players and display both of their respective cards.
<?php
/* creating a deck of cards class to be used in a blackJack game*/
class Deck{
public $cards = array();
public $player = [];
//creates an instance of a deck of cards (works)
public function __construct(){
$values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
$suits =array('Diamond','Club','Heart','Spade');
foreach ($suits as $suit) {
foreach($values as $value){
$this->cards[] = "$value of $suit's";
//$deck = $this->cards;
}
}
}
/*add more decks to increase number of total cards
in my array (works)*/
public function numberOfDecks($number){
$cards = $this->cards;
$this->number = $number;
for($i = 0 ; $i < $number-1; $i++){
$this->cards = array_merge($this->cards, $cards);
}
return $cards;
}
/*adding elements to a player as expected need to return multiple players and their cards multiple players (does not currently work)*/
public function deal($numberOfPlayers){
$this->numberOfPlayers = $numberOfPlayers;
$player = $this->player;
$number = 2;
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0];
array_shift($this->cards);
}
}
for($k = 0; $k < $numberOfPlayers; $k++)
return $player[$k];
}
}
$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//works as expec
$shuffled = shuffle($deck->cards);//works as expected
$deck->deal(2);
var_dump($deck);
here is the output I am currently getting back
The problem is in this chunk here:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0]; // <--- fix me!
array_shift($this->cards);
}
}
When that outer loop goes around again, it just straight overwrites the cards issued. Try something like this:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j][] = $this->cards[0]; // <- push into an array
array_shift($this->cards);
}
}
This one's creating and pushing the cards into an array for each player, rather than there being just the one (repeatedly overwritten) card.

Laravel - Insert a model multiple times

I have a model that I change some attributes I want to insert it but Eloquent, after the first save() will automatically do an update while I'm using save() method, here is my code:
for ($i = 0; $i < $range; $i++) {
$model->attr = "Some new value";
$model->save(); // after the first save() will do update but I want to an insert
}
You need to create a new instance of the Model every time you loop through. Try this:
for ($i = 0; $i < $range; $i++) {
$model = new Product;
$model->attr = "Some new value";
$model->save(); // after the first save() will do update but I want to an insert
}
I am not sure what your Model name is but I used Product in this instance. Replace this with your Model name.
You can use create
$attributes = [
'foo' => 'bar'
];
for ($i = 0; $i < $range; $i++) {
$attributes['foo'] = 'bar'.$i;
Model::create($attributes);
}
Or if you want create a function in your model:
public function saveAsNew(){
$this->exists = false;
$this->attributes[$this->primaryKey] = null; // reset the id
return $this->save();
}
Also I wrote this function, that saves the same model multiple times (yes I know that's not what your after but I wanted to post it anyways:
public function saveMultiple($times){
$saved = true;
for($i = 0; $i < $times; $i++){
if(!$this->save()){
$saved = false;
}
$this->attributes[$this->primaryKey] = null; // unset the id
$this->exists = false;
}
return $saved;
}

How to do PHP loops correctly

Write a class called math. It is to have one property called num. It also has one method (function) called factorial. This method is to start at 1 and multiply all of the integers to num. If num is 5 then you would multiply 1*2*3*4*5. Of course you are to do this in a loop.
Which loop should I use? For or do while? Also, do I need an inner loop?
I started with
For (i = 1; i <= 5; i++)
{
}
however, i'm stuck on what to do next...any suggestions?
You can do it using any loop. for loop can be converted to while and do .. while and opposite is true too.
for(i=0;i<5;i++)
is same as
i=0; while(i<5){i++;}
To to find the factorial you should multiply all the values from 1 to the number you want factorial of. So if $num = 5. Only one single loop is needed. You'd want to run this loop.
for($i=1;$i<$num;$i++){
$num*=$i;
}
I am not giving a full solution here because the question seems homework. If I give you full solution it will be spoon-feeding.
$result = 1;
$target = 5;
for ($i = 1;$ i <= $target; $i++)
{
$result *= $i;
}
echo $result;
or
$result = 1;
$target = 5;
while($target > 0) {
$result *= $target;
$target--; // You could do this all in one line, but for learners, this is clearer.
}
echo $result;
Each iteration you will want to multiply the total of the factorial by the value of $i
class Math {
public static function Factorial($factorial) {
$output = 1;
for($i = 2; $i <= $factorial; $i++)
$output *= $i;
return $output;
}
}
I've gotten to where I prefer the while(i--) loop:
<?php
class Math {
public $num = 0;
public function factorial() {
$result = 1;
$num = $this->num;
while ($num) {
$result *= $num--;
}
return $result;
}
}
$factor = new Math();
$factor->num = 5;
echo $factor->factorial();
?>
http://codepad.org/hUOgAoz2

How to iterate over an array of objects without foreach and ArrayAccess

I'm having to develop a site on PHP 5.1.6 and I've just come across a bug in my site which isn't happening on 5.2+. When using foreach() to iterate over an object, I get the following error: "Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference..."
Does anyone know how to convert the following foreach loop to a construct which will work with 5.1.6? Thanks in advance!
foreach ($post['commercial_brands'] as $brand)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $brand;
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}
Improving upon Coronatus's answer:
$max = count($post['commercial_brands']);
for ($i = 0; $i < $max; $i++)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$i];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}
You should never have a function in the condition of a loop, because each time the loop goes around it will run the function.
$x = 0;
$length = count($post['commercial_brands']);
while($x < $length){
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$x];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
$x++;
}
//while 4 eva
for ($i = 0; $i < count($post['commercial_brands']); $i++)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$i];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}

Categories