I am new to PHP and Laravel. I am making a Laravel login app which prints "success" after comparing the entered value and database value.
My problem is: when both value are matched it prints "success" but when they are not matched it shows ModelNotFoundException .
I have created an exception for this but again same error occurs.
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 get value from index.blade.php and from database named registers which 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
Have you added the error trapping logic to your app/Exceptions/Handler.php file?
Check out http://laravel.com/docs/5.0/eloquent ; specifically where it states:
Retrieving A Model By Primary Key Or Throw An Exception
Sometimes you may wish to throw an exception if a model is not found. To do this, you may use the firstOrFail method:
$model = User::findOrFail(1);
$model = User::where('votes', '>', 100)->firstOrFail();
Doing this will let you catch the exception so you can log and display an error page as necessary. To catch the ModelNotFoundException, add some logic to your app/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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With