Again, close look at the exception. I will highlight the key words
FatalErrorException in RoutineController.php line 40: syntax error, unexpected '[', expecting ']'
'type'=> Input::get['type'],
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to insert images that are wrapped in a dedicated div. They're inserted to the database while they're in array.
<li>
<input class="filter" data-filter=".chest" type="radio" name="radioButton" id="radio5">
<label class="radio-label" for="radio5">Chest</label>
</li>
<!-- from the script -->
elem+= '<input type="hidden" name="routine[]" value='+findId+'>';
public function create(Request $request)
{
$user = Auth::user();
$routine = $user->routine()->create([
'aerobic'=>$request['routine[]'],
'anaerobic'=>$request['routine[]'];
]);
}
it returns an error about this line:
'type'=> Input::get['type'],
FatalErrorException in RoutineController.php line 40:
syntax error, unexpected '[', expecting ']'
In this line I am trying to get the input from here:
{!! Form::select('type', ['Aerobic' => 'Aerobic', 'Anaerobic' => 'Anaerobic']); !!}
please tell me why does it not submitting the data?
Again, close look at the exception. I will highlight the key words
FatalErrorException in RoutineController.php line 40: syntax error, unexpected '[', expecting ']'
'type'=> Input::get['type'],
I don't understand; so these images are what; standard exercise images? Why are you saving html tags into the database; can you share the user story here?
My instinct would be to have these image paths stored into their own table and create a pivot table to associate image(s) to the user.
@d3xt3r honestly this is the first time I insert an array of images from a function.
@tykus_ikus hey.
I will be try as much coherent as I can:
A user can create a routine that's consist from images of exercises he can choose from. the chosen exercises' images are moved to a sidebar with js. then from the sidebar they're submitted with a submit button. and this is where I am stuck.
I just want to store these img tags into the routines table. ( I also have a 2 pivot tables: user_routine (many users can have many routines) and routine_exercises (many routines have many exercises)).
Then I want to retrieve these images to another view - where use can view all of his routines.
That's the story, generally.
thanks for helping.
honestly this is the first time I insert an array of images from a function
The error so far as nothing to do with that. :) Its a SYNTAX ERROR
Hopefully, you have solved it. Its ...
// 'type'=> Input::get['type'], // change this to
'type'=> Input::get('type'),
First fix the above, then we shall figure out the rest (if any)
@tykus_ikus just tell me how would you approach this thing then, I would really like to know.
I am now debugging a sql query after getting over this with you ( and checking in the docs). :)
I am now encountering an error when trying to access the routine page: Trying to google meanwhile.
QueryException in Connection.php line 651:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`sportsapp`.`follower_followee`, CONSTRAINT `follower_followee_followee_id_foreign` FOREIGN KEY (`followee_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `follower_followee` (`followee_id`, `follower_id`) values (routine, 2))
Are you trying to store images in the database or display them in a custom division?
Edit: if you are trying to display images in a division it is really no different than displaying text or anything else and a division.
@jlrdw I am trying to store the images the user has chosen (store <img> tags).
I want them to view their selected images in another view.
Don't store the image tags store the filename of the image.
How can I insert these paths to the database?
In other words - how to access the <img src > atrributes of each image given?
(I have already got the in the script in an array (routine[]) - shown above).
You need to get the array of images first then use foreach loop
$imgs = Input::file('imgs'); // or $imgs = Request->file("imgs")
foreach($imgs as $img) {
// 1- process img upload to your server
$picture = new Picture;
$picture->url = $img;
$picture ->routine_id = $routine['id'];
$picture->save();
}
you can also use if (Input::hasFile('imgs')) to check if you user uploaded any images.
for image processing i suggest you use Intervention
@Ali_Ali thanks for replying!
The thing's the images are stored in a table by their paths, and not as an image, and I need to store selected images (being held in an array), to the pivot table , where I should place the user id and its images.
the array is called routine[] for this case.
If i understood your model correctly you should have something like this

If so then you can do this
foreach($imgs as $img) {
// 1- process img upload to your server
// 2- create Picture
$picture = new Picture;
$picture->path = $img;
$picture->save();
// 3- update the pivot table
$picture->rotine()->attach($rotine->id);
}
Jeffrey Way explain this in Many to Many relationship
I got you @Ali_Ali . I will implement this a bit later and later let you know here how it go.
And yes - my models relationship are defined just like that!
Could you just tell me about the upload part, and how I should catch the array from routine[] name attribute ?
I will look into the Many to Many relationship video soon.
Thank you !
so say you have this input in your blade
<input type="hidden" name="routine[]" value='{{ $routine->id }}'>
then in your controller you can do for each loop like this
$routines= Input::['routine'];
foreach($routines as $routine_id) {
// 1- process img upload to your server
// 2- create Picture
$picture = new Picture;
$picture->path = $img;
$picture->save();
// 3- update the pivot table
$picture->rotine()->attach($routine_id);
}
@Ali_Ali thanks very much for that!!
I am sorry I did not pointed that out on the first place - I have 3 models and 2 pivot table that connects between them: 3 models:
User.php
Routine.php
Exercise.php
and 2 pivot tables: user_routine - many users have many routines. routines-exercises - many routines have many exercises (which are the images actually).
I should do 2 actions when clicking on submit: 1) save the routine to the routines table, and here's its migration:
public function up()
{
Schema::create('routines', function (Blueprint $table)
{
$table->increments('id')->unsigned()->index();
$table->string('name');
$table->enum('type',array('aerobic','anaerobic'))->default('anaerobic');
$table->integer('user_id');
$table->rememberToken(); //http://stackoverflow.com/questions/23262351/laravel-remember-token
$table->timestamps();
});
}
public function up()
{
Schema::create('exercises',function (Blueprint $table)
{
$table->string('Anaerobic');
$table->string('Aerobic');
});
}
I have set all relations right in the 3 models.
I hope you could give me percise description for this , especially since I don't know how to make 2 actions on submit.
Also, is there a difference between using this:
$exercise_ids = $request->input('routine');
Instead of this?:
$routines= Input::['routine'];

save the routine to the routines table
$routine = Routine::create($routine); // where $routine is an array of the data that you want to insert into the routines table
Now you can *insert new record into routine_exercise *insert new record into user_routine
so
// get $exercise_id that you want to associate with this routine
$routine->exercise()->attach($exercise_id);
// get $user_id that you want to associate with this routine
$routine->exercise()->attach($user_id );
Every record in the pivot table are just to link one routine to one exercise in the routine_exercise table. The 2 main tables (routines and exercise) are not aware of each other.
the difference between
$exercise_ids = $request->input('routine');
and
$routines= Input::['routine'];
is that $request->input('routine'); can be use with your condition when you create your RoutineRequest.php but Input::['routine']; will take whatever the input is and use it without checking.
@Ali_Ali hi and thanks alot!
ok I have dropped the user_id column from the routines table, makes sense.
I have this function now, please notice the comments I have written and let me know if I got something wrong:
public function save(Request $request)
{
$routines= Input::['routine']; // Get the array of exercises from the view.
// Now iterate over them with a foreach.
foreach($routines as $routine_id)
{ // I am not sure how it is iterating over the array in the next 3 lines.
$exercise = new Exercises;
$exercise->path = $img;
$exercise->save();
// get $exercise_id that you want to associate with this routine.
$routine->exercise()->attach($exercise_id);
// get $user_id that you want to associate with this routine.
$routine->exercise()->attach($user_id );
}
$routine = Routine::create($routine); // creating the routine with its exercise in the pivot table.
return redirect ('view_routine')->with(['user' => $this->user],'routine'); // redirect to another view with the current user and routine variables passed.
It does return an error for this line:
$routines= Input::['routine'];
This is why I have asked about the difference.
I have tried with the $request instead and it returns this:
Undefined variable: routine (View: /var/www/sports-application/resources/views/routine.blade.php)
Please just tell me if the code's generally okay, or should I still use Input:: in some other way ( also tried with () ).
Also, why I get this undefined variable error, even if I am passing it to the view?
Would love to hear your opinion, and thanks again for the continuous help!
$routine->exercise()->attach($exercise_id);
what is $routine ? in the foreach. You need to
$routine = Routine::create($input);
$routine->exercise()->attach($exercise_id);
$routine->user()->attach($exercise_id);
*or get already created routine then attach it to exercise e.g
$routine = Routine::findOrFail($routine_id);
$routine->exercise()->attach($exercise_id);
$routine->user()->attach($exercise_id);
if you need to create only one routine then you should do it before the foreach.
// get $exercise_id that you want to associate with this routine.
$routine->exercise()->attach($exercise_id);
// get $user_id that you want to associate with this routine.
$routine->exercise()->attach($user_id );
in the above code you don't actually get anything you make new record in the pivot table to associate 2 records of 2 many to many relationship of 2 tables.
Request and Input does the same thing so in your case you can use anyone you like.
@Ali_Ali thanks for the reply. I have updated the code, accordingly, and understood your suggestions:
public function save(Request $request) {
$routine = Routine::create($input);
// get $exercise_id that you want to associate with this routine.
$routine->exercise()->attach($exercise_id);
// get $user_id that you want to associate with this routine.
$routine->exercise()->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();
}
return redirect ('view_routine')->with(['user' => $this->user],'routine');
It now returns an error about the variable $routine = Routine::create($input);
ErrorException in RoutineController.php line 74:
Undefined variable: input
I am wondering what's the value of $input?
should not I place instead the name attribute of the hidden field?
routine[]
instead?
Thanks, much appreciated!
yes ofc you need to define $input e.g
$input = Input::get('name_of_input');
//or
$input = Input::all(); // then use $input['name_of_input']
//or
$input = $request->all();
so if you use Input::all() then you don't need Request $request in your method argument
Also, in the 4th line below you need to attach the user to the routine not the exercise
// get $exercise_id that you want to associate with this routine.
$routine->exercise()->attach($exercise_id);
// get $user_id that you want to associate with this routine.
$routine->exercise()->attach($user_id );
Okay @Ali_Ali , I have changed it according to your advice:
public function save() {
$user = Auth::user();
$input = Input::get('routine[]');
$routine = Routine::create($input);
// 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)
{
$exercise = new Exercises;
$exercise->image_path = $img;
$exercise->save();
}
$routine = Routine::create($routine);
return redirect ('view_routine')->with(['user' => $this->user],'routine');
It returns an error now (googling this so meanwhile,will update if needed):
Argument 1 passed to Illuminate\Database\Eloquent\Model::create() must be of the type array, null given, called in /var/www/sports-application/app/Http/Controllers/RoutineController.php on line 77 and defined
I don't actually know where should I declare the array other than the Input::get line. I suspect it's in this line?
public function save() {
EDIT: I have written dd($input); and the result is null in the browser. maybe it does not get the array of images from the view?
Additional questions that I would like to be answered:
Does it matter when it comes to the order of attaching (first attach exercise to a routine, and then attach routine to a user)? can I write it the opposite?
And about these input source methods:
$input = Input::get('name_of_input');
//or
$input = Input::all(); // then use $input['name_of_input']
//or
$input = $request->all();
Is there an advantage to use the second $input over the first one?
And can I process data with the third one as well like the first two?
$input = Input::get('routine[]');
to
$input = Input::get('routine');
no it doesn't matter you will have the same result
if you have a form with multiple input then i think its better to use $input = Input::all() to get everything and use it in ::create($input); . but if you just need one specific input then you may use $input = Input::get('name_of_input');
yes you can. Use whatever make sense to you even if it's not the best way. Gradually you will find your way to the best and most efficient way of doing it
can you post your form ?
@Ali_Ali okay done it. when running dd($input); I am getting just null in the browser.
Thanks for the answers! I took my notes from it.
Can you tell me why it's null now when I want to get the array of images from the view? Code from the view:
<input type="hidden" name="routine[]" id="show_text_box" value='{{ $routine->id }}'>
(tried writing both routine[] and routine in the view)
EDIT: I forgot to mention I still get the same error:
Argument 1 passed to Illuminate\Database\Eloquent\Model::create() must be of the type array, null given, called in /var/www/sports-application/app/Http/Controllers/RoutineController.php on line 79 and defined
can i ask you which form is this in? and why do you need it? and why is it array?
<input type="hidden" name="routine[]" id="show_text_box" value='{{ $routine->id }}'>
if you are creating new routine then you don't need that cos the id auto increment. If it's in choose form then why is it hidden you should use select and options.
@Ali_Ali I am inserting to the tables , and it may cause to get a null array:
Here I get the name and type of routine:
{!! Form::open(['route' => 'view_routine']) !!}
Pick your routine type: {!! Form::select('type', ['Aerobic' => 'Aerobic', 'Anaerobic' => 'Anaerobic']); !!} <br><br>
Give your routine a name: <input type="text" name="routine_name" required>
And going down in the form, I have the hidden field, js code to copy chosen images to a sidebar, and form closing:
<div id="sidebar-nav">
<ul>
<form action="{{ route('save_routine') }}" method="post">
{!! csrf_field() !!}
<!-- content will come here from js as li elements -->
<button id="submit_routine" type="submit" name="submit_routine">Submit</button>
</form>
</ul>
</div>
<script>
$(function(){
$('#toggle-button a').click(function(e){
$('#sidebar-nav').toggleClass('showSidebar'); // Shows the side bar (from the right, when clicking on the button, with the help of css).
$('main').toggleClass('pushWrapper'); // This makes the transition for the sidebar in the css file.
e.preventDefault(); // Prevent the anchor tag to refresh the page or taking it to other page , with e.preventDefault(), same as the javascript:; did.
});
})
</script>
<!-- end of html structure for sidebar routine. -->
{!! Form::close() !!}
<script type="text/javascript">
(function($) {
$("#submit_routine").on('click', function() {
});
})(jQuery);
</script>
It's all wrapped in a form so all the data could be sent to the database once I click the submit button of the form. Should I remove the [] from routine[] name attribute in the form?
It's hidden since the chosen exercises are shown in sidebar that's showing on click, and does not show by default on page load.
Get the idea? I hope you'd know what's wrong here now ! :)
Thanks!
Please or to participate in this conversation.