Category Archives: Blog

Blog

Laravel whereIn, whereNotIn With SubQuery Example

In this Laravel whereIn and whereNotIn with subquery example article, I’ll show you how to use whereIn and whereNotIn subquery in laravel queries.

Laravel SubQuery

In laravel, sometimes you may want fetch records from multiple database tables having complex and nested relationships. You may also require to exclude some nested data from DB table. So this can be achieved using whereIn and WhereNotIn clause in subquery.

Laravel WhereIn SubQuery

In the given example query we will fetch data from user table, which is available in the user_role table using whereIn subquery.

WhereIn SubQuery Using Query Builder

WhereIn SubQuery Using Model

The above laravel statement given whereIn subQueries represents following SQL query:

Laravel WhereNotIn SubQuery

In the given example query fetch data from user table, which is not available in the user_role table by using whereNotIn subquery.

whereNotIn SubQuery Using Query Builder

WhereNotIn SubQuery Using Model

The above laravel statement given whereNotIn subQueries represents following SQL query:

Laravel WhereHas Eloquent Example

In this Laravel WhereHas Eloquent Example tutorial, we will learn about laravel eloquent whereHas() method with the example. In this tutorial we will also learn about laravel has() and with() eloquent methods.

Laravel Eloquent With Method

The Laravel with() method represent eager loading. The laravel with method preload the relationship(s) you specify along the main model. This is helpful when you have a collection of models and you want to load a relation for all of them. With eager loading you run only one additional DB query instead of one for every db model.

Example:-

User > hasMany > Post

Laravel Eloquent has Meth

od

The eloquent has() method in laravel is used to filter the selecting model based on the selected relationship provided. It works like  where method in laravel.

Example:-

User > hasMany > Post

Laravel Eloquent WhereHas Method

The eloquent whereHas() method in laravel works like the has() method but it just allows you to specify some additional filters for related laravel model to check in laravel.

Example:-

User > hasMany > Post

where Between Laravel Eloquent Query

In this laravel whereBetween eloquent tutorial, I’ll show you how to fetch data between provided range from table in laravel.

whereBetween Laravel Eloquent Query

The laravel whereBetween clause is used to query data between provided range for specific column criteria. Suppose you want to know How many users have registered between two dates range. You can query this data using whereBetween eloquent methods.

whereBetween query With Ids

This laravel eloquent query gets the users between provided id 1 to 50 from users table.

Laravel Eloquent wherebetween Dates

Suppose you want to fetch all records from database between two dates, you can pass the start date and end date in whereBetween clause along with the column name.

The above laravel whereBetween query fetches the rows between start date to end date provided.

Laravel whereNotBetween Query Example

In this laravel where not between eloquent query example tutorial, I’ll show you how to fetch data from database table skipping data in the provided between range using laravel whereNotBetween eloquent query.

Laravel whereNotBetween eloquent query

The laravel whereNotBetween eloquent queryis used to fetch data from database table skipping data in the provided between range. Suppose, you want to fetch some data from database in laravel and you may want to skip some records between two column values. This can be achieved using laravel eloquent whereNotBetween cluase.

Laravel whereNotBetween query With Ids

The above laravel eloquent query fetches all records from the users table skipping records between id 1 to 50 from users table.

Laravel Eloquent whereNotBetween Dates

The above laravel eloquent query get all records from database table. But it skips records between given start date to end date.

Laravel Multiple Where Conditions Example

In this Laravel Multiple Where Conditions Example tutorial, I’ll show you how to use multiple where clause in laravel eloquent query.

Laravel Multiple Where Conditions Example

In laravel, when you fetch data from database tables you come to situation when you need to pass multiple conditions in laravel query. In laravel eloquent queries this can be done by providing multiple where clause in laravel eloquent query. In this tutorial, you will learn how to create and use multiple where clause query using laravel eloquent. Laravel eloquent model allows you to provide multiple where conditions to fetch required data. In this tutorial you will learn to use multiple where conditions with eloquent queries.

In this article we will share various examples of how to add multiple where clause with laravel search query with multiple conditions, update query with multiple conditions, delete query with multiple conditions, and relationship with multiple conditions in laravel.

Laravel multiple where clause

In this example we will be using Multiple where conditions in laravel query:

Laravel multiple where with Relationship

Open your model file and create scopes as following:

Then call scops as following:

Laravel orWhere Condition with Eloquent Query Example

In this Laravel orWhere conditions with eloquent query example tutorial, I’ll show you how to use laravel orWhere method in laravel query builder and model. In this article you will also learn to use laravel orWhere with multiple conditions. In this tutorial I’ll share different use case of laravel orWhere eloquent method. In this Laravel orWhere conditions with eloquent query tutorial you will learn about the syntax and use of Laravel orWhere method.

Laravel orWhere clause syntax

Here is the general syntax of the laravel orWhere method:

Laravel orWhere Query

