How to log the exception in Laravel | Exception handling in Laravel

This tutorial is to learn how to handle and log the exceptions in Laravel. We will simply use try & catch to handle the exceptions in Laravel.

This is always good practice to handle the exceptions in any application to track the issue.

Laravel provides the following library to log the exceptions.

Now I'll demo the code to catch the exception and log it.

Here I have defined UserServiceProvider class and inside this class deleteUser() is a function to delete the user.

So here in catch part we are logging the exception. ...  Read More

Share This:

Left join with last record of right table in Laravel & Mysql

Here we have 2 tables 'users' and 'answers' where users is left table and answers is right table which has user answers.

We wanted to left join users with answers but the join should be with the latest record or answers table.


Share This:

How to pass data to all views in Laravel 5?

Define a function which will return the data which you wanted to use on every view page.

Like I defined my function in ProductServiceProvider.php

Now call getTrendingProducts() function in AppServiceProvider.php which is under App/Providers direcory.

Now you will be able to use $trandingProducts variable on every view of application.

Share This:

How to change message languages dynamically in Laravel | Change localisation in Laravel

Create folders inside resources/lang for different different languages and write message files with same name and also write same key among all the message files.

like I have 2 languages EN and ES.. then I created 2 folders, one is en and second is es.

Inside both folders I created password files. like:



Now I can call any of these 2 language messages by their file and key name like:

here I can pass the language code dynamically in the constructor and message of that language will be called. ...  Read More

Share This:

Join or Left Join where no foreign key - using where condition in Laravel


Share This:

Join or Left Join only with where condition in laravel


Share This:

if else and case when in Laravel query

Often you need to do some MySQL query operation conditionally. You may need get any column value conditionally. How you will do that?

Here is the example. You can either IF ELSE or CASE WHEN for conditional queries.

Here in this example of MySQL and Laravel, if start_at column value in products table is NULL then we retuning 'started_at' column value else we are returning minimum value of 'start_at' column.

If and Else:

Products::SELECT(DB::raw('IF(MIN(products.start_at) IS NULL, products.started_at, MIN(products.start_at)) as start_at'))->get();

Case When:

Products::SELECT(DB::raw('CASE WHEN COUNT(products.user_id) != 0 THEN COUNT(products.user_id) ELSE 1 END as user_id'))->get();

Share This:

Email invoice in PDF | Generate PDF in Laravel | MPDF

So many time we need to send our invoices or reports in pdf. Here I'm going to explain how to generate the pdf in Laravel. You will also learn that how to send pdf in email attachment in Laravel. Here we will be creating a dynamic view page or HTML page and then will convert that html page to pdf.

Because we are creating the pdf file in Laravel so we need to install Laravel first and here is the link to let you know how to install the Laravel.

Install Laravel:
Install laravel

I'll be generating the PDF using MPDF library. So first of all we need to install mpdf using below command. I have use 6.1 version you can change it according to your need. ...  Read More

Share This:

Stripe | Register user on stripe in Laravel PHP | Create subscription plan | Plan subscription | Charge payment | Stripe payments in Laravel and PHP

Stripe is the beautiful payment gateway which allow you to accept payments from your customers easily and securely. Stripe also allows to transfer payments from bank to bank. We can create recurring payments through the Stripe. We can create subscriptions for the users through the Stripe API. You can do adaptive payments through the Stripe. For example If customer pat $100 than you can split this payment to multiple users like you can give 30% to cab driver and rest for the Admin.

In this article I'm going to explain followings: ...  Read More

Share This: