Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Articles
Page 11 of 81
How to create and update Laravel Eloquent?
Laravel Eloquent provides powerful methods to create or update records in your database. The updateOrCreate() method and DB facade's updateOrInsert() method offer elegant solutions for upsert operations. Prerequisites: You need Laravel installed with a configured database connection and a Student model created. Assume we have created a table named students using the CREATE statement as shown below − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, ...
Read MoreHow to extract raw form data in Laravel?
In Laravel, you can extract raw form data using several methods depending on your needs. Laravel's Request class provides various methods to access form data in different formats − raw strings, arrays, or individual field values. Using file_get_contents() The file_get_contents() function with php://input returns the raw POST data as a string. This method captures the exact data sent from the form ? _token=zHuIkXpqcRqvZO4vTgxH0fFk5fCmvqSavrCjHVMi&username=testing&password=abcd Using getContent() Method The getContent() method on Laravel's Request class returns the raw request body as a string − similar to file_get_contents() but more Laravel-specific ? ...
Read MoreHow to get a list of registered route paths in Laravel?
In Laravel, you can retrieve a list of all registered route paths using several methods. All routes are stored in the routes/ folder, and you'll need to include the Route facade to work with them ? Make sure Laravel is installed and configured properly before running these examples. Using Artisan Command The simplest way to view all routes is using the artisan command ? php artisan route:list +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+ | Domain | Method | URI ...
Read MoreHow to delete a file from the public folder in Laravel?
In Laravel, you can delete files from the public folder using the File facade or PHP's built-in unlink() function. The File facade provides convenient methods for file operations. To work with File facade you need to include the class as shown below − use Illuminate\Support\Facades\File; Using File::delete() Method The delete() method from the File facade can delete single files or multiple files. It accepts a file path or an array of file paths ? File deleted successfully Using PHP unlink() Function The unlink() function is ...
Read MoreHow to pass an array as a URL parameter in Laravel?
In Laravel, you can pass arrays as URL parameters using several PHP built-in functions. This is useful when you need to send structured data through URLs for API calls or redirects. Using http_build_query() The http_build_query() function converts an array into a URL-encoded query string, making it the most common approach for passing arrays in URLs. Basic Example Here's a simple demonstration of how http_build_query() works ? field1=test&field2=xyz Laravel Controller Example The following example shows how to use http_build_query() in a Laravel controller to construct URLs with array ...
Read MoreHow to get the last inserted ID using Laravel Eloquent?
Eloquent is Laravel's object relational mapper (ORM) that helps interact with databases. Each table has a mapping Model that handles all operations on that table. When you need to insert data and get the ID of the newly created record, Laravel provides several methods to retrieve the last inserted ID. Syntax Using Laravel Eloquent model, you can use the insertGetId() method to get the inserted ID − Model::insertGetId([field=>value, field2=>value…]) The method insertGetId() returns the last inserted ID from the table. Using insertGetId() with Eloquent Model The simplest approach to get the last ...
Read MoreHow to select certain fields in Laravel Eloquent?
Laravel Eloquent provides several methods to select specific fields from database tables instead of retrieving all columns. This approach improves performance by reducing memory usage and network traffic. Using select() Method The select() method allows you to specify which fields to retrieve − The output of the above code is − Siya Khan=>siya@gmail.com This is equivalent to the SQL query − SELECT name, email FROM users WHERE id=1; Using get() with Field Array Another approach is passing field names directly to the get() method ...
Read MoreHow to get the Current URL inside the @if Statement in Laravel?
In Laravel, you can access the current URL within Blade template @if statements using several built-in methods. These methods help you conditionally display content based on the current route or URL. Using Request::path() The Request::path() method returns only the path portion of the URL (without domain) −
Read MoreHow do you check if a field is NOT NULL with Eloquent?
In Laravel's Eloquent ORM, you can check if a field is NOT NULL using the whereNotNull() method. This method filters records where the specified column contains non-null values. Syntax The basic syntax for checking NOT NULL fields ? Model::whereNotNull('column_name')->get(); Using Eloquent Model Here's how to check if the remember_token field is NOT NULL using an Eloquent model ? The output of the above code is − Siya Khan Heena Khan Seema The generated SQL query is − SELECT * FROM users WHERE ...
Read MoreHow to add a new value to a collection in Laravel?
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. Basic Collection Example Here's how to create a basic collection ? Illuminate\Support\Collection Object( [items:protected] => Array( ...
Read More