Support the ongoing development of Laravel.io →
Input Database Eloquent
Last updated 2 years ago.
0
$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?

Last updated 2 years ago.
0

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);

}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiva shiva Joined 24 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.