Create new page in laravel

To create a new page in Laravel, you can follow these steps:

Step 1: Create a new controller

In the terminal, navigate to the root directory of your Laravel project and run the following command to create a new controller:

php artisan make:controller PageController

This will create a new file called PageController.php in the app/Http/Controllers directory.

Step 2: Define the controller method

In the PageController.php file, add a new method to handle the page request. For example:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function index()
    {
        return view('pages.index');
    }
}

This method will render a view called index.blade.php located in the resources/views/pages directory.

Step 3: Create the view

Create a new file called index.blade.php in the resources/views/pages directory:

<!-- resources/views/pages/index.blade.php -->

<h1>Welcome to my page!</h1>

This is a simple HTML page with a heading.

Step 4: Define the route

In the routes/web.php file, add a new route to map the URL to the controller method:

Route::get('/page', 'PageController@index');

This route will match any GET requests to the /page URL and call the index method of the PageController controller.

Step 5: Run the migration and seed the database (optional)

If you want to store data related to the page in the database, you'll need to create a migration and seed the database. Run the following command to create a new migration:

php artisan make:migration create_pages_table

Then, open the create_pages_table migration file and add the necessary code to create the table:

// database/migrations/2023_02_20_000000_create_pages_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePagesTable extends Migration
{
    public function up()
    {
        Schema::create('pages', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('pages');
    }
}

Run the migration to create the table:

php artisan migrate

Then, run the following command to seed the database with some sample data:

php artisan db:seed

Step 6: Test the page

Open a web browser and navigate to http://localhost:8000/page (or the URL you specified in the route). You should see the page rendered with the heading "Welcome to my page!".

That's it! You've created a new page in Laravel.