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>






















How to pass data to the view from the route in Laravel ??

 We can pass the data to the view using help function 

  1. with ( )
  2. compact ( )

Using With 

Route::get('/greet', function () {
   $name = 'Dev';
    // declaring and assigning a value to the variable 
    return view ('greeting')->with('name', $name); // pass the variable to the view 
});
               

HTMl


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Greeting</title>
</head>
<body>
    <h1>Good Morning: <?= $name ?></h1> // calling the variable

    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. 
        Rem repudiandae vel hic natus quo iure reprehenderit esse possimus
         illum placeat magni labore dolorem eveniet quasi, aut optio, dolor 
            harum quibusdam.</p>
</body>
</html>

Out Put























By using compact( ).

 Route::get('/greet', function () {
    $name = 'Dev <span style = "color:red">coder</span>';
    $post = 'coding';
     // declaring and assigning a value to the variable 
     return view ('greeting',compact( 'name','post'));
 });

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Greeting</title>
</head>
<body>
    <h1>Good Morning: <?= $name ?></h1> <!-- php syntax  with out escaping the css  -->

    <h1>Good Morning: {{ $name }}</h1> <!-- laravel syntax escaping -->
    
    <h1>Good Morning:{!! $name !!}-{!! $post !!}</h1> <!-- laravel syntax  with out escaping the css  -->

    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.
         Rem repudiandae vel hic natus quo iure reprehenderit esse possimus illum placeat magni labore dolorem eveniet quasi, aut optio, dolor harum quibusdam.</p>
</body>
</html>

output





























 

       


 

                                                                                                                                                                                      

Sunday, May 16, 2021

how to define a route ??

 locate the route file web.php in side the route directory of the project 


Basic Route which returns a value 

Route::get('/greeting', function () {
    return 'good morning';
});
output on the browser


Basic Route which returns a view  
Route::get('/greeting', function () {
    return view ('greeting');
});
To return a view we need to create  a view first other wise it throw an error view not found
 To create a view locate the view folder and create a file greeting.blade.php 




 
Response on the browser



how to install a fresh laravel project using composer??

Install composer from the web using the link






Using composer installer.

First of all, you have to download the Installer of Laravel with the help of Composer, like this:

composer global require "laravel/installer"


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...