Laravel

Laravel 5.7 HTTP URL Redirects

Today in this article we will discuss the HTTP URL Redirection in Laravel 5.7.  Laravel HTTP Redirect is an instance of Illuminate\Http\RedirectResponse class, which contains all necessary headers that needed to redirect the user to specific URL. Laravel provides many ways to generate RedirectResponse instance. redirect helper is the simplest way to redirect user to another URL. redirect helper is a global helper method. Redirect To URL in Laravel This is the simplest way to redirect the user to a particular URL.

Note: In the above example, If user hits the /dashboard URL then we are redirecting that user to new URL and that is /home/dashboard. Redirect Back To The Previous Page in Laravel Laravel is a very powerful PHP framework …

Read More
Laravel

Email Verification – New Feature of Laravel 5.7

Today, Laravel is a very powerful framework of PHP. Because of it’s vast available & scalable libraries/packages & developer’s full control on the project, It is now a new trend in PHP Web Development. On September 4th, 2018 Laravel has released it’s new edition i.e. Laravel 5.7 with some new features. Which make development more easy, fast and flexible. Laravel 5.7 Changes Log Laravel 5.7 continues the improvements made in Laravel 5.6 by introducing Laravel Nova. Optional email verification to the authentication scaffolding. Support for guest users in authorization gates and policies. Console testing improvements. Symfony dump-server integration. localizable notifications. Variety of ther bug fixes and usability improvements. Today, In this article we will discuss one of the new features of …

Read More
How To Fix Laravel SQLSTATE[42000]: Syntax Error Or Access Violation 1071 Specified key was too long

How To Fix Laravel SQLSTATE[42000]: Syntax Error Or Access Violation 1071 Specified key was too long

Today we will discuss the most common error of Laravel 5.4+ that is “Syntax Error Or Access Violation 1071 Specified key was too long”. Before moving ahead we should know the main root cause of this error. Laravel 5.4+ has made changes to the database’s default character set. Now, the updated character set is utf8mb4, which supports the emojis. Now, with this character set, you can store emojis in database table easily. If your web application/website is running on MySQL v7 or above then you do not need to do anything. Otherwise, you may face the following error while running the database migration command. Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes …

Read More
Laravel

How to get last executed query in Laravel?

We have been much time need to get executed query log or you want to get last executed query or maybe if you want to display SQL query from Laravel query builder then you can do it that. so I have 2 examples for display executed query in Laravel 5.6. When you are working on Laravel application at that times, I think you need much time to print last run query or if you want to check direct from phpMyAdmin SQL box, so at that time you have to follow these 2 examples. So, let’s see 2 examples of getting SQL query from Laravel Query Builder. Example 1: In this example, we can get directly get current SQL query using …

Read More
Laravel

How to turn off CRSF protection for a specific routes in Laravel?

Laravel is a very powerful framework of PHP. It gives full control to developers to tackle any request. Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. Sometime in your project, you may need to disable the CRSF protection for particular routes/URLs. You can turn off CRSF protection by editing VerifyCsrfToken.php file.  In Laravel, open app/Http/Middleware/VerifyCsrfToken.php file and you can add specific URLs to $except array. The CSRF middleware is automatically disabled when running tests.

Source: https://laravel.com/docs/5.7/csrf

Read More
PHP-logo

What are traits in PHP OOPS / Laravel ?

What is Traits? Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own. In layman language,  traits are simply a group of methods that you want to include within another class. A Trait is like an abstract class that cannot be instantiated by itself. The trait is created to reduce the limitations of single inheritance in PHP by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Example of Trait:

To use Trait within the other classes use use keyword with Trait name. i.e. user AbcClass;

Now if you create new objects …

Read More
Laravel

How to get last inserted id in Laravel?

Today in this article I show you How to get last inserted id in Laravel? Sometimes in the programming field, you might need the last record inserted id and its very common need. If you are working on Laravel framework and you want to fetch last created (inserted) id, then you can get that easily. Laravel provides a method insertGetId() to get last inserted id. If you are working with DB::table() then you have to use insertGetId() to insert data into the database. This function always returns inserted record id (primary key). It is a very simple way to get last inserted id. Example with insertGetId() method:

But, if you are inserting record using Laravel Model then it is pretty simple …

Read More
Laravel

How to get last record of database table in Laravel?

Sometimes we may need getting only last/latest record of a table in our project, we can get latest/last record from a database table in several ways these are: latest() orderBy() In below example, you can see how can we get the last record of the table. Using latest() method latest() the method belongs to fieldcreated_at.By defaultlatest()takes created_at field, if you want to give id then you can give an argument as a field name.

In above example, we have a tableproducts & we want to get last/latest/newly added product. Here we used latest() with first() because latest() will fetch records according to created_at (meaning ORDER BY created_at DESC) and then first() will get only single/first record from the queried table. Using latest() …

Read More