Monday, May 17, 2021

Create a controller in laravel.

To create a controller using the syntax: 

λ php artisan make: controller ControllerName





















Creating some function which returns a string or a view. 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{

    public function root()
    {
        return view('welcome'); //return a view
    }

    public function about()
    {

        $name ="Dev"// declaring a varibale and assigning a value to the varibale 

        return view ('about')->with('name'$name); // passing data to the view 
  with ('name ', $varibale)
    }


    public function contact()
    {
        
        return 'contact'; //return a string
    }
}

let's check the route as well as how we define the routes?

Route::get('/''PagesController@root');
 Route::get('contact''PagesController@contact');
 Route::get('about''PagesController@about');

so when you hit localhost:8000 (port may differ as per your config) this will return
Welcome page

let's create a welcome page first so go to the view directory create a file
welcome.blade.php by default this page generated during installation so you can simply
edit the page as per your choice.

 <div class="title m-b-md">
    Welcome to code factory 20-21 
 </div>






















No comments:

Post a Comment

Call Factory and make multiple record entry Using tinker

  $d = Factory(App\Department::class,3)->create(); > $d = Factory(App\Department::class, 10)->create(); = Illuminate\Database\Eloqu...