In my home page in my Laravel project I have a modal which contains a form.
<form id="profilesubmitform" method="POST" action="/profilesubmit"> <div class="form-group"> <input type="textarea" class="form-control" id="bio" placeholder="Enter your bio here"> <small class="form-text text-muted">Tell us about yourself!</small> </div> <div class="form-group"> <input type="text" class="form-control" id="profileurl" placeholder="Website/Link"> <small class="form-text text-muted">Add your site, Carrd, Youtube channel, anything!</small> </div> <button type="submit" class="btn btn-primary">Submit</button> </form>
And of course in my web.php file I’ve made a route like so:
Route::post('/profilesubmit', 'AppHttpControllersProfilesController@profileSubmit');
It’s referring to the function in my ProfilesController that should take the data from the form and save it to my database.
public function profileSubmit(Request $request){ $user = Auth::user(); $user->bio = $request->bio; $user->profileurl = $request->profileurl; $user->save(); return redirect('home'); } }
The $user
variable should be getting the authenticated (logged in) user in the database. The purpose of this all is that so they can submit a bio and URL that will eventually be put on their profile but for now It’s just being put into the ‘bio’ and ‘profileurl’ columns in the table.
However when I submit the form it adds nothing. Notably, it’s saying that “save()” is an undefined method which is odd because it only gets caught as an error when I define $user as “Auth::user();”. Any help?
Advertisement
Answer
You need to add name
attribute for each <input/>
Only form elements with a name attribute will have their values passed when submitting a form.
Also add {{ csrf_field() }}
for AntiCSRF
here I’ve rewritten your code.
<form id="profilesubmitform" method="POST" action="/profilesubmit"> {{ csrf_field() }} <div> <input type="textarea" id="bio" name="bio" placeholder="Enter your bio here"> <small>Tell us about yourself!</small> </div> <div> <input type="text" id="profileurl" name="profileUrl" placeholder="Website/Link"> <small>Add your site, Carrd, Youtube channel, anything!</small> </div> <button type="submit">Submit</button> </form>