2
votes

I've managed to retrieve and display the Auth user profile details in my profile view, but I'm missing something (or a few things) to do the actual update.

Can you help?

profile.blade.view

    <div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading">Update your user profile</div>

            <div class="panel-body">                    

            {!! Form::open(array('action' => array('HomeController@postProfileEdit', $user->id))) !!}

            <div class="form-group">
            {!! Form::label('firstName', 'First name:') !!}
            {!! Form::text('firstName', $userinfo->first_name, ['class' => 'form-control']) !!}

            {!! Form::label('lastName', 'Last name:') !!}
            {!! Form::text('lastName', $userinfo->last_name, ['class' => 'form-control']) !!}
            </div>

            <div class="form-group">
            {!! Form::submit('Update', ['class'=>'btn primary']) !!}
            </div>

            {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>

routes.php

Route::get('profile', 'HomeController@profile');
Route::post('profile', 'HomeController@postProfileEdit');

HomeController.php - profile()

/**
 * Show the application user profile to the user.
 *
 * @return Response
 */
public function profile()
{
    $user = Auth::user();
    $userinfo = \Auth::user();

    return view('profile')->with(['user' => $user])->with('userinfo', $userinfo);
}

HomeController.php - postProfileEdit(Request $request)

/**
 * Store updates to user profile.
 *
 * @return Response
 */
public function postProfileEdit(Request $request)
{

    $userinfo = Request::all();
    $user = new User;
    $user->exists = true;
    $user->id = Auth::user();
    $user->first_name = $userinfo['firstName'];
    $user->save();

    return redirect()->action('HomeController@profile');

    //return Response::make('Your user profile was updated successfully!');
}

requests.php - for validation

abstract class Request extends FormRequest {

/**
 * The URI to redirect to if validation fails
 *
 * @var string
 */
protected $redirect = 'users/create';

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'firstName' => 'required|min:3',
        'lastName' => 'required|min:3'
    ];
}

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return false;
}

}

I'm getting:

FatalErrorException in HomeController.php line 170: Class 'App\Http\Controllers\updateProfile' not found

I know the $updateProfile = new updateProfile;

line is wrong, I'm just not sure what is wrong.

Hope this makes sense.


Update:

I changed the class from updateProfile to User (name of the model where I'm doing the change) in HomeController.php

Now I get the following message:

ErrorException in HomeController.php line 174: Undefined variable: userinfo

I'm not sure I can use userinfo from profile.blade.view (see above).


Update

I was able to make it work (or not get an error) by fixing HomeController@postProfileEdit (post), and return to HomeController@profile (get) when done. But apparently, the save did not get done because even though I changed the first_name value, it stays the same when I go back to HomeController@profile (where first_name is retrieved and displayed).

2
Is it displaying any error? is it getting the route right? - Danilo Kobold
@DaniloKobold Yes, sorry, I'm getting: FatalErrorException in HomeController.php line 170: Class 'App\Http\Controllers\updateProfile' not found . I know the $updateProfile = new updateProfile; line is wrong, I'm just not sure what is wrong. Thanks - user3489502

2 Answers

2
votes
Class 'App\Http\Controllers\updateProfile' not found

This error means that Laravel is looking for updateProfile class in current App\Http\Controllers namespace but cannot find it. I guess that class is in different namespace so you need to add use statement at the beginning of the HomeController file, e.g.:

<?php

namespace App\Http\Controllers;

use Some\Namespace\updateProfile;
1
votes

Building on @jedrzej.kurylo's answer.

I have to assume the updateProfile is an Eloquent Model found in the app directory:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class updateProfile extends Model {
    ...
}

Notice the namespace declaration at the top of the file? Next, consider the namespace your HomeController.php file:

namespace App\Http\Controllers;

By default, when constructing a class in Laravel, it looks in the current namespace of the controller you're working with. In this case, its trying to find a class updateProfile that is in the App\Http\Controllers namespace, which we know doesn't exist.

There are two ways to accomplish what you're trying to do. First, use a use statement at the top of your controller for each Model you're including.

use App\updateProfile;
...

Secondly, specify the namespace while declaring the class:

$updateProfile = new \App\updateProfile;

Either method should clear the error for you.