0

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;
]);
...
3
  • 1
    Check this condition above insert query and use that variable Commented Aug 1, 2017 at 7:51
  • You have to write code withing if..else..if not like you wrote i.e. if..else..if not acceptable under query. Let me if you required more explanation. Commented Aug 1, 2017 at 7:52
  • what is beverage where do you get it ? if it is a variable you can just do drinks=$beverage Commented Aug 1, 2017 at 8:58

7 Answers 7

2

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,
]);
Sign up to request clarification or add additional context in comments.

1 Comment

This is best way.
1

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
]);

Comments

1

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];
]);

Comments

0

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();

Comments

0

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
]);

Comments

0

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]);

1 Comment

i tried your way, it seems that only "desert" manage to stored in "food table" but not "drinks". "Drinks" return null in "food table".
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.