phplaravelformscontrollersubmit

Laravel check form submit value


Hi I was trying to use form to submit a value to search in the database,however I could not make it work. Can anyone help with this?

<form action="/comp1230/assignments/assignment2/public/search" method="post">
<h2>Please enter the keywords you want to search:</h2>
<input id="search"type=text name="searchcontent">
<input type="submit" value='Search'>
</form>

Above is the welcome.blade.php form, I want to have this submited value to be used in my controller,code is shown below:

    public function search()
{

     $search = Input::get('searchcontent');
     $results=Records::paginate(5);
     $records=[];
     foreach($results as $result)
     if(!in_array($search,$result)){
        continue;
     }else{
        array_push($records,$result);
     }

    return view('home',['records'=>$records]);
}  

route:

Route::post('/search', 'RecordController@search');

but what i got is

419
Sorry, your session has expired. Please refresh and try again.

GO HOME

Please help, thanks a lot!


Solution

  • Laravel pagination only works with get parameters.

    and after pagination, array conversion takes miscellaneous concepts

    the following example illustrates this concept

              1. Get data from database
              2. Filter data corresponding search_content using in_array (in_array accepts data array only and not for objects)
              3. Perform manual pagination and display records
    

    Code for RecordsController.php

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Input;
    use Illuminate\Pagination\LengthAwarePaginator as Paginator;
    
    use App\Records;
    
    class RecordController extends Controller
    {
        // Show Entry Form
        public function index()
        {
            return view('search_entry');
    
        }
    
        public function search(Request $request)
        {
    
             $search = Input::get('searchcontent');
    
             $results=Records::get()->toArray();
             $records=[];
    
             foreach($results as $result)       //in_array accepts array only
             {
    
                if(in_array($search, $result))
                {
                    array_push($records, $result);
                }
    
             }
    
            $page = $request->page; // current page for pagination
    
            // manually slice array of product to display on page
            $perPage = 5;
            $offset = ($page-1) * $perPage;
            $new_record = array_slice($records, $offset, $perPage);
    
            // your pagination 
            $new_record = new Paginator($new_record, count($records), $perPage, $page, ['path'  => $request->url(),'query' => $request->query()]);
    
            return view('home',['records' => $new_record]);
        }  
    }
    

    Code for search_entry.blade.php

    <!doctype html>
    <html lang="{{ app()->getLocale() }}">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>LaravelTest</title>
        </head>
        <body>
            <form action="{{url('search')}}" >
                <h2>Please enter the keywords you want to search:</h2>
                <input id="search"type=text name="searchcontent">
                <input type="submit" value='Search'>
            </form>
    
    
        </body>
    </html>
    

    code for home.blade.php (resultant view php)

    <!doctype html>
    <html lang="{{ app()->getLocale() }}">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>LaravelTest</title>
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
        </head>
        <body>
            <h3>SEARCH CONTENT</h3>
            <table border=1 cellspacing=0 cellpadding=2>
                <thead>
                    <tr>
                        <td>ID</td> <td>NAME</td> <td>AGE</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        foreach($records as $record)
                        {
                            echo "<tr><td>".$record['id']."</td><td>".$record['name']."</td><td>".$record['age']."</td></tr>";
                        }
                    ?>
                </tbody>
                </table>
                {{ $records->links('pagination::bootstrap-4') }}
    
    
        </body>
    </html>
    

    Add the following Routing code in Web.php

    Route::get('search_entry','RecordController@index');
    Route::get('search','RecordController@search');