Skip to content Skip to sidebar Skip to footer

Re-write Pure Php Code In Laravel-5.1 Format

any ideas on how to convert this Laravel-5.1 code. I have this code running in pure PHP, I'll like to write it in Laravel since I'm using Laravel for the development. Pointing me t

Solution 1:

Well since you are asking for the Laravel way to do things there will be few steps to get this done. In short, you need to 1) create a model, 2) update your routes.php file, 3) create a controller, and (4) update your ajax call to reflect Laravel's routing conventions. I suggest using the command line php artisan commands to create the Model and Controller as they will place the necessary files in the correct paths so that Laravel will autoload them for you.

  1. Model - run php artisan make:model GeoName from the command line, this should create a model at app/GeoName.php in which you will need to change the tablename to reflect your custome name.

    <?namespaceApp;
    
    useIlluminate\Database\Eloquent\Model;
    
    classGeoNameextendsModel{
    
    // this will map your custom table name to laravel. protected$table = "geonames_names"; 
    
    }
    

    Laravel will automatically expect the tablename to be a plural version of the model in this case it would look for geonames, to override that you'll need add the protected $table attribute above.

  2. Update the app/Http/routes.php file to catch the AJAX post request.

    Route::post('bootstrap/source','GeoNameController@ajaxQuery');

    This will catch a POST request to http://localhost:2222/bootstrap/cache, there's more in the Laravel Documents on routes here.

  3. Create a controller using php artisan make:Controller GeoNameController --plain in the command line. Plain was used here to stop the automatic scaffolding of the typical CRUD request types of index, create, edit, show, update and delete. This will create the file app/Http/Controllers/GeoNameController.php

    <?phpnamespaceApp\Http\Controllers;
    
    useIlluminate\Http\Request;
    
    useApp\Http\Requests;
    useApp\Http\Controllers\Controller;
    
    classGeoNameControlerextendsController{
         // Add the following herepublicfunctionajaxQuery(Request $request){
    
             $query = $request->get('query');
    
             $geoNames = GeoName::where('name',$query)->lists('name','timezone_id')->get();
    
             return$geoNames;
         }
    }
    

    Keep in mind that query is used in $query = $request->get('query'); because that is what you named the data variable in your ajax request. ( data: 'query=' +query,)

  4. Finally, in your jQuery ajax request remove the .php in the request call. url: 'http://localhost:2222/bootstrap/source' as you'll never directly call files in Laravel, the routes files handles all the destinations for your application.

A few things of note, (1) Your database should be configured using the .envand app/config.php files, (2) Laravel will automatically detect that the jQuery ajax function is expecting a JSON response so Laravel will provide exactly what it asks for.

You will may likely run into an issue with XSFR token permission issue which you can read about how to resolve in the Laravel Docs here. If you don't already know, the Laravel's Master Documentation is excellent!

Of course there is a lot more to learn about using Laravel and many excellent Laravel resources out there. Good luck!

Post a Comment for "Re-write Pure Php Code In Laravel-5.1 Format"