Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alialbaker2007's avatar

@osherdo

For routines table you need to insert only name and type. so you should do something like

{!! Form::open(['route' => 'view_routine']) !!}  // i strongly recommend changing view_routine to something else e.g store

    <div class="form-group">
        {!! Form::label('type', 'Type') !!}
        {!! Form::select('type', ['Aerobic' => 'Aerobic', 'Anaerobic' => 'Anaerobic'],null ,['class' => 'form-control']); !!}
    </div>

    <div class="form-group">
        {!! Form::label('name', 'Name') !!}
        {!! Form::text('name', null, ['class' => 'form-control postcode',  'placeholder' => 'Name']) !!}
    </div>

    <div class="form-group">
            <button type="submit" name="submit" class="submit btn btn-primary">Submit</button>
    </div>

{!! Form::close() !!}

then in your controller you can just do this

$routine = Routine::create(Request::all());

what you have now is a form inside another form and you have one button for the nested form that post nothing to save_routine that is why you r getting null given error.

i suggest you get your form working first then worry about your script and keep your script away from your form

osherdo's avatar

@Ali_Ali ok, I have first changed the Form::open to this:

{!! Form::open(['action' => 'RoutineController@save']) !!}

So when submitting the form, I will launch the method to save the data to the database.

May I ask why should I change the name here?

{!! Form::open(['route' => 'view_routine']) !!} 

view_routine is where I redirect after submitting the form. Is it because it does not save the data in this case to the db?

Going further, I have placed the Request::all instead of this:

    $routine = Routine::create(Request::all());

In the controller.

Can I remove this line as well? (after the foreach loop) , since I do a request all now:

    $routine = Routine::create($routine);

I will place the js code outside the view to be an exetrnal script that is being included and let you know what's up in an hour or two. thanks very much!

alialbaker2007's avatar

@osherdo

ok so when you submit this form

{!! Form::open(['action' => 'RoutineController@save']) !!}

it goes to save method in the RoutineController

in the save method you need to redirect to the page you want which is probably view_routine in your case.

this

{!! Form::open(['route' => 'view_routine']) !!}

send post request to view_routine

1 like
osherdo's avatar

@Ali_Ali hey. I have all the image saving in the save method and then rediect, so it's ok.

I understand the differences now :)

The second one's just redirecting, without possibility to save the data to the db.

Moving on:

This is the updated controller's method code:

public function save() {
    
   $routine = Routine::create(Request::all());


    // get $exercise_id that you want to associate with this routine.
    $routine->exercise()->attach($exercise_id);
    // Attach the user to the routine 
    $routine->user()->attach($user_id );

    foreach($routine as $routine_id) 
    {
    // 1- process img upload to your server

    // 2- create Picture
    $exercise = new Exercises;
    $exercise->image_path = $img;
    $exercise->save();

    }
    $routine = Routine::create($routine);

    return redirect ('view_routine')->with(['user' => $this->user],'routine','exercise_id');

It returns an error for this line:

    $routine->exercise()->attach($exercise_id);
Undefined variable: exercise_id

I am not sure how to define this variable since we're getting all the exercises from the $routine variable, aren't we? this is where I am stuck now.

here's my update form as well: http://paste.ofcode.org/3aFiuNCBETgAnr2za3tiMSG

You can see I have now only one form (and not additional nested one).

I would like to know how to solve this?

thanks a lot!!

alialbaker2007's avatar

@osherdo

You need to put this line

$routine->exercise()->attach($exercise->id);

inside your foreach

osherdo's avatar

okay @Ali_Ali.

Should I place this line inside the foreach as well?

    // Attach the user to the routine 
    $routine->users()->attach($user_id );

Also, it returns an error for this variable in this line:

Undefined variable: user_id

What's the definition for this variable? should it be something like user()->'id'; ?

I am not sure what's the value there.

EDIT: it returns the same error even if I place this:

    $routine->users()->attach($user_id );

inside the foreach.

alialbaker2007's avatar

@osherdo

  1. If you only need to attach one user to one routine. which is what it looks like then you don't need to place
$routine->users()->attach($user_id );

and i can't see you creating any new use.

Do it in the method where you create new user.

Now coming back to $user_id You have to ask yourself

*do you want to attach the user to routine? *when do you want to do that

if you want to keep this line then you have to get the user id Auth::user()->id

osherdo's avatar

@Ali_Ali Ok I got you now, and understand it. Removed it outside the forach.

I will try to create another variable except user() , and define it like this, and use it: $var=Auth::user()->id;

osherdo's avatar

@Ali_Ali hi.

I have worked this through.

I am now tackling an issue: the data is inserted but not the the correct column.

exercise_id values are inserted to routine_idandroutine_idvalues are inserted toexercise_id``` .

why's that?

controller code:

public function create()
{
    $user = Auth::user();

    $user_id=Auth::user()->id;

    $input = Input::get('routine');

    $routine_type= Input::get('type');

    $routine_name = Input::get('routine_name');

    $routine = new Routine; // Creating A new object called Routine, coming from a eloquent model called Routine.php.
    $routine->routine_name = $routine_name;  ; // Get Input Dynamically. 
    $routine->routine_type = $routine_type;
    $routine->user_id = $user_id; // getting the id from the $user_id variable.
        $routine->save(); // Inserting the record inside the table.

    
    foreach($input as $exercise)
    {
          $exerciseRoutine = new ExerciseRoutine;
          $exerciseRoutine->user_id = $user_id;
          $exerciseRoutine->routine_id = $routine->id;
          $exerciseRoutine->exercise_id = $exercise;
          $exerciseRoutine->save();
    }

ExerciseRoutine.php model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ExerciseRoutine extends Model
{
    //
}

This is how the exercises are added to the array:

    //Restricting the user from adding more than 9 exercises.
                     var count = $('#sidebar-nav li').size(); // Find the number of <li> items added to the sidebar.
                        //console.log("count"+count);
                      if(count > 8)
                    {
                        alert('You cannot add more than 9 exercises');
                        return false; // Stoping the execution of the script. Just like exit() in php .
                    } 
                    
                        //console.log($('#sidebar-nav li').size());
                        
                    elem= '<li>';
                    // adding the exercises to an array ( [] )
                    elem+= '<input type="hidden" name="routine[]" value='+findId+'>';
                    // Now it equals to this:
                    //<li>  < input type="hidden" name="routine[]" value='+findId+'>
                    elem+= '<img src='+findImg+'>';
                    elem+= '<span>' + findText + '</span>';
                    elem+= '</li>';
                    //console.log("this is the element"+elem);
                    $('#sidebar-nav ul').first().append(elem); //  Overtime the exercise is clicked - the elem variable has a list item <li> </li> which is appended to the first UL item
                        // I pick the first ul element.

                    $('#sidebar-nav ul form button[type="submit"]').show();

I would appreciate your take on this one :)

Previous

Please or to participate in this conversation.