Category

Category: Laravel

How to do ajax in Laravel 5.2

  1. Create a dummy button on your view:
    1
    2
    <input type = "submit" class = "btn btn-primary m-d do-ajax" value = "AJAX" />
    {{ csrf_field() }} {{-- This will include a unique token which will we will pass along with our AJAX post --}}
  2. On your JS script, add the following:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $('body').on('click', '.do-ajax', function () {
        $.ajax({
            url: './page/do-ajax',
            headers: {'X-CSRF-TOKEN': $('input[name=_token]').val()},
            data: {
                test: 'test'
            },
            type: 'POST',
            datatype: 'JSON',
            success: function (resp) {
                alert(resp);
            }
        });
    });
  3. On your controller add this new method:
    1
    2
    3
    4
    5
    6
    7
    public function do_ajax(Request $request) {
        if( $request->ajax() ) {
            return response()->json([
                'testdata' => 'it works!'
            ]);
        }
    }
  4. On your route, add the following code:
    1
    Route::post('/page/do-ajax', 'PageController@do_ajax');
  5. Try it out, click the button. You should get the response of the ajax request: “it works!”.

Adding Helper Files in Laravel 5

  1. Create a folder in your “app” directory, name it “Helpers.
  2. Then within your Helpers folder, add your function files.
    Name your files anything you like.
    Having a folder with multiple files allows us to avoid one big file that gets
    too long and unmanageable.
  3. Open your command line then navigate to your project ROOT folder
    then create a service provider called “HelperServiceProvider.php” by
    running the php artisan command:

    1
    php artisan make:provider HelperServiceProvider
  4. Open the file: app/providers/HelperServiceProvider.php
    Update the “register” method with the following snippet:

    1
    2
    3
    4
    5
    6
    public function register()
    {
        foreach (glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
    }
  5. Lastly, register the service provider in your config/app.php in the providers array
    1
    2
    3
    'providers' => [
        'App\Providers\HelperServiceProvider',
    ]

    Now any file in your Helpers directory is loaded, and ready for use.

Moving Laravel 5.6 from local machine to live server

  1. Zip your laravel app files.
    Laravel default installation has over 6000 files, it’s recommended that you zip your files
    then unzip it in your Cpanel Web Explorer.
  2. Export your app’s database then set it up on your server.
  3. After you have uploaded your laravel app files, open the .env file found on the root
    directory of your laravel app.
    Update the database credentials and the APP_URL.
  4. Open your .htaccess file, then add the following line just bellow RewriteEngine On:
    1
    RewriteBase /
  5. That’s it!, now check your live site.
  6. If you are getting 403 Forbidden Access or 500 Internal Server message, try changing
    the permissions of your storage folder found on the root directory of your laravel app.
    Set the permission to 0777.
  7. If you are still having issues, contact your hosting.

Handling active menu item in Laravel 5

Put this in your helper file:

1
2
3
4
5
6
function set_active( $route ) {
    if( is_array( $route ) ){
        return in_array(Request::path(), $route) ? 'active' : '';
    }
    return Request::path() == $route ? 'active' : '';
}

Usage:

1
2
3
<li class = "{{ set_active('admin/users') }}">
    <a href="{{ url('/admin/users/') }}">Users</a>
</li>

Multi-level:

1
2
3
4
5
6
7
8
9
10
11
12
13
<li class="{{ set_active(['admin/items', Request::is('admin/auction/*'), 'admin/something']) }}">
    <ul>
        <li class = "{{ set_active('admin/item') }}">
            <a href="{{ url('/admin/item') }}">All</a>
        </li>
        <li class = "{{ set_active('admin/item/create') }}">
            <a href="{{ URL::to('/admin/item/create') }}">Add New</a>
        </li>
        <li class = "{{ set_active('admin/something') }}">
            <a href="{{ URL::to('/admin/something') }}">Something</a>
        </li>
    </ul>
</li>