1

I'm working in Laravel 5.2, and using the Eloquent ORM for dealing with Database. I tried to update user data in database but when I click on my UPDATE button it gives me the error below. I am using the PUT method for updating the data in database as per Laravel REST rules.

Error URL :

http://localhost/users?_token=wazgR1tQaznQwRdejXdx4g3jLgbtlfPLIeIiXdRy&name=warka&email=&password=

Error:

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404

localhost Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8

User Controller:

   <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Facades\Input;

use Illuminate\Support\Facades\Redirect;

use Illuminate\Support\Facades\View;

use App\User;

class UsersController extends Controller
{
    public function create(){
        return view('create');
    }

    public function store(){
        $rules = array(
            'name' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
        $validator = Validator::make(Input::all(),$rules);

        if($validator->fails()){
            return Redirect::to('http://localhost/laravelking/users/create')->withInput()->withErrors($validator);
         }else{
            User::create(array(
                'name' => Input::get('name'),
                'email' => Input::get('email'),
                'password' => Input::get('password')
            ));
            return Redirect::to('http://localhost/laravelking/users');
        }

    }

    public function index(){
        return view::make('users')->withUsers(User::all());
    }
    public function show($id){
        $user = User::find($id);

        if($user == null){
            return Redirect::to('http://localhost/laravelking/users');
        }else{
            return View::make('profile')->withUser($user);
        }
        return 'list '.$id;
    }
    public function update($id){
        $rules = array(
            'name' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
        $validator = Validator::make(Input::all(),$rules);

        if($validator->fails()){
            return Redirect::to('http://localhost/laravelking/users/'.$id.'/edit')->withInput()->withErrors($validator);
         }else{
            $user = User::find($id);
            if(Input::has('name')) $user->name = Input::get('name');
            if(Input::has('email')) $user->email = Input::get('email');
            if(Input::has('password')) $user->password = Input::get('password');
            $user->save();
            return Redirect::to('http://localhost/laravelking/users/'.$id);
        }
    }
    public function edit($id){
        $user = User::find($id);
        if($user == null){
            return Redirect::to('http://localhost/laravelking/users');
        }else{
            return View::make('edit')->with('id',$id);
        }
    }

    public function delete($id){
        return 'list'.$id;
    }
}

View Update Form :

<form role="form" method="PUT" action="users/".$id>
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="form-group">
    <label for="username">New Name:</label>
    <input type="username" class="form-control" name="name" id="name">
  </div>    
  <div class="form-group">
    <label for="email">New Email address:</label>
    <input type="email" name="email" class="form-control" id="email">
  </div>
  <div class="form-group">
    <label for="pwd">New Password:</label>
    <input type="password" name="password" class="form-control" id="pwd">
  </div>
  <div class="checkbox">
    <label><input type="checkbox"> Remember me</label>
  </div>
  <button type="submit" class="btn btn-default">Update</button>
</form>

Profile VIEW From Where I enter to update View is :

<div class="container">
    <div class="col-md-8 col-lg-8 col-sm-12">
        <div class="jumbotron">
            <h1> Hello {!! $user->name !!}</h1>
            <h3> Your Email is {!! $user->email !!}</h3>
            <h3 style="color:red"> Your Password is {!! $user->password !!}</h3>
            <h1> {!! Html::link('users/'.$user->id.'/edit','Edit ') !!}</h1>
        </div>
    </div>
</div>

Route :

Route::group(['middleware' => ['web']], function () {
    //
    Route::resource('users','UsersController');
});

Updated :

Just changed the Form Method to : <form role="form" action='http://localhost/laravelking/users/<?php echo $id; ?>' method="PUT">

I.e $id to PHP echo and it worked but data in DB is not updated !

The New URL now looks like this:

http://localhost/laravelking/users/1?_token=wazgR1tQaznQwRdejXdx4g3jLgbtlfPLIeIiXdRy&name=Lololololol&email=Lololololol%40gaic.com&password=Lololololol

But problem is data is not updated in DB

7
  • How does your routes file look like? If he doesn't find the requested page this should be a missing or wrong route. Commented Mar 11, 2016 at 7:25
  • 1
    yes but the routes are still wrong if you get an 404 Error from http://localhost/users/4/users?_token=wazgR1tQaznQwRdejXdx4g3jLgbtlfPLIeIiXdRy&name=John+Doe&email=john%40gmail.com&password=john1234 then this meanshttp://localhost/users/4/users doesn't even exist. Which means it can't find a route for that. This is not a Laravel error, the 404 comes from your browser saying that it can't find the requested URL. Commented Mar 11, 2016 at 7:38
  • @Peh that's not the problem buddy can you take a deeper look ! Thanks Commented Mar 11, 2016 at 8:02
  • 2
    can you show your routes for the Users update in your question Commented Mar 11, 2016 at 8:03
  • @Qazi check I have updated the question! Commented Mar 11, 2016 at 8:06

1 Answer 1

1

Your application is in the laravelking directory, but your form is sending the request just to http://localhost.

You better change your form action to something like this:

 <form action="{{url('users/' . $id)}}" ... >

Because HTML forms does not support PUT, PATCH and DELETE methods you need to add an additional field to make this work:

<input name="_method" type="hidden" value="PUT">

Or you can use some of the Laravel helpers for this:

// Plain PHP
echo method_field('PUT'); 

//Blade template engine
{{ method_field('PUT') }}

You can read more about this here: Laravel HTTP Routing - Method spoofing

Sign up to request clarification or add additional context in comments.

6 Comments

I am sending request to localhost/laravelking/users/<?php echo $id; ?> specific ID Not to Localhost and I tried that hidden field input BUT nothing is updated in database !
Are you triggering your update method? Is the validation passing? Debug a little and give us more information. Check if there are any validation errors. If you are not changing the name and email the unique validation will fail.
yes update method ! And I even removed the validation code still it is not updating DB data!
Just dd() the $user object after the find() method, then after you set the fields and then check what is returned by the save() method and you'll understand where it fails. Also check what sql query is executed.
Thanks for the help but all I did was just replace the HTML5 Form tag Opening and closing with Blade Template {!! Form::open(array('url' => 'users/'.$id, 'method' => 'put' , 'role' => 'form' )) !!} //My Form Data with Styles {!! Form::close() !!} and it worked like a charm!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.