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();
jackjoesmith
  • 951
  • 4
  • 20
  • 33
  • Well, that - `$user->user_info -> $data->save();` is a very strange line... Does page report about any errors ? – BlitZ Sep 26 '15 at 06:27
  • i get an error exception: array to string conversion. How would it usually be written? – jackjoesmith Sep 26 '15 at 06:40
  • 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();`. – BlitZ Sep 26 '15 at 06:49
  • I get this error when i try that `Call to a member function save() on a non-object` – jackjoesmith Sep 26 '15 at 06:51
  • 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();`. – BlitZ Sep 26 '15 at 06:54
  • I suggest you to read [this question](http://stackoverflow.com/questions/22279435/what-does-mass-assignment-mean-in-laravel). The feature you asking is called *"mass assignment"*. Here is [general approach](http://stackoverflow.com/questions/17076156/how-to-mass-assign-an-update-in-laravel-4), but for laravel 4, however I think, that it might work for Laravel 5 as well. – BlitZ Sep 26 '15 at 07:07

1 Answers1

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;
}
num8er
  • 18,604
  • 3
  • 43
  • 57