Found 1466 Articles for PHP

How to remove a parameter from all Request Objects at Controller Level in Laravel?

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:17:27

4K+ Views

To get all the field values from the html form you can make use of the Request class. The class Illuminate\Http\Request; has to be included in your controller. Example 1 This example shows the student registration form and it has fields like name, email, age and address. Student Form @if (count($errors) > 0) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif Student Registration Name Email Age Address The StudentController class is as follows −

How to validate aninput field if the value is not NULL in Laravel?

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:14:03

2K+ Views

To validate data you can make use of the Validation class. The validation helps to validate data as well as to display error messages to the user. Example 1 In the example below the make() method is used. The first argument is the data to be validated and the second is the rule applied on the data : name. $validator = Validator::make( array('name' => 'Disha'), array('name' => 'required|min:5') ); As per the above the name assigned is Disha. As per the rule the name is mandatory and the minimum characters required is 5. ... Read More

How to validate exact words in Laravel?

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:11:53

3K+ Views

To validate data you can make use of the Validation class. The validation helps to validate data as well as to display error messages to the user. To get the exact words to validate you can make use of Rule::in method available with laravel. Using Rule::in method whatever the values provided by this rule has to be matched otherwise it will fail. Example 1 working with Rule::in method To work with Rule::in method you need to include the class : use Illuminate\Validation\Rule; or use Rule; $input = [ 'category' => ['ABC', 'XYZ'], ]; Validator::make($input, [ ... Read More

How to validate Route Parameters in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 14:38:13

9K+ Views

In laravel the routes are defined inside routes/ folder.The routes are defined inside web.php file. The file is created when the laravel installation is done. Laravel routes take in a URI and a closure function as shown below − use Illuminate\Support\Facades\Route; Route::get('/student', function () { return 'Hello Student'; }); The routes defined inside web/routes.php are assigned a web middleware group and they have session states and CSRF protection. You can also call your controller inside routes as shown below − use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']); Following are the route methods you can make ... Read More

How to pass a CSRF token with an Ajax request in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 14:18:40

19K+ Views

CSRF stands for Cross-Site Request Forgeries. CSRF is a malicious activity performed by unauthorized users acting to be authorized. Laravel protects such malicious activity by generating a csrf token for each active user session. The token is stored in the user's session. It is always regenerated if the session changes, hence the token is verified for each session to make sure the authorized user is performing any task. Here is an example to get access to the csrf_token. To generate csrf token You can get the token in two ways. By using the $request→session()→token() By using the csrf_token() method ... Read More

How to compare two encrypted (bcrypt) passwords in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 14:10:07

6K+ Views

In Laravel, you can make use of the Hash facade module to work with passwords. It has bcrypt for helping you store your passwords securely. The Hash facade bcrypt() method is a powerful way to hash a password. It prevents malicious users from breaking the password generated using bcrypt(). The hashing details are available inside config/hashing.php. The default driver has bcrypt() as the hashing to be used. Hashing Passwords To work with Hash Facade you need to include the class: Illuminate\Support\Facades\Hash Example To hash passwords you can use the make() method. Here is an example of a hash password ... Read More

How to define a route differently if a parameter is not an integer?

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

478 Views

The route parameters are available inside the curly braces and the name given has alphanumeric characters. Along with alphanumeric, you can also make use of underscore when choosing the name for your route params. Syntax The syntax for route parameters is as shown below − Route::get('/user/{myid}', function ($myid) { // }); Here myid the route parameter that we want to use further. Multiple Route Params You can have more than one route parameter as shown in the syntax below − Route::get('/students/{post}/feedbacks/{feedback}', function ($postId, $feedbackId) { // }); In above case there are ... Read More

How to upload files in Laravel directly into the public folder?

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

25K+ Views

This is what we have in our public/ folder. Let us move the files uploaded inside images/ folders in public. The file upload display is as follows − The blade template for the above is as follows − Student Form @if (count($errors) > 0) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif Example 1 Now upload a file and see if the changes are in the public folder.

How to insert raw data in MySQL database in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 13:58:54

5K+ Views

You can make use of the Query Builder tool to insert raw data in MySQL tables. You have to include the class: Illuminate\Support\Facades\DB; or use DB; Assume we have created a table named students using the CREATE statement as shown below − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, ... Read More

What are Named Routes in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 13:56:04

2K+ Views

In Laravel routes are defined inside the routes/ folder. All the routes that are required inside your project are defined inside routes/web.php. Example 1 A simple example of defining route is as follows − Route::get('/', function () { return view('welcome'); }); So now when you hit the page http://localhost:8000/ you will be redirected to the view(‘welcome’). Named routes are routes with names. A name is given to the route defined and the name later can be used in case the user wants to perform redirection. Example 2 Here is an example of a named route With ... Read More

Advertisements