Tag Archives: laravel online course

Get current route name in laravel 5


Get current route name in laravel 5 : You can get the current route name in laravel 5 simply using the Route::getCurrentRoute()->getPath();. Here in this tutorial we are going to explain the methods to get the current route.


Get current route name in laravel 5

You can use the below method to get the current route in the laravel5 –

Get current route name in laravel 5:

Route::getCurrentRoute()->getPath();
or 
\Request::route()->getName();

The above example will give you the current route in laravel 5.1

Get Route Action Name

You can use the below syntax to get the current route name –

How to get current route name in laravel 5:

Route::getCurrentRoute()->getActionName();

The above example will give you the current route action name in laravel 5.1

Laravel Redirect to Url


Laravel Redirect to Url : Laravel Redirects are basically instance of the Illuminate\Http\RedirectResponse. This contains the headers which are responsible to redirect the user from on url to another url. There are many ways to redirect the user from one url to another url. The simple method is use helper method of global helper helper. In this tutorial we are going to explain how to use redirect method in laravel to redirect the users from one url to another url.


Laravel Redirect to Url

Here are the redirection methods which are used in Laravel-

Simple Redirect-

Laravel Redirect to Url:

Route::get('users', function () {
    return redirect('home/users');
});

Which Will redirect user to home/users location.

More About Laravel Redirect to Url

Let us have more example on laravel redirect-

Laravel Redirect Back-

Laravel Redirect Back to url-

Route::get('users/login', function () {
   return back()->withInput();
});

Which Will redirect user to back to the previous location.

Laravel Redirect to Named Routes-

Laravel Redirect to Named Routes-

return redirect()->route('users');

Which Will redirect user to the route users.

Laravel Redirect to Named Routes With Parameters-

Laravel Redirect to Named Routes With Parameters-

return redirect()->route('users',['id' => 10]);

Which Will redirect user to the route users with parameter id and it’s value 10.

Laravel Redirect to Controller Action-

You can redirect to controller Action in laravel as below-

Laravel Redirect to Controller Action-

return redirect()->action('UsersController@index');

Which Will redirect user to the route index Action of UsersController Controller.

Laravel Redirect to Controller Action With Parameters-

You can redirect to controller Action with parameters in laravel as below-

Laravel Redirect to Controller Action With Parameters-

return redirect()->action('UsersController@index',['id' => 10]);

Which Will redirect user to the route index Action with parameter id and it’s value 10 of UsersController Controller.

Laravel Redirect with Flashed Session Data-

You can redirect flashed session data in laravel as below-

Laravel Redirect with Flashed Session Data-

Route::post('user/profile', function () {
    // Update the user's profile information...
    return redirect('home')->with('messgae', 'User's Profile updated Successfully!');
});

Which Will redirect user to the home with flashed session message ‘User’s Profile updated Successfully!’.

Note :Flashed Messages are shown only once and cleared after that.

Laravel delete multiple rows


Laravel delete multiple rows : You can delete multiple rows in laravel by passing array of ids which you want to delete. You can pass as much as ids you want to delete from the table. Here we are going to explain this with example.


Laravel delete multiple rows

Deleting multiple records by passing array of ids as below-

Mass Delete in Laravel Example 1

Laravel delete multiple rows

$ids = array(10, 20, 30);
DB::table('table_name')->whereIn('id', $ids)->delete(); 

Mass Delete in Laravel Example 2

Using model you can also perform mass deletion as below –

Laravel delete multiple rows

$deletedRows = Users::where('id', '>', 500)->delete();

Laravel Load multiple views in same controller


Laravel Load multiple views in same controller : You can load multiple views in single controller function using the nest method. Here is simple example of loading multiple views from single controller method.


Laravel Load multiple views in same controller

You can load multiple views in laravel controller as below –

Laravel Load multiple views in same controller:

return View::make('header_template', $data)
->nest('left_content_view', $data2)
->nest('main_content_view', $data3)
->nest('right_content_view', $data4)
->nest('footer_template', $data5);

Thus you can load multiple views using the nest.

Laravel get current url


Laravel get current url : If you are working with laravel you often need to get current url and its parameters. You can use helper function URL::current(); to get the current url in laravel4, laravel5. Here we are going to demonstrate the method to get current url and how to get its parameter.


Laravel get current url Syntax

Here is simple syntax to get current url in laravel.

Laravel get current url Syntax

Route::get('/current/url', function()
{
return URL::current();
});

Laravel get url parameters

Here is simple syntax to get url parameters in laravel. Suppose you have following url www.xyz.com/home/user?page=2. Suppose you need to get the page value that is 2.

Laravel get current url Syntax

$segment = Request::segment(3);

Which will give the page string’s value from url.

Send Sms in laravel PHP


Send Sms in laravel PHP : We often need to send Sms for implementing some specific tasks such as sending verification code on user’s phone for confirmation. For implementing sms we need third party service which can provide us services to send sms using their api’s . For now we are going to demonstrate this with the well known twilio sms services – https://www.twilio.com/try-twilio


How to Send Sms in laravel PHP

Here are the steps to send sms in laravel php-

Step 1 :

Create Account on twilio – https://www.twilio.com/try-twilio

Step 2 :

Open composer.json and add the following lines –

"aloha/twilio": "~1.0"

Step 3 :

Now Run The two Commands Below –

  • composer update
  • php artisan config:publish aloha/twilio

Step 4 :

Now open – /app/config/packages/aloha/twilio/twilio.php and the required configuration. You can find the settings on https://www.twilio.com/user/account/voice-messaging

Step 5 :

Now you are ready to send the message as in below example –

$phone = 9112312312**;
Twilio::message($phone, "Hi Thanks For Registration! ");

Laravel Cache


Laravel Cache : Cache is one of the important part of any framework. Cache improves the system’s performance. Laravel has powerful cache mechanism which makes it perfect. Laravel provides a unified API for various caching systems. Laravel cache config file is located at config/cache.php where you can add configurations.
Laravel supports following types of caching-

  • Memcached
  • Redis

Laravel Cache Syntax And Example-

Let us go step by step –

Settings –

Databse :

Add a database setup to create table which will store information for database cache-

Laravel Create Database Cache Table –

Schema::create('cache', function($table) {
    $table->string('key')->unique();
    $table->text('value');
    $table->integer('expiration');
});

Memcached –

Add settings for memcached. Go to config/cache.php and add the memcached settings-

Laravel Memcached –

 'memcached' => [
            'driver'  => 'memcached',
            'servers' => [
                [
                    'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
                ],
            ],
        ],

You can add your own host name.

Note : Memcached needs http://pecl.php.net/package/memcached to be installed.

Redis

Laravel Cache Redis Settings –

'redis' => [

    'cluster' => false,

    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],

],
Note : Make sure predis/predis package (~1.0) is installed.

