Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you an example of “How to use any route in laravel 8”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.
First, what we’re doing here, This is the example :
Sometimes you may need to register a route that responds to multiple HTTP requests. You may do so using the match method.
Or, you may even register a route that responds to all HTTP requests using any method.
When we define multiple routes that share the same URI, routes using the get, post methods should be defined before routes using the any, match, and redirect methods. This ensures the incoming request is matched with the correct route or not.
Recommended Article: How to check request method in laravel.
Route::any('/', function () {
//
});
Let’s get started.
We will create a blade file and submit the input request parameters in the database table using routes, we need two primary routes for returning a blade file and submitting input parameters to the database.
Basically, we create two routes (get and post) but using any route we can return a blade file and submit form data using a single route.
Generating Migration
At first, we create a new migration file for the “books” table, In your cmd terminal hit the given below command.
php artisan make:migration create_books_table
Migration Structure
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('book_name');
$table->string('book_author');
$table->string('book_description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
Run Migration
php artisan migrate
Create a Model
php artisan make:model Book
App\Models\Book.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
use HasFactory;
}
Create a Controller
php artisan make:controller AnyRouteExampleController
routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AnyRouteExampleController;
#Any-Route-Example
Route::any('/any-route-example',[App\Http\Controllers\AnyRouteExampleController::class, 'index'])->name('any.route');
app\Http\Controllers\AnyRouteExampleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Book;
class AnyRouteExampleController extends Controller
{
public function index(Request $request){
$method = $request->method();
if($request->isMethod('post'))
{
$book = new Book();
$book->book_name = $request->name;
$book->book_author = $request->author;
$book->book_description = $request->description;
$book->save();
dd('Data Submitted Successfully.');
}
else
{
return view('books.create');
}
}
}
resources\views\books\create.blade.php
<main>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="main">
<h3><a>Any Route Example in Laravel.</a></h3>
<form role="form" action="{{route('any.route')}}" method="post">
@csrf
<div class="form-group">
<label for="name">Book name <span class="text-danger">*</span></label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="form-group">
<label for="author">Book Author<span class="text-danger">*</span></label>
<input type="text" name="author" class="form-control" required>
</div>
<div class="form-group">
<label for="description">Book Description <span class="text-danger">*</span></label>
<textarea name="description" class="form-control" rows="4" cols="50"></textarea required>
</div>
<div class="form-group">
<button type="submit" class="btn btn btn-secondary">save</button>
</form>
</div>
</div>
</div>
</div>
</main>
Output :
http://127.0.0.1:8000/any-route-example
In this article, we learned “How to use any route in laravel 8”, I hope this article will help you with your Laravel application Project.
Also Read: How to get the complete user location in Laravel 8.