LearnProgramming
LearnProgramming

Reputation: 806

Laravel query with if condition

I wanted to insert data into table if the condition is met. Is there such things as this codes below?

DB::table('food')->insert([
    'dessert' => $dessert1,
    if(beverage == 1)
        'drinks' => 1;
    else if(beverage == 2)
        'drinks' => 2;
]);
...

Upvotes: 0

Views: 2950

Answers (7)

Atiqur
Atiqur

Reputation: 4022

Do your condition checking first, that will make your code clear and readable like @RJParikh

$drinks = 0;
if(beverage == 1)
   $drinks = 1;
else if(beverage == 2)
   $drinks = 2;

Or if you have limited number of entry do like @Zayn Ali

$drinks = [ 1 => 1, 2 => 2 ]; // $beverageValue => $drinksValue

Then use insert -

DB::table('food')->insert([
    ......,
    ......,
]);

But keep in mind if you use insert like this you have to make the fields fillable from model. Better you make a model on food table and follow @Firas Rassas answer. Combining answers your code will look like below -

 $drinks = 0;
    if(beverage == 1)
       $drinks = 1;
    else if(beverage == 2)
       $drinks = 2;

    $food = new Food(); // new instance of food model
    $food->dessert = $dessert1;
    $food->drinks = $drinks;
    $food->save();

Upvotes: 0

Zayn Ali
Zayn Ali

Reputation: 4915

A simple way would be to make an array of predefined values as beverage => drink

$drinks = [ 1 => 1, 2 => 2 ];

DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => $drinks[$beverage];
]);

Upvotes: 1

Maitray Suthar
Maitray Suthar

Reputation: 273

You can also make condition in DB like this

$query = DB::table('food');

$query->insert(['dessert' => $dessert1]);

if(beverage == 1)
    $query->insert(['drinks' => 1]);
else if(beverage == 2)
    $query->insert(['drinks' => 2]);

Upvotes: 0

apokryfos
apokryfos

Reputation: 40653

You either need to pre-determine the value with the if outside the insert:

$values = [ "dessert" => $dessert1 ];
if($beverage == 1) //How on earth did you ever expect if (beverage == 1) to work in anything?
     $values['drinks'] = 1;
else if($beverage == 2)
    $values['drinks'] = 2;

DB::table('food')->insert($values);

You can also determine the value inline using the ternary operator (?:):

DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => ($beverage==1?1:($beverage==2?2:null));
]);

In your case the following may also work:

DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => $beverage
]);

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you can try this:

if($beverage == 1){
  DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => 1,

]);

}

if(beverage == 2){
  DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => 2,

]);

}

OR

$food = new Food;
$food->dessert = $dessert1;

if($beverage == 1){
  $food->drinks = 1;
}

if($beverage == 1){
  $food->drinks = 2;
}

$food->save();

Upvotes: 0

Firas Rassas
Firas Rassas

Reputation: 509

You can try this: (Food is your model)

$food = new Food(); // new instance of food model
$food->dessert = $dessert1;
if(beverage == 1)
 $food->drinks = 1;
else if(beverage == 2)
 $food->drinks = 2;
$food->save();

That's how you handle if conditions with query, but according to your question you can do the following:

DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks' => $beverage,
]);

Upvotes: 2

RJParikh
RJParikh

Reputation: 4166

Use like this way:

Code:

$drinks = 0;
if(beverage == 1)
   $drinks = 1;
else if(beverage == 2)
   $drinks = 2;

DB::table('food')->insert([
    'dessert' => $dessert1,
    'drinks'  => $drinks
]);

Upvotes: 1

Related Questions