Laravel Cache Example

Let us understand the Laravel Cache functionality with simple Example –

Laravel Cache Syntax

Cache::put(); Accepts three parameters key, value and time in minutes you want to store the cache.

Laravel Cache Syntax –

Cache::put('key', 'value', $minutes);

Here is a simple example –

Laravel Create Cache Example Syntax

Cache::put(); Accepts three parameters key, value and time in minutes you want to store the cache.

Laravel Cache Syntax –

Route::get('/', function()
{
Cache::put('mykey', 'Welcome to Home Page !', 1);
});

Now if you hit the rout url /URL cache will be created but you will see nothing on browser. To check the cache created go to
app/storage/cache folder and check the file created after opening the file you will see the cache data along with content “Welcome to Home Page !”

Laravel Get Cache Example Syntax

Cache::get(); Accepts one parameter key to get the created cache.

Laravel Get Cache Syntax –

Route::get('/', function()
{
Cache::get('mykey');
});

Now if you hit the index url /URL it will return the cache data from cache file create above.

Laravel Create Cache Forever Example Syntax

Cache::forever(); If you want to keep the cache file forever means it will not expire until you delete it.

Laravel Forever Cache Syntax –

Route::get('/', function()
{
Cache::forever('mykey', 'Welcome to Home Page !');
});

Laravel Forget Cache/Delete Cache Example Syntax

Cache::forget(); is used to delete the cache created using the key. It accepts key as input parameter to remove the cache for that key.

Laravel Forget Cache Syntax –

Route::get('/', function()
{
Cache::forget('mykey');
});

Laravel 5 Print Last Query


Laravel 5 Print Last Query : As developer for debugging the queries we often need to print the queries executed in our program which helps us to work more efficiently. In laravel you need to enable query log DB::enableQueryLog();before the query and then you can print queries as $queries = DB::getQueryLog();


Laravel 5 Print Last Query Example And Syntax

Here is simple example of printing queries in laravel –

Laravel 5 Print Last Query :


DB::enableQueryLog();// enable query first

    // Your queries goes here
    // ...

    // Now print Queries executed....
    $queries = DB::getQueryLog();
    foreach($queries as $k=>$query)
    {
        Log::debug(" $k Query - " . json_encode($query));
    }

Which will print all queries executed.

Note : DB::enableQueryLog(); should be enabled before you log.

Laravel pagination


Laravel pagination : Pagination is used to display the dynamic data using links for dynamic results. In other framework you need to work more to implement pagination functionality. Laravel provides rich pagination functionality which you can use to generate pagination links quickly. We are going to explain the simple method to create pagination in laravel using the paginate method.


Laravel pagination Syntax And Example –

Lets create a simple pagination in laravel –

Here is we are going to add pagination in UsersController and added pagination on user’s view-

Laravel pagination Syntax And Example

class UsersController extends BaseController {

    public function userList()
    {
        $this->layout->title = "App Users ";
        $num = 20; // no of results you want to show on each page.
        $usersList = Users::orderby('created_at', 'desc')->paginate($num);
        $this->layout->content = View::make('content.userscontent')->with('usersList', $usersList);
    }

}

Where $num is no of results you want to show on each page.

Now lets display the pagination links on view

Laravel pagination Show Links On Views-

links(); ?>

Here we have displayed the links for pagination.