Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
Request
To retrieve an occurrence or instance of the current HTTP laravel request object through dependency injection, you need to type-hint the Illuminate\Http\Request class on your controller system or method. The service container will automatically insert or inject the incoming request instance.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
Dependency Injection & Route Parameters
If the input is also getting expected by the controller method from a route parameter, you must list your route parameters following your different dependencies. For instance, if your route is determined like so:
Route::put(‘user/{id}’, ‘UserController@update’);
You can, however, type-hint the Illuminate\Http\Request furthermore obtain or access your route parameter id by establishing your controller method as follows:
File name : index.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Update the specified user.
*
* @param Request $request
* @param string $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}
Accessing The Request Through Route Closures
You can additionally type-hint the Illuminate\Http\Request class on a laravel request object route Closure. The service container will automatically include or append the incoming request inside the Closure when it is performed:
use Illuminate\Http\Request;
Route::get('/', function (Request $request) {
//
});
Retrieving the Request Path
The path method will return foo/bar: if the incoming request is targeted at http://domain.com/foo/bar.
$uri = $request->path();
The ‘is’ method enables you to validate that the incoming request path meets a furnished or given pattern. You can utilize the * character as a wildcard when appropriating this method:
if ($request->is('admin/*')) {
//
}
if ($request->is('admin/*')) {
//
}
Retrieving the Request URL
You can apply or use the URL or fullUrl methods to recover or retrieve the full URL for the incoming request. The URL method will return the URL outwardly the query string, while the fullUrl method comprises the query string:
File name : index.php
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
Retrieving the Request Method
The method will render or can say return the HTTP verb concerning the request. You can apply or use the ‘isMethod’ method to establish and analyse that the HTTP verb coordinates a given string:
$method = $request->method();
if ($request->isMethod('post')) {
//
}
Input Trimming & Normalization
Retrieving Input
Retrieving All Input Data
You can further reclaim or retrieve all of the input data as an array utilizing the all method:
$input = $request->all();
Retrieving an Input Value
$name = $request->input('name');
$name = $request->input('name', 'Azzy');
When working regarding forms that include array data inputs, you must use “dot” notation to obtain or access the arrays:
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');
$input = $request->input();
Retrieving Input from The Query String
The query method will solely recover or retrieve values from the query string: While the input method retrieves or regains values from whole request payload (including the query string).
$name = $request->query('name');
$name = $request->query('name', 'Helen');
$query = $request->query();
Retrieving Input Via Dynamic Properties
$name = $request->name;
Retrieving JSON Input Values
$name = $request->input('user.name');
Retrieving a Portion Of The Input Data
$input = $request->only(['username', 'password']);
$input = $request->only('username', 'password');
$input = $request->except(['debit_card']);
$input = $request->except('debit_card');
Ascertaining If an Input Value Is Present :-
//
}
if ($request->has(['name', 'address'])) { }
Flashing Input to the Session
The flash method on the Illuminate\Http\Request class will flash the current input to the session so that it is accessible throughout the user’s following request to the application:
$request->flash();
You can further utilize the ‘flashOnly’ and ‘flashExcept’ methods to flash a subset of the request data to the session. These methods are helpful for administering sensitive knowledge or information such as identifications of passwords out of the session:
$request->flashOnly(['username', 'address']);
$request->flashExcept('password');
Flashing Input Then Redirecting
flash input to the session moreover then redirect to the preceding page, you can readily chain input flashing onto a redirect applying the ‘withInput’ method:
return redirect('form')->withInput();
return redirect('form')->withInput(
$request->except('password')
);
Retrieving Old Input
Apply the ‘old’ method on the Request instance to reclaim or retrieve flashed input from the previous request. The ‘old’ method will draw the earlier flashed input data from the session:
$username = $request->old('username');
laravel request object also implements a global ‘old’ helper. If you are representing old input inside a Blade template, it is plentiful handy to apply the ‘old’ helper. If no old input subsists for the addressed field, null will be returned:
<input type="text" name="username" value="{{ old('username') }}">
Reclaiming or Retrieving Cookies from Requests
Request laravel framework creates all the cookies and is encrypted as well signed with an authentication code, indicating they will be counted invalid if the client modifies. Apply the cookie method on an ‘Illuminate\Http\Request’ instance: to recover or retrieve a cookie value from the request.
$value = $request->cookie('name');
Alternatively, you can apply the Cookie facade to obtain cookie values:
$value = Cookie::get('name');
Appending or Attaching Cookies to Responses
You can add a cookie to an outgoing Illuminate\Http\Response instance utilizing the ‘cookie’ method. You must pass the name, value, and a number of minutes the cookie should be regarded valid to this method:
return response('You are impressive')->cookie(
'name', 'value', $minutes
);
Generating Cookie Instances
laravel request has file that comprises the ability to generate Cookies; If you would prefer to create a Symfony\Component\HttpFoundation\Cookie instance that can be assigned to an acknowledgment instance at a succeeding time, you may apply the global cookie helper. This cookie will not be given back to the client except it is connected to a response instance:
$cookie = cookie('name', 'value', $minutes);
return response('Hello World')->cookie($cookie);
Retrieving Uploaded Files
You are also allowed to obtain uploaded records or files from a Illuminate\Http\Request instance utilizing the file method or applying dynamic properties. The file method states or returns an instance of the Illuminate\Http\UploadedFile class
if ($request->hasFile('photo')) {
//
}
Validating Successful Uploads
In extension to monitoring if the file is already available, you may check that no problems were uploading the data file through the ‘isValid’ method:
if ($request->file('photo')->isValid()) {
//
}
File Paths & Extensions
The UploadedFile class further comprises request laravel methods for locating or accessing the file’s fully-qualified pathway plus its expansion. The ‘extension’ method will endeavor to figure out the file’s extension based on its contents. This extension may be distinct from the extension that was provided by the client:
$path = $request->photo->path();
$extension = $request->photo->extension();
Storing Uploaded Files
$path = $request->photo->store('images');
$path = $request->photo->store('images', 's3');
If you do not desire to generate a file name automatically, you can apply the ‘storeAs’ method, which holds or accepts the path, file name, and disk name as its arguments:
$path = $request->photo->storeAs('images', 'filename.jpg');
$path = $request->photo->storeAs('images', 'filename.jpg', 's3');
How to check if request input field is empty or not?
Laravel Check Request Input Exists
File name : index.php
public function store(Request $request)
{
if($request->has('user_id')) {
dd('user_id is exists.');
} else {
dd('user_id is not exists.');
}
}
Laravel Check Request Input Field Empty or not
File name : index.php
public function store(Request $request)
{
if($request->has('user_id') && !empty($request->input('user_id'))) {
dd('user_id is not empty.');
} else {
dd('user_id is empty.');
}
}
Laravel Check Request All Input Empty or not
File name : index.php
public function store(Request $request)
{
if(count($request->all()) > 0) {
dd('request all input not empty.');
} else {
dd('request all input empty.');
}
}
How to check request is ajax or not?
File name : index.php
public function index(Request $request)
{
if($request->ajax()){
return response()->json(['status'=>'Ajax request']);
}
return response()->json(['status'=>'Http request']);
}
################ OR ##################
public function index()
{
if(Request::ajax()){
return response()->json(['status'=>'Ajax request']);
}
return response()->json(['status'=>'Http request']);
}
################ OR ##################
if (Request::secure())
{
//
}
How to Get Request Parameters in Controller
File name : index.php
public function contact(Request $request)
{
$allParameters = $request->all();
dd($allParameters);
########## OR ############
$allParameters = Input::all();
########## OR ############
$allParameters = \Request::query();
########## OR ############
$id = $_GET['id'];
########## OR ############
$allParameters = $request->input();
}
Get Headers From Request
File name : index.php
$headers = $request->header('connection');
########## OR ############
$headers = \Request::header();
########## OR ############
$headers = getallheaders();
########## OR ############
$headers = apache_request_headers();
File name : index.php