Thats all I can Give you really because that is the specific error. Basically I am trying to use the if statements variable outside of the statement but which is inside of the method.
@JLRDW - I forgot to paste that code in. Sorry. Here it is.
public function getStuff(Request $request){
//Do some savings of data here
$insert = new Stuff;
$insert->free_meal = $request->free_meal;
$insert->save();
if ($insert->free_meal == 1){
$push = 'Guest is also eligible for a Free Meal';
}
return $push;
}
So after the save, I am trying to access that data using $insert->free_meal
Ok so for clearence here is the full code sample of what I am trying to do
public function getStuff(Request $request){
//Do some savings of data here
$insert = new Stuff;
$insert->free_meal = $request->free_meal;
$insert->save();
if ($insert->free_meal == 1){
$push = 'Guest is also eligible for a Free Meal';
}
$username = '121222121';
$password = '121222121';
$client = new \SmsSender($username, $password);
$message = $client->send(
'0000000000', //This is where the mobile number goes
$push // Message
);
}
I hope this clears the confusion. I am trying to take that variable in to something else which is down below. I am trying to send that variable as the message inside the message sender.
This isn't about scope since the variable is defined inside the same function.
The if condition is still not executed and you need to find out why.
Based on that code I would guess that $request->free_meal is not actually 1. Or it isn't saved into the $insert object.
I'd put a dd($insert->free_meal); right in front of the if condition to check it's actual value.
That said: It would still be a good idea to initiate the $push variable somewhere above since it makes it easier to read your function and what it is supposed to do.
@FTIERSCH - Actually you are right. It was a dumb mistake on my side. it was not returning 1 because I had a small issue while adding the free_meal to the db. So nothing was being passed. That was the whole issue here. Thanks!
@pickab00 you should still initialize $push before the conditional like many others above have told you to. If that conditional fails you're going to run into the same issues with the variable not being defined. But if the conditional always evaluates to true, then why do you even have it?