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