Friday, May 21, 2021

how to create model with the migration , seeder, factory , controller at same time ?

 Syntax : php artisan make:model ModelNmae .

This command will create a model with ModelName. During the model creation, we can create a seeder, factory, controller, migration for the model .

Syntax: PHP artisan make: model Profile --all 

for creating Migration only 

PHP artisan make: model Profile -m 

or 

PHP artisan make: model --migration 

similar way you can generate Factory 

PHP artisan make: model Profile -f

or 

PHP artisan make: model --factory

similar way you can generate Controller

PHP artisan make: model Profile -c

or 

PHP artisan make: model --controller

similar way you can generate ControllerSeeder

PHP artisan make model Profile -s

or 

PHP artisan make: model --seeder



Wednesday, May 19, 2021

Creating a Model In Laravel.

 Synatx : php artisan make:model ModelName 

Let's create a model for Product 

So we in the terminal write PHP artisan make: model Product 

















Writing a factory in Laravel.

By using the factory we can generate some fake data for the testing



<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Product;
use Faker\Generator as Faker;

$factory->define(Product::classfunction (Faker $faker) {
    return [
        'name' => $faker->name// generate a random name 
        'brand'=>$faker->randomElement//generate a random elemtn
        'description'=> $faker->text// bunch of text
        'hsn'=> $faker->numberBetween($min = 1000$max = 9000), 
// number between 1000 -90000 to 9000
        'tax'=> $faker->randomElement(['18%','12%','5%','NILL','28%']) 
// pic a value from this array 
    ];
});.



Creating a factory in laravel

 Command: PHP artisan make: factory FactoryName

ex: PHP artisan make: factory ProductFactory  or you can make it during creating Model

PHP artisan make: model ModelName --all (it creates a model, controller, factory, seeder, and migration)

in this case, we are creating a factory for Product  so we put Product in the place of the model name

D:\www\crm (main)

λ php artisan make: factory RolesFactory --model=Role

Factory created successfully.

It generates a blank  factory file







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