In this example query we will learn to use orWhere clause with single and multiple where condition.

Laravel orWhere with Query Builder

In this example you will learn to use orWhere clause with laravel query builder. You can use laravel orWhere with query builder as following:

Example:-

Laravel orWhere with Eloquent Model

In this example you will learn to use orWhere clause with laravel eloquent model. You can use laravel orWhere with eloquent model as following:

The above given whereNull query you will get the following SQL query:

Laravel Multiple orWhere

In this example you will learn to use multiple orWhere clause. You can use multiple orWhere with eloquent model as following:

Laravel Where Null and Where Not Null Query

In this Laravel where Null and where Not Null example tutorial I’ll show you how to use whereNull() and whereNotNull() methods in laravel. In this tutorial you will learn to use whereNull() and whereNotNull() methods for creating laravel eloquent queries. In this article I’ll share different use case of the whereNull() and whereNotNull() methods in laravel. In this Laravel where Null and where Not Null tutorial you will learn about the syntax and the use of whereNull and whereNotNull eloquent methods.

Laravel whereNull method syntax

Here is the general syntax of the laravel whereNull method:

Laravel whereNotNull method syntax

Here is the general syntax of the laravel whereNotNull method:

Laravel whereNull Query

In this example query we will fetch data from database table for the  users where the name field empty. In this query we will demonstrate the use of whereNull Query. The laravel whereNull Query can be used as following:

The above given whereNull query you will get the following SQL query:

Laravel whereNotNull Query

In this example query we will fetch data from database table for the  users where the email_verified_at field is not empty in DB table:. In this query we will demonstrate the use of whereNotNull Query. The laravel whereNotNull Query can be used as following:

The above given whereNull query you will get the following SQL query:

Laravel where Like Query Example

In this Laravel where like query example tutorial I’ll show you how to use laravel whereLike() method with query builder in laravel. You also learn how to use whereLike() eloquent method in search query. I’ll also show you how to use laravel where like for multiple like condition. In this tutorial I’ll demonstrate you different use case of the laravel whereLike method with examples.

Laravel where Like Query use with Model

Laravel whereLike() method is simple implementation of MySQL Like keyword. In laravel you can use % wildcard character with whereLike condition in similar as you do in mysql like.

Example:-

The above given Where like query you will get the following SQL query:

Using macros with Like

In laravel you can define a macro using the macro static method in class you want to define the macro for. We need to define a macro for the Eloquent class, so we can extend it as following:

Laravel whereLike with multiple columns using macros

In laravel when you want to search with multiple columns then you can exten this macro to support multiple columns as following:

Laravel Eloquent whereRaw Query Example

In this laravel where raw query example tutorial I’ll show you how to write whereraw and where DB raw query in laravel. In this article you will also learn to use the laravel whereRaw eloquent method to building query in laravel. I’ll also show you how to use laravel where raw with multiple conditions in eloquent queries.

Laravel whereRaw Query using Model

In this example I’ll show you how to use laravel whereRaw query in laravel model. In laravel you can use whereRaw with model as following:

The above given whereRaw query you will get the following SQL query:

whereRaw Query using Query Builder

In this example I’ll show you how to use laravel whereRaw query using Query Builder. In laravel you can use whereRaw with Query Builder as following:

The above given whereRaw query you will get the following SQL query:

How to Use DB Raw Query in Laravel

In this Laravel db raw query example tutorial, I’ll show you how to use db raw query in laravel. In this tutorial you will also learn how to use DB::raw() with join() method in laravel eloquent model. In laravel DB::raw() eloquent method you can also add subquery in laravel queries. In laravel you can use db raw queries to insert, retrieve, update, and delete data into database table. In this article we will demonstrate you the different use case of the db raw queries.

Laravel retrieve all records using DB raw query

In laravel to retrieve all records you can use db raw query as following:

Example:-

Delete using DB raw query

To delete data from the database table you can use DB raw query as following:

Example:-

DB raw With Select Clause Query in laravel

Sometimes in laravel you come to situations where you would need raw queries in your controller or model. In various cases in laravel you may need to use a raw expression in a query. In laravel you can write raw queries using DB as following:

Example:-

Laravel DB Raw Query With Join

You can implement raw join queries using DB raw as following:

Using DB Raw Get Max Value in Laravel Query

In laravel you can get max values using db raw as following:

Laravel Eloquent Relationships Example

In this Laravel Eloquent Relationships example tutorial, I’ll show you how to create and use eloquent relationships in laravel database model. In this article we will learn to implement and use different types of eloquent model relation available in laravel. We will also learn to perform create, insert and to delete operations in different type of relationships.

Type of Laravel Eloquent Relationships

In laravel, there are following types of eloquent relationships available:

Laravel One to One Relationship Example

The one to one relationship is a very common relationship in laravel model. In this example we will learn to implement and use one to one relationship in laravel. We will also show you how to  insert, retrieve, update, and delete data with the eloquent model from the database table in laravel.

