Found 1046 Articles for PHP

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

How to check if a cookie is set in Laravel?

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

3K+ Views

When you visit a web page it, usually generates text files that contain small pieces of data such as username and password, and stores them on the user’s browser. These are knowns cookies they used to identify user system and can be accessed by the web server or the client computer (on which they are stored). The information stored in the cookies is specific to a web server. Once you connect to a server a cookie is created labeled with a unique ID and stored in your computer. Once a cookie is exchanged/stored in a client, and if you ... Read More

How do Laravel Eloquent Model Attributes map to a table?

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

3K+ Views

Eloquent is a new object relational mapper (ORM) that helps to interact with databases. With Eloquent each table has a mapping Model that takes care of all the operations on that table. Model in Laravel represents the table in the database. For example, if you have table customers, the model name will be customer, for users it will be user, employees it will be employee. The table name has to be plural and the model name has to be singular. This is a pattern followed, but that does not stop you from using the naming convention of your choice for ... Read More

How to check database connections in Laravel?

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

20K+ Views

Laravel database configuration is stored inside config/database.php. The list of database configurations is listed inside this file. By default, you need to tell Laravel which database you are going to make use of. The default database used is mysql and we are going to stick to it and check the database connection to mysql. /* |-------------------------------------------------------------------------- | Default Database Connection Name |-------------------------------------------------------------------------- | Here you may specify which of the database connections below you wish | to use as your default connection for all database work. Of course | you may use many connections at once using the Database library. ... Read More

How to obtain a list of all files in a public folder in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 08:34:14

2K+ Views

To get the list of all files from public folder you can make use of File facade. To work with it you need to include following class use Illuminate\Support\Facades\File; Following files are present inside public folder −Example 1 To get all files from public folder

How to select count with Laravel's fluent query builder?

Shilpa Kalangutkar
Updated on 30-Aug-2022 08:18:19

17K+ Views

The fluent query builder in Laravel is an interface that takes care of creating and running the database queries. The query builder works fine with all databases supported in laravel and can be used to perform almost all database operations on it. The advantage of using a fluent query builder is that it has protection against sql injection attacks. It makes use of PDO parameter binding and you can be free to send your strings as you need. The fluent query builder supports a lot of methods like count, min, max , avg, sum that fetches you the aggregate values ... Read More

How to get distinct values for non-key column fields in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 12:52:36

8K+ Views

Here are different ways that can give you distinct values for non-key column fields in Laravel. Assume we have created a table named students with the following query CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, mail VARCHAR(20) NOT NULL, ... Read More

How to select all the column names from a table in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 12:23:13

4K+ Views

Here are different ways to get the column names from a table in Laravel. Assume we have created a table named Students in MySQL database using the following query − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL, email VARCHAR(15) NOT NULL, ... Read More

How to find a user in Laravel by Username?

Shilpa Kalangutkar
Updated on 29-Aug-2022 12:15:48

2K+ Views

There are various ways to find a Username in Laravel Using the first() method ExampleThe first() method returns the record found for the search value. It returns null if there are no matching records. To this method You can make use of the method first() to find the user by username.

How to alias a table in Laravel Eloquent queries using Query Builder?

Shilpa Kalangutkar
Updated on 29-Aug-2022 12:13:01

8K+ Views

Eloquent is a new object-relational mapper (ORM) that helps to interact with the database. With Eloquent each table has a mapping Model that takes care of all the operations on that table. Assume we have already created a table with the name student with the following contents − +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | id | name | email | created_at | updated_at ... Read More

Advertisements