Categories
Laravel

How to get last inserted id in Laravel

In this post, I would like to show the different ways to get last inserted ID from the database table when working with Laravel PHP Framework. In the core PHP, we will get last inserted ID with mysqli_insert_id() or PDO::lastInsertId() methods, but in Laravel framework you don’t need have to use those methods. In the […]

Categories
Laravel

Laravel 5.5 – How to log all Eloquent Queries

In this post, I will show you simple method to log each and every Eloquent Query of your application executes. We gonna log queries to the storage/logs/laravel.log file. To log database queries we gonna add a database calls lister as shown in the below example. Open your app/Providers/AppServiceProvider.php and add following code to boot() method: […]

Categories
Laravel

Laravel 5.5 – Custom Blade::if() Directives

In Laravel 5.5, writing custom blade if Directives is pretty easy. Using this new feature, you can write simplified and clean if statements in your views. Imagine, you have to check, if the user is admin or not, in this situation you general check for two conditions first you will check whether the user is […]

Categories
Laravel

Laravel 5.5 – New Blade Directives , @auth and @guest

As part of Laravel 5.5 version release, Laravel introduced new blade directives called @auth and @guest.As the name implies both can be useful to determine if the current user is authenticated or guest. In the previous versions of Laravel to check user authentication status we’d used @if directive with the combination of Auth()->check(). But the […]

Categories
Laravel

Laravel 5.5 – DD and Dump in Collections

Laravel offers two new useful debugging methods dump() and dd() as part of Laravel 5.5 version release. These methods are added to Collections, it means you can call dump() and dd() on collection instances. In previous Laravel versions, when debugging collections we’d assign a variable to a collection then keep dumping the variable as we […]

Categories
Laravel

Laravel 5.5 – Optional Objects

Laravel 5.5 includes a new Optional class with a new helper() function, which you can think of as a generic null object implementation. The point of the optional() class and helper is to prevent situations like where you need to fetch a property from a relationship and if its empty this optional helper gracefully handles […]

Categories
Laravel

Laravel 5.5 – Dynamic templates with View::first

Laravel 5.5 is introduced a new feature called dynamic view loading and it makes your controllers simple and more expressive when dealing with dynamic templates. Using This feature we can avoid plain old conditional statements to load the appropriate template for the request. For example, let’s assume you have different pages and some page have […]

Categories
Laravel

Laravel – Generate PDF file from view

We gonna use barryvdh/laravel-dompdf Laravel package to generate PDF files from view file. This package is just a wrapper around DOMPDF library. Installtion Use below composer command to install the package After installing laravel-dompdf, add the ServiceProvider class to the provider’s array in config/app.php You can optionally use the facade for shorter code. So when […]

Categories
Laravel

Laravel 5.5 – The Route::redirect method

Laravel 5.5 introduced new Router class method called redirect, which gives the flexibility to redirect to other URLs directly without creating a controller or closure. How to use Route::redirect() Let’s define a route with Route::redirect() method, if user tries to access http://localhost:8000/contact page, redirect the user to http://locahost:8000/contact-us page. The Route::redirect() is very useful method […]

Categories
Laravel

Laravel 5.5 – The Route::view method

Laravel 5.5 introduced new Router class method called view, which gives the flexibility to render the view file directly without creating a controller or closure. This is not a great big feature, but it simplifies your code and removes a couple of files. How to use Route::view() Let’s define a route with Route::view, to access […]

Categories
Laravel

How to Create APIs in Laravel 5.5 using API resources

In this post, I will show how easy it is to use API resources to build REST APIs. In the previous Laravel REST API tutorial, I have used Fractal 3rd party library to control API response data. In Laravel 5.5, we now have API resources and these resources are literally developed based on Fractal.Now we […]

Categories
Laravel

Laravel 5.5 New feature- Fresh Migrations

In Laravel 5.5 a new Artisan command added to migrate: namespace. This is similar to the existing migrate:refresh option, however, rather than rolling back your existing migrations, migrate:fresh drops all the table and migrates them from scratch. The only difference between a refresh command and fresh command is, the fresh command simply drops all the […]

Categories
Laravel

Create REST API with the Laravel 5.5

This tutorial provides an example of building a complete RESTful API using Laravel 5.5.* Framework. You gonna use the different HTTP methods during the REST API development, quick introduction about each method. GET to retrieve data POST to add data PUT to update data DELETE to delete data Each method as a purpose isn’t it?. […]