lundi 11 juillet 2016

Edit profile page using laravel

Whenever a user goes to http://ift.tt/2a4fX2Y I want a form to pre populate the values they've entered during the signup/registration flow.

I was able to do that with my form here

<h1><b>Your Profile</b></h1>
<form method="POST" action="/profile/update">
    <div class="form-group hidden">
        <input type="hidden" name="_token" value="">
        <input type="hidden" name="_method" value="PATCH">
    </div>
    <div class="form-group ">
        <label for="email" class="control-label"><b>Name:</b></label>
        <input type="text" name="name" placeholder="Please enter your email here" class="form-control" value=""/>

        <?php if ($errors->has('name')) :?>
        <span class="help-block">
            <strong></strong>
        </span>
        <?php endif;?>

    </div>
    <div class="form-group ">
        <label for="email" class="control-label"><b>Email:</b></label>
        <input type="text" name="email" placeholder="Please enter your email here" class="form-control" value=""/>

        <?php if ($errors->has('email')) :?>
        <span class="help-block">
            <strong></strong>
        </span>
        <?php endif;?>

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

But now im running into the issue if the user just hits submit button without changing any fields, laravel's built in validations checks and gives me an error message of The name has already been taken. which is true. so im not sure how to handle this issue, but here is my controller below

<?php

namespace App\Http\Controllers;

use Auth;
use App\User;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    /**
     * Update user profile & make backend push to DB
     * 
    **/

    public function index() {

        /**
         * fetching the user model
         **/
        $user = Auth::user();
        //var_dump($user);

        /**
         * Passing the user data to profile view
         */
        return view('profile', compact('user'));

    }

    public function update(Request $request) {
        /**
         * Validate request/input 
         **/
        $this->validate($request, [
            'name' => 'required|max:255|unique:users',
            'email' => 'required|email|max:255|unique:users',
        ]);

        /**
         * storing the input fields name & email in variable $input
         * type array
         **/
        $input = $request->only('name','email');

        /**
         * fetching the user model
         */
        $user = Auth::user();

        /**
         * Accessing the update method and passing in $input array of data
         **/
        $user->update($input);

        /**
         * after everything is done return them pack to /profile/ uri
         **/
        return back();
    }
}

and here is my routes file.

// only authenticated users
Route::group( ['middleware' => 'auth'], function() {

    Route::get('/home', 'HomeController@index');

    // practicing using forms for sending data to the DB & populating form fields with DB data
    Route::get('profile', 'ProfileController@index');
    Route::patch('profile/{id}', 'ProfileController@update');

});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire