Found 1380 Questions for PHP

What is middleware in Laravel?

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:21:44
The middleware is a protection layer in laravel. It helps to filter the http request coming down. It acts as the middle layer between request and response. For example − Laravel will check if the user logged in is an authenticated user or not. The request has to pass through the middleware .If it's an authenticated user the user will get a pass otherwise it will be logged out. Another example of using middleware is age restriction. If the user is failing under a certain age range, allow the user to get a few pages otherwise not. The middleware folder ... Read More

How to make Laravel (Blade) text field read-only?

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:20:11
Blade is a template engine that helps you to build your view display in laravel. It also helps you to use php code inside the template. The blade templates are saved as filename.blade.php and are stored inside resources/views/ folder. To understand the above question let us create a view calling the blade template. Example 1 Inside routes/web.php I have created the following route that is calling the view hello with data ['mymsg' => 'Welcome to Tutorialspoint']. Route::get('hello', function () { return view('hello', ['mymsg' => 'Welcome to Tutorialspoint']); }); The hello.blade.php is inside resources/views folder − {{$mymsg}} ... Read More

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

Shilpa Kalangutkar
Updated on 01-Sep-2022 07:17:27
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
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
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
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
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
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
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
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.
1 2 3 4 5 6 7 ... 138 Next
Advertisements