Creating a login application and getting a ModelNotFoundException error in Laravel 5

I am new to PHP and Laravel. I am making a Laravel logging application that prints "success" after comparing the entered value and the database value.

My problem: when both values ​​match, it prints "success", but when they do not match, it throws a ModelNotFoundException .

I created an exception for this, but the same error occurs again.

Thank you in advance!

Here is my route.php code

route.php

Route::get('register', ' RegisterController@register '); Route::post('register/show', ' RegisterController@show '); Route::post('register/store', ' RegisterController@store '); Route::get('register/login', ' RegisterController@login '); 

Here is my register controller, which gets the value from index.blade.php and from a database with a register name that has two columns username and password

RegisterController.php

  <?php namespace App\Http\Controllers; use App\Http\Requests; use App\Register; use App\Http\Controllers\Controller; use Illuminate\Database\Eloquent\ModelNotFoundException; use Request; class RegisterController extends Controller { public function register() { return view('register.index'); } public function store() { $input = Request::all(); Register::create($input); return redirect('register'); } public function show() { try { $result2 = Request::get('username'); $result = Register::where('username', $result2)->firstOrFail(); return view('register.show', compact('result','result2')); } catch(ModelNotFoundException $e) { return "login fail" . redirect('register/login'); } } public function login() { return view('register.login'); } } 

register.php

  use Illuminate\Database\Eloquent\Model; class Register extends Model { protected $fillable = ['username', 'password']; } 

CreateRegistersTable

  <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateRegistersTable extends Migration { public function up() { Schema::create('registers', function(Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('password'); $table->timestamps(); }); } public function down() { Schema::drop('registers'); } } 

index.blade.php

 @extends('master') @section('login') {!! Form::open(['url' => 'register/store']) !!} {!!Form::label('username','Name:')!!} {!!Form::text('username', null)!!} <br> {!!Form::label('password','Password:')!!} {!!Form::text('password', null)!!} {!!Form::submit('submit')!!} {!! Form::close() !!} @stop 

login.blade.php

  @extends('master') @section('register') {!! Form::open(['url' => 'register/show']) !!} {!!Form::label('username','Name:')!!} {!!Form::text('username', null)!!} <br> {!!Form::label('password','Password:')!!} {!!Form::text('password', null)!!} {!!Form::submit('submit')!!} {!! Form::close() !!} @stop 

** show.blade.php **

 @extends('master') @section('show') <?php if( $result->username == $result2 ) { echo "success"; } ?> @stop 
+5
source share
1 answer

Have you added error capture logic to app / Exceptions / Handler.php?

Check out http://laravel.com/docs/5.0/eloquent ; in particular, where indicated:

Getting the model with the first key or throwing exceptions

Sometimes you may need an exception if the model is not found. For this you can use the firstOrFail method:

 $model = User::findOrFail(1); $model = User::where('votes', '>', 100)->firstOrFail(); 

Doing this will allow you to catch an exception so that you can register and display the error page as needed. To catch a ModelNotFoundException, add some logic to your application / Exceptions / Handler.php file .

 use Illuminate\Database\Eloquent\ModelNotFoundException; class Handler extends ExceptionHandler { public function render($request, Exception $e) { if ($e instanceof ModelNotFoundException) { // Custom logic for model not found... } return parent::render($request, $e); } } 
+2
source

All Articles