How do I update user profile? Laravel-5
Asked Answered
C

1

7

Just want to start by saying I have no clue what I'm doing... I have a user_info table that looks like this

    Schema::create('user_info', function(Blueprint $table){
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $table->string('address');
        $table->string('city');
        $table->string('state');
        $table->string('zip');
        $table->text('description');
        $table->text('experience');
        $table->timestamps();
    });   

I'm having trouble creating the update controller which looks like this right now.

public function update(Request $request)
{

  $user = $request->user();
  $data['description'] = $request->input('description');
  $data['experience']=$request->input('experience');

  $user->user_info -> $data->save();
}

again...no clue what I'm doing...

and this be my form:

    <div class='col-md-10 well form-well'>
        {!! Form::open(['method' => 'PATCH', 'action'=> ['UserController@update', Request::user()->id]]) !!}
        <div class='row'>
                <div class='form-group'>
                    <div class='col-md-2'>
                        {!! Form::label('description', 'About You')!!}
                    </div>
                    <div class='col-md-7'>
                        {!! Form::textarea('description', null, ['class'=>'form-control', 'rows'=>'3'])!!}
                    </div>
                </div>
        </div>
        <div class='row'>
                <div class='form-group'>
                    <div class='col-md-2'>
                        {!! Form::label('experience', 'Experience and Skills')!!}
                    </div>
                    <div class='col-md-7'>
                        {!! Form::text('experience', null, ['class'=>'form-control'])!!}
                    </div>
                </div>
        </div>
        <div class='form-group'>            
            {!! Form::submit('Save Changes',['class'=> 'btn btn-md btn-success']) !!}
            {!! Form::close()!!}
    </div>

Update: I was able to update it like this:

$user->user_info->description = $data['description'];
  $user->user_info->experience = $data['experience'];
  $user->user_info->save();

But is there a way I can do something like :

$user->user_info->$request::Input::all();
$user->user_info->save();
Cockchafer answered 26/9, 2015 at 6:6 Comment(6)
Well, that - $user->user_info -> $data->save(); is a very strange line... Does page report about any errors ?Radices
i get an error exception: array to string conversion. How would it usually be written?Cockchafer
I have no experence in Laravel, however, it is pure PHP message. I would suggest to look through manuals how it should be done properly. Also I would suggest to try something like just $data->save() or $user->user_info = $data->save();.Radices
I get this error when i try that Call to a member function save() on a non-objectCockchafer
Well, as of your latest update, you may try to loop through $request->input(...) and assign their values to $user->user_info, then commit it to $user->user_info->save();.Radices
I suggest you to read this question. The feature you asking is called "mass assignment". Here is general approach, but for laravel 4, however I think, that it might work for Laravel 5 as well.Radices
N
2

Try this:

public function update(Request $request, $id)
{
  $User = User::with('user_info')->find($id);
  if(!$User) {
    return response('User not found', 404);
  }

  $UserInfo = $User->user_info;
  if(!$UserInfo) {
    $UserInfo = new UserInfo();
    $UserInfo->user_id = $id;
    $UserInfo->save();
  }

  try {
    $values = Input::only($UserInfo->getFillable());
    $UserInfo->update($values);
  } catch(Exception $ex) {
    return response($ex->getMessage(), 400);
  }
}

also in Your UserInfo model add this:

protected $fillable = array('description', 'experience');
public function getFillable() {
  return $this->fillable;
}
Neodymium answered 26/9, 2015 at 6:56 Comment(19)
Is there a way I can do mass assignment instead? Something like : UserInfo ->request::input->all()->save()->Cockchafer
Also, my model includes address but that's to be submitted from another form. I don't want to have to use a different controller action for it.Cockchafer
I get user info does not existCockchafer
Have You defined relation in User model?Neodymium
Let us continue this discussion in chat.Neodymium
so i should probably do something like ``` if !UserInfo{ UserInfo->user_id=$id```?Cockchafer
i get an error saying Class 'App\Http\Controllers\UserInfo' not foundCockchafer
funny :) You see that UserInfo not found so in You controller file add: user App\UserInfo;Neodymium
lol that is funny. but now i get this : SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update user_info set _method = PATCH, _token = ' Is it saying i don't have a token column in my model?Cockchafer
that's because You're sending Input::all() to update :)Neodymium
Jack, have You checked?Neodymium
yeah sorry it still doesn't work. I'm still getting QLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update `user_info` set `_method` = PATCH, `_token` = VKUWdhmhflCenZVpGGL7iJ4Wkx0SSAasflu4Vdl6, `description` = fsdf, `experience` = fsef, `3` = , `updated_at` = 2015-09-26 07:53:04)Cockchafer
And I think one of the columns: '3' is the id number of the user.Cockchafer
have You added fillable to Your model?Neodymium
is this line ` $UserInfo = ($User->user_info)? $User->user_info : new UserInfo(); ` saying 'does user -> user_info exists? if not create one?'Cockchafer
yes, but now issue is when You're doing update(Input::all())Neodymium
Yeah I added the fillable alreadyy. I actually did this when I first made the model.Cockchafer
Now I'm not getting any errors, but its also not updatingCockchafer
I did var_dump(UserInfo->user_id) after the update and it returns null.Cockchafer

© 2022 - 2024 — McMap. All rights reserved.