1

I have homecontroller in which I have a function storestudent it should able to handle exception, how to do it?

 public function storestudent(Request $request)
    {
      try
      {


        $student= new student();
        $student->sregno= $request['regno'];

            $student->save();
            return redirect('/home');

          } catch (Exception $e){

                throw new Execution;

          }
     } 

try-catch exception is not working , help me to fix this please. Thank you.

this is Execution.php

<?php

namespace App\Exceptions;

use Exception;

class Execution extends Exception
{

//
}

in handler.php

 public function report(Exception $exception)
{
    if($exception instanceof Execution)
    {
        echo "invalid register no";
    }
    parent::report($exception);
}

it gives me this error

This page isn’t working 127.0.0.1 is currently unable to handle this request.
HTTP ERROR 500

anyone help me to display my message and it should redirect to same page.

1 Answer 1

3

Here with try and catch you can use Log to keep errors in records and you can check records in log section in application:

public function storestudent(Request $request)
{
  try
  {


    $student= new student();
    $student->sregno= $request['regno'];

        $student->save();
        return redirect('/home');

      } catch(Exception e){
        $error = sprintf('[%s],[%d] ERROR:[%s]', __METHOD__, __LINE__, json_encode($e->getMessage(), true);
        \Log::error($error);
      }

 }

For more info for Log fascade check this link: Logging.

And another way is to handle exception custom exception handling which you can get here.Error handling

To show error messages on redirect simply use flash messages in laravel without custom exceptions need. following code:

public function storestudent(Request $request)
{
 try
 {


$student= new student();
$student->sregno= $request['regno'];

    $student->save();
    \Session::flash('message','Saved successfully');
    return redirect('/home');// if you want to go to home

  } catch(Exception e){
    \Session::flash('error', 'Unable to process request.Error:'.json_encode($e->getMessage(), true));
  }

   return redirect()->back(); //If you want to go back

 }

Flash messages:

@if(Session::has('message'))
 <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ 
     Session::get('message') }}</p>
@endif

or for more common flash view:

 <div class="flash-message">
    @foreach (['danger', 'warning', 'success', 'info','error'] as $msg)
       @if(Session::has($msg))
        <p class="alert alert-{{ $msg }}">{{ Session::get($msg) }} 
        </p>
       @endif
   @endforeach
 </div>

Controller code:

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Candidate;
  use App\student;
  use Exception;

 class Homecontroller extends Controller
 {


//function for login page

    public function insertReg()
    {

        return view('login');

    }

     public function storestudent(Request $request)
    {
     try {
          if ( !student::where('sregno',$request->regno )->exists() ) {
              $student= new student();
              $student->sregno= $request->regno;


            if ($student->save()) {
             \Session::flash('message','Saved successfully');
             return redirect('/home');// if you want to go to home
          } else {
             throw new Exception('Unable to save record.);
          }
        } else {
             throw new Exception('Record alreasy exists with same sregno');
        }
      } catch(Exception $e){
          \Session::flash('error', 'Unable to process request.Error:'.json_encode($e->getMessage(), true));
      }

       return redirect()->back(); //If you want to go back

  }

Hope this helps.

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

2 Comments

sir how to display error for invalid entry, if the condition does not work in this. it checks only for record exists in table or not. doesn't check for some random wrong entry. please help me to fix it.
For simple validation can use Validator of laravel. link: laravel.com/docs/5.5/validation And if want to go deep then can create custom request for your parameter validation.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.