php - Creating New Forms in Laravel 5 and Submitting Them -
i wish clarify steps new form submitted database using laravel 5.2 , bootstrap 3.
i have login/register pages set using laravel's defaults, , work fine. want create user profile page accessible authenticated users. using 1 row in database of user info. fields filled in during registration, , want them have access additional fields (while restricting access registration fields user name).
in example code below, there fields upload personal photo, enter first name, , enter last name. (none of these done during registration.)
what have done (all code below):
- create view profile.blade.php
- create controller profilecontroller.php
- update routes.php in controller directory.
a note:
- when try submit form appears below, get, type error: argument 1 passed app\http\controllers\profilecontroller::update() must of type array, none given.
what next steps required page working properly?
profile.blade.php:
@extends('layouts.app') @section('content') <div class="container" style="padding-top: 30px;"> <h1 class="page-header">user profile</h1> <div class="row"> <!-- left column --> <div class="col-md-4 col-sm-6 col-xs-12"> <div class="text-center"> <i class="fa fa-user fa-5x"></i> <h6>please upload photo...</h6> <input type="file" class="text-center center-block well-sm"> </div> </div> <!-- edit form column --> <div class="col-md-8 col-sm-6 col-xs-12 personal-info"> <form class="form-horizontal" role="form" method="post" action="{{ url('/profile') }}"> {!! csrf_field() !!} <div class="form-group"> <label class="col-lg-3 control-label">first name:</label> <div class="col-lg-8"> <input class="form-control" value="<?php echo auth::user()->firstname; ?>" id="firstname" name="firstname" placeholder="first..." type="text"> </div> </div> <div class="form-group"> <label class="col-lg-3 control-label">last name:</label> <div class="col-lg-8"> <input class="form-control" value="<?php echo auth::user()->lastname; ?>" id="lastname" name="lastname" placeholder="last..." type="text"> </div> </div> <div class="form-group"> <label class="col-md-3 control-label"></label> <div class="col-md-8"> <button type="submit" class="btn btn-primary"> <i class="fa fa-btn fa-user"></i>submit </button> <span></span> <input class="btn btn-default" value="cancel" type="reset"> </div> </div> </form> </div> </div> </div> @endsection
profilecontroller.php:
<?php namespace app\http\controllers; use app\http\requests; use illuminate\http\request; class profilecontroller extends controller { /** * create new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * show application dashboard. * * @return \illuminate\http\response */ public function index() { return view('profile'); } protected function update(array $data) { return user::update([ 'firstname' => $data['firstname'], 'lastname' => $data['lastname'], ]); } }
and added following in routes middleware:
route::get('/profile', 'profilecontroller@index'); route::post('/profile', 'profilecontroller@update');
it's protocol mismatch, since you're posting form. need change route
route::post('/profile', 'profilecontroller@index');
using validator great idea, since make sure input need be, , required fields filled out.
your update function should this:
public function update(request $request) { $first_name = $request->input('firstname'); $last_name = $request->input('lastname'); $id = auth::user()->id; $user = \app\user::find($id); $user->firstname = $first_name; $user->lastname = $last_name; $user->save(); return view('profile'); // sanitize, validate, before update // instead of returning update result, can instead show view or forward them page. }
Comments
Post a Comment