Select Certain Fields in Laravel Eloquent

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:29:55

8K+ Views

Laravel Eloquent provides many ways to select the fields that you need from the table. Here are a few examples that show how it can be done. Example 1 The following is an example of this −

Get Current URL Inside If Statement in Laravel

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:26:02

1K+ Views

To get the current URL you can use the methods explained in the example below − Example 1

Insert New User into MySQL Table from Laravel

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:22:06

954 Views

You can insert a new user in the following ways as shown in the examples below. To insert a new user from Laravel you can do it using artisan seeder. To do that first create a seeder using the command below − php artisan make:seeder UserSeeder Once the command is executed, you will get the UserSeeder.php file inside database/seeders. Add the code to add user records inside users table in the seeder file as shown below. Example 1

Check If a Field Is Not Null with Eloquent

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:20:08

5K+ Views

In the Eloquent Model you can make use of whereNotNull() method to get the not null values from the database. Example 1 In this example we are checking the field rememer_token if it’s NOT NULL.

Add a New Value to a Collection in Laravel

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:09:46

13K+ Views

Collection in Laravel is an API wrapper that helps you deal with different operations to be performed on arrays. It makes use of the class Illuminate\Support\Collection to deal with arrays in Laravel. To create a collection from a given array you need to make use of the collect() helper method that returns a collection instance. Later you can use a chain of methods like converting to lowercase, sorting on the collection instance. Example 1

Delete a Single Record in Laravel 5

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:08:06

546 Views

There are different ways you can do it. Let us test a few examples that help us delete a single record in Laravel. Assume we have created a table named users table as shown below − mysql> select * from users; +----+---------------+------------------+--------------+------------+------+ | id | name | email | password | address | age | +----+---------------+------------------+--------------+------------+------+ | 1 | Siya Khan | siya@gmail.com | hgfdv3vhjd ... Read More

Check if User Email Already Exists in Laravel

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:05:43

5K+ Views

There are many ways you can test if email exists. One way is using the validator class. To make use of validator you need to include the class as shown below; use Illuminate\Support\Facades\Validator; Example 1 The example shows how to make use of Validator to check if email is registered or not.

Order Results of Related Models in Laravel Eloquent

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:00:22

481 Views

Laravel Eloquent comes with the orderBy() method that can be used to order the results from the model. We are going to make use of Users table as shown below − Let us create another table user_roles with following data − Example 1 Following examples show how to make use of orderBy to order results.

Update a Query on Laravel Fluent Using DB::raw

Shilpa Kalangutkar
Updated on 30-Aug-2022 09:12:45

6K+ Views

You can make use of db::raw when you want to test or use some arbitrary string inside the fluent query builder. To work with db::raw you have to make use of the DB facade class − use Illuminate\Support\Facades\DB; Here are a few examples that show the use of DB:raw(). Example 1

Chunk Results from a Custom Query in Laravel

Shilpa Kalangutkar
Updated on 30-Aug-2022 09:06:15

7K+ Views

If your database table has lots of data, chunk() method is the best to use. The chunk() method can be used on the DB facade and also on Eloquent models. The chunk() takes care of fetching a small amount of data at a time and the result is present inside the closure for processing. Consider the following users table that has 1000 records in it. We are going to make use of chunk() to retrieve 100 records at a time.Example 1

Advertisements