After refresh go back

Published 3 weeks ago by evripidesk

I have the below code that is search for security code (that is in my db) and I use form to search it, is working fine but if I hit the refresh button (browser) then I get back with the error , No record found how can I stop it and with the refresh get the normal details

<?php 
 public function searchSecurityCode($id)
     {
       $id=Hashids::decode($id);
       $id = implode(',', $id);

       $security_code = Session::get('security_code');

       $this->validate(request(), [
           'q' => 'required'],
           [
               'q.required'    => 'Please type the security code ',

           ]);
            $q = Input::get ( 'q' );

            $success = Clients::where(function($query) use ($id,$q) {
                                          $query->where('id', 'LIKE', $id)
                                                  ->where('security_code', 'LIKE', $q);

                                      })->get();


            if(count($success) > 0) {
              Session::forget('security_code');
              Session::forget('name');
              $products = Products::with('user’,’clients’)->where(‘client_id','LIKE',$id)->paginate(10);
              $resetsecuritycode = Clients::find($id);
              $resetsecuritycode->security_code = str_random(10);
              $resetsecuritycode->save();

              return view(‘clients.history')->withClients($products);

             }else
            return redirect()->back()->with('status', 'No record founded!');


     }

My route is below

Route::post('/clients/{id}/history/', '[email protected]');

Sergiu17

Hi, do you have a get route like this?

Route::get('/clients/{id}/history/', '');
evripidesk

I dont have any get for this, do I need this if yes which method can I use for get (eg)

robrogers3

the mistake is you don't return a redirect. you directly return a view.

you should do (or something like this).

return redirect('/foo')-> withClients($products);

then in the foo route controller you can access the products via the session.

problem: if the $products data is large, the session is not the best way. let me know if you need help in this case

evripidesk

thank you @robrogers3 for your reply I try it but I get blank page updated code

<?php 
 public function searchSecurityCode($id)
     {
       $id=Hashids::decode($id);
       $id = implode(',', $id);

       $security_code = Session::get('security_code');

       $this->validate(request(), [
           'q' => 'required'],
           [
               'q.required'    => 'Please type the security code ',

           ]);
            $q = Input::get ( 'q' );

            $success = Clients::where(function($query) use ($id,$q) {
                                          $query->where('id', 'LIKE', $id)
                                                  ->where('security_code', 'LIKE', $q);

                                      })->get();


            if(count($success) > 0) {
              Session::forget('security_code');
              Session::forget('name');
              $products = Products::with('user’,’clients’)->where(‘client_id','LIKE',$id)->paginate(10);
              $resetsecuritycode = Clients::find($id);
              $resetsecuritycode->security_code = str_random(10);
              $resetsecuritycode->save();
              Session::put('products',$products);


             }else
            return redirect()->back()->with('status', 'No record founded!');


     }

     public function viewhistory($id)
     {
        $products = Session::get('products');
        return redirect('products/'.$id.'/history')-> withProducts($products);
     }

Routes Route::post('/clients/{id}/history/', '[email protected]'); Route::get('/clients/{id}/history/view', '[email protected]');

RamjithAp

Follow this. Your Route

Route::post('/clients/{id}/history/', '[email protected]'); Route::get('/clients/{id}/history/view', '[email protected]');

Controller

<?php 
 public function searchSecurityCode($id)
     {
       $id=Hashids::decode($id);
       $id = implode(',', $id);

       $security_code = Session::get('security_code');

       $this->validate(request(), [
           'q' => 'required'],
           [
               'q.required'    => 'Please type the security code ',

           ]);
            $q = Input::get ( 'q' );

            $success = Clients::where(function($query) use ($id,$q) {
                                          $query->where('id', 'LIKE', $id)
                                                  ->where('security_code', 'LIKE', $q);

                                      })->get();


            if(count($success) > 0) {
              Session::forget('security_code');
              Session::forget('name');
              $products = Products::with('user’,’clients’)->where(‘client_id','LIKE',$id)->paginate(10);
              $resetsecuritycode = Clients::find($id);
              $resetsecuritycode->security_code = str_random(10);
              $resetsecuritycode->save();
              Session::put('products',$products);

              return redirect('products/'.$id.'/history/view');

             } else {

              return redirect()->back()->with('status', 'No record founded!');

             }

     }

     public function viewhistory($id)
     {
        $products = Session::get('products');
        return view('products.view', compact($products));
     }

Your view file resources/views/products.view.blade.php

@foreach($products as $product)
<h1>{{ $product->name }}
@endforeach
evripidesk

@RamjithAp Perfect I correct the line from

return redirect('products/'.$id.'/history');

to

return redirect('products/'.$id.'/history/view');

and is working perfect

thank you man

RamjithAp

Welcome!

Please sign in or create an account to participate in this conversation.