0

In my Laravel project I use this code into controller method to save data:

    $countCourseTeacher++;
    $routine->p1=Input::get('course_teacher' . $countCourseTeacher); 
    $routine->p2=Input::get('course_teacher' . $countCourseTeacher); 
    $routine->p3=Input::get('course_teacher' . $countCourseTeacher);

But I want to use $routine->p$i into a for loop Like

for ($i=1; $i <=6; $i++) {
               $countCourseTeacher++;
               $routine->p$i=Input::get('course_teacher' . $countCourseTeacher);
               }

When I use $routine->p$i instead of using $routine->p1 , $routine->p2, $routine->p3..... It gives error. Is there any way to do this into a forloop using p$i???

2 Answers 2

1

That is the normal behaviour in PHP. You can use interpolation. If you want to use interpolation for object properties/methods, use brackets around the string. Here are a quick example:

for ($i=1; $i <=6; $i++) {
    $countCourseTeacher++;
    $routine->{"p$i"} = Input::get('course_teacher'.$countCourseTeacher);
}
0
0

Any Way I find My answer myself. I use thos code below

for ($j=1; $j <=6; $j++) {
       $routine=new RoutineCreate;
       $routine->configuration_id=$idcnfg; 
       $routine->day=Input::get('day' . $j);
       for ($i=1; $i <=10; $i++) {
        $pi="p" . $i;
       $countCourseTeacher++;

       $routine->$pi=Input::get('course_teacher' . $countCourseTeacher);
       }
       $routine->save();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.