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 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