Laravel 5.4 (Laravel Mix CSS/JS)

first we need to install npm & node.js

after that, go to your laravel fresh project (open terminal)

$ cd to/your/project
$ npm install

then open file and edit webpack.mix.js, on this example i want combine multiple js into one (minity also)

 mix.scripts([
    'public/js/one.js', // create this file too
    'public/js/two.js', // create this file too
    ], 'public/js/combinejs.js'); // this file is appeared after below command

then run combine & minify with below command

$ npm run production

done.

Create Function Google Shorterner

copy this function into your code

function get_googl($url){
    $apikey = 'yourApiKeyFromGoogle';
    $asset = array('longUrl' => $url);
    $object = json_encode($asset);
    $google = 'https://www.googleapis.com/urlshortener/v1/url?key='.$apikey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $google);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $object);
    $data = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($data);
    $resulturl = $response->id;
    return $resulturl;
}

then using this function

<?php

echo get_googl('http://www.example.com/long-url-blabla-john-doe');

Create Custom Helper Laravel 5.*

1. create file in app\Http directory ex: helpers.php
2. then fill this code into helpers.php file


<?php
function convertExample($param)
{
     // create your logic here
      return $value
}

3. add/modify your file composer.json in autoload section to this below code

"files": [
            "app/Http/helpers.php"
        ]

4. run composer dump-autoload

Populate form input select (Laravel)

this is for select option (dropdown)



<select name="province" id="province" class="form-control">
@foreach(config('constants.master_province') as $key => $value)
<option value="{{ $key }}" {{ (old('province', (!empty($vendor->province)) ? $vendor->province : '') == $key) ? 'selected="selected"' : '' }}>{{ $value }}</option>
@endforeach
</select>


and below for input


 
<input type="text" name="url" id="url" class="form-control" value="{{ old('url', (!empty($vendor->url)) ? $vendor->url : '') }}">

Manual pagination with Laravel 5.*

manual paginate with laravel



use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
... 

$array = [12,23,34,45];
$page = Input::get('page', 1); // Get the current page or default to 1, this is what you miss!
$perPage = 20;
$offset = ($page * $perPage) - $perPage;

return new LengthAwarePaginator(array_slice($array, $offset, $perPage, true), count($array), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]);

 

source: https://laracasts.com/discuss/channels/laravel/laravel-pagination-not-working-with-array-instead-of-collection