$user = User::find($id);
$user->update($input);
// can be joined on one line
$user = User::find($id)->update($input);
Other than that I don't see anything wrong. You can also use
$user = User::find(1);
$use->username = 'john';
$user->email = '[email protected]';
$user->save();
Did you set
['method' => 'PATCH']
on your form?
Yes I did use the ['method' => 'PATCH']. I tried the code you gave and I got a "Whoops, looks like something went wrong."
Here is my edit
@extends('users.scaffold')
@section('main')
<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.update', $user->id))) }}
<ul>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::text('password') }}
</li>
<li>
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone:') }}
{{ Form:: text('phone') }}
</li>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ link_to_route('users.show', 'Cancel', $user->id, array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
@if($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif
My model
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//ADDING A NEW USER
protected $guarded = array('id');
protected $fillable = array('username', 'password', 'email', 'phone', 'name', );
public static $rules =array(
'name' => 'required|min:5',
'email' => 'required|email',
'password' => 'required'
);
//EDITING A USER
//$user = User::find($id);
$user = User::find($id)->update($input);
}
No no, why would you put
$user = User::find($id)->update($input);
inside your model? Ok you could, but inside a method, but I meant inside your controller.
if($validation->passes()){
$user = User::find($id)->update($input);
}
And you should enable error reporting for development. Go to /app/config/app.php and change 'debug' => false to debug' => true. Remember to disable that when your site goes live.
Hi pogachar
I added that code to the controller and it worked. Thanks soo much. For some reason I must have misunderstood the tutorial I was busy going through.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community