swap it for an if statement
if(function_exists($this->recurrence)) {
$this->$recurrence($newTask);
}
// here do whatever you want to do when someone submits an invalid recurrence (because they are messing with the code)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
swap it for an if statement
if(function_exists($this->recurrence)) {
$this->$recurrence($newTask);
}
// here do whatever you want to do when someone submits an invalid recurrence (because they are messing with the code)
Thanks @snapey so am i correct in thinking the controller should be laid out as follows... the only reason i ask is that i havent done much/if any with using separate private functions before.
Also i'm getting an error of //Error: Undefined property: App\Http\Controllers\TaskController::$recurrence'
public function store(Request $request)
{
$newTask = $request->validate([
// Validation Here...
]);
$recurrence = strtolower($request->recurrence);
if(function_exists($this->recurrence)) // <----------------------- Error here
{
$this->$recurrence($newTask);
}
}
private function daily($newTask)
{
//
}
private function weekly($newTask)
{
//
}
private function monthly($newTask)
{
//
}
private function createEvent($at,$newTask)
{
App\Event::create([
'task_id' => $newTask->id,
'customer_id' => request()->customer_id,
'start_time' => $at,
'end_time' => $at,
'recurrence' => request()->recurrence,
]);
}
Thank you again for your help on this.
Please note
$this->$recurrence($newTask);
$ before recurrence
You are calling the function whose name is in the $recurrence variable
Thanks @snapey, strangely though i now get an Error Exception of Undefined property::$monthly
I've tried changing the private function monthly($newTask) to a public function but still no joy.
can you copy and paste your code here
Hey @snapey. Of course...
$newTask = $request->validate([
//Validation Here...
]);
$recurrence = strtolower($request->recurrence);
if(function_exists($this->$recurrence))
{
$this->$recurrence($newTask);
}
}
public function daily($newTask)
{
$at = now();
foreach(range(1,365) as $x) {
$at->addDay();
$this->createEvent($at,$newTask);
}
}
public function weekly($newTask)
{
$at = now();
foreach (range(1, 52) as $x) {
$at->addDays(7);
$this->createEvent($at, $newTask);
}
}
public function monthly($newTask)
{
$at = now();
foreach (range(1, 12) as $x) {
$at->addMonth();
$this->createEvent($at, $newTask);
}
}
public function quarterly($newTask)
{
$at = now();
foreach (range(1, 4) as $x) {
$at->addMonth();
$this->createEvent($at, $newTask);
}
}
public function createEvent($at,$newTask)
{
$task = array(
'ref' => request()->ref,
'start_time' => $at,
'recurrence' => request()->recurrence,
);
\App\Task::create($task);
return redirect...
}
```
Sorry, my mistake, use this line instead
if (method_exists($this, $recurrence)) {
also change this to just return;
return redirect...
Awesome thanks @snapey.
Can i ask why you would use just return? I would prefer to redirect to a view with a flash message to say that the records have been added.
Instead i get a blank page.
Thanks in advance.
return in the create event method
return in the monthly/daily/weekly
return with redirect in the controller method
@snapey You are a legend. Thank you very much for all of your help on this much appreciated.
Hey @snapey sorry to resurrect this but i just had a quick question.
based on the foreach loop you suggested previously, its currently sending the following to the database
User selects 28/07/2020 in the <input> field
//foreach loop called...
//Database inputs
--28/08/2020
--28/09/2020
--etc
Is there a way to insert the date that was specified 28/07/2020 plus the iterations of the loop as currently its jumping ahead a month + iterations as soon as its submitted.
Thanks in advance.
public function monthly($newTask)
{
$at = now();
foreach (range(1, 12) as $x) {
$at->addMonth();
$this->createEvent($at, $newTask);
}
}
starts at $at=now()
instead, pass in the start date or get it direct with the request helper.
Make sure its a carbon instance
If you want the first event to be the date specified then swap the order of the statements.
public function monthly($newTask)
{
$at = Carbon::parse(request()->start);
foreach (range(1, 12) as $x) {
$this->createEvent($at, $newTask);
$at->addMonth();
}
}
Awesome Thanks @snapey. I didnt think it would be as simple as swapping the statements.
Thank you again.
Please or to participate in this conversation.