Laravel Eloquent One to One Relationship Example

Let suppose we have two tables posts and contents in our example tutorial. In laravel we can create both tables using migration as following:

Post table migration:

Contents table migration:

In this example, the Post model is closely associated with a Content model in one to one relationship. In one to one relationship we will create a post_content() method within the Post model and there is hasOne() method to relate it with Content model as following.

The Eloquent model adds a foreign key based on the model name and should have a matching id value in it. In this relationship the post_id is the foreign_key for Content model,

Inverse of One to One Relationship Example

For now we can have the access to the content from the post model. Now we create an inverse relationship in the content model. For this we will use the belongsTo method for getting the post data from the content model as following.

We can now access the post content using the relation method as following:

Laravel One to Many Relationship Example

In this step by step tutorial I’ll guide you through to create many to many relationship and learn how to use this relationship. In this example we will use one to many relationship, and perform crud (create, read, update, delete) operation with the eloquent model from the database table in laravel.

Laravel Eloquent One to Many Relationship Example

Let suppose we have  tables name posts and authors. Using laravel migration, you can create both tables with the following fields:

Author table migration:

Post table migration:

Define One To Many Relationship

Now we will define One To Many Relationship for the model we have created. In One to many relationships means a relationship where one single model can owns the number of the other model. As per the example model we created, a single author can have written many post articles. Let’s take an example of one to many relationships.

Now, we can access the collection of post articles by using the post() method as.

Defining Inverse One To Many Relationship in Laravel

As of now we can access all the post articles of an author, now suppose if we want to allow a post model to access its Author model or access author details by using the post model. The inverse relationship of both One to One and One to Many works the same way. The inverse of the one to many relationship can be defined using the belongsTo method. Thus we can define in example mode as following:

Now we can get the author details by using posts in one to many relationship as following:

Retrieving Data In One to many relationship

In one to many relationship you can retrieve data from model as following:

You can get all posts of a particular author:

Similarly, you can retrieve the inverse related model.

Laravel Many to Many Relationship Example

In laravel using belongsToMany() method, you can define many to many relationship in laravel eloquent models. Then you can insert, update and delete data from table using many to many relationship in laravel.

Laravel Eloquent Many to Many Relationship Example

In this example we will demonstrate the implementation of Many to Many Relationship using two tables posts and tags. In this relationship each of the post can have many tags and each of the tag can be associated with many posts.

Now, lets understand how to define many to many relationships, Using belongsToMany() method:

Now, you  can access the tags in the post model as follow:

The inverse of Many to Many Relationship Example

In Laravel, the inverse relationship of a many to many relationships can be defined using belongsToMany method on the reverse model.

Now you can access the post in the tag model as follow:

Laravel Eloquent HasMany Through Relationship Example

In this Laravel Has Many Through Eloquent Relationship tutorial we will implement an example for the Laravel Has Many Through Eloquent Relationship. We will generate migration files of “users”, “posts” and “countries” table and then add a foreign key with users and posts table as following:

In users migration file:

In posts migration file:

In countries migration file:

In laravel, the “has-many-through” relationship provides a shortcut for accessing distant relations via an intermediate model relation. For example, a Country model might have many Post models through an intermediate User model. In this example,  we can easily get all blog posts of a given country. I’ll show you how to define HasMany Through relationship:

To fetch data using this relationship as follow:

Laravel One to One Relationship Example

In this Laravel one to one relationship example tutorial, I’ll show you to implement one to one relationships In laravel eloquent model. You will also learn how to create and use one to many relationship in laravel.

Laravel One to One Relationship Example

The one to one relationship is a very common relationship in laravel model. In this example we will learn to implement and use one to one relationship in laravel. We will also show you how to  insert, retrieve, update, and delete data with the eloquent model from the database table in laravel.

Laravel Eloquent One to One Relationship Example

Let suppose we have two tables posts and contents in our example tutorial. In laravel we can create both tables using migration as following:

Post table migration:

Contents table migration:

In this example, the Post model is closely associated with a Content model in one to one relationship. In one to one relationship we will create a post_content() method within the Post model and there is hasOne() method to relate it with Content model as following.

The Eloquent model adds a foreign key based on the model name and should have a matching id value in it. In this relationship the post_id is the foreign_key for Content model,

Inverse of One to One Relationship Example

For now we can have the access to the content from the post model. Now we create an inverse relationship in the content model. For this we will use the belongsTo method for getting the post data from the content model as following.

We can now access the post content using the relation method as following:

Laravel Insert record in one to one relationship

You can insert record in post and content table using one to one relationship as following:

Now, if you want to retrieve data from both post and content table, you can use one to one relationship as following:

Laravel Delete record in One to One relationship

Now, to delete record from both tables posts and contents in one to one relationship, you can do it as following: