Sunday, October 30, 2022

how to use inker in laravel

Command : php artisan tinker





















get the value 

>>> $state = State::get();
[!] Aliasing 'State' to 'App\State' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#4342
     all: [
       App\State {#4343
         id: 1,
         name: "jharkahand",
         created_at: "2022-10-30 04:28:11",
         updated_at: "2022-10-30 04:28:34",
       },
       App\State {#4344
         id: 2,
         name: "odisha",
         created_at: "2022-10-30 04:38:07",
         updated_at: "2022-10-30 04:38:07",
       },
       App\State {#4345
         id: 3,
         name: "bihar",
         created_at: "2022-10-30 04:38:32",
         updated_at: "2022-10-30 04:38:32",
       },
       App\State {#4346
         id: 4,
         name: "chattisgarh",
         created_at: "2022-10-30 04:39:25",
         updated_at: "2022-10-30 04:39:25",
       },
     ],
   }







create a record

 $state = State::create(['name'=>'maharastra']);
=> App\State {#4327
     name: "maharastra",
     updated_at: "2022-10-30 05:38:28",
     created_at: "2022-10-30 05:38:28",
     id: 5,
   }


or




>>> $state = new State
=> App\State {#4344}
>>> $state->name = 'west bengol';
=> "west bengol"
>>> $state->save();
=> true

check or find the entry by its id 
>>> $state = State::find(6);
=> App\State {#4333
     id: 6,
     name: "west bengol",
     created_at: "2022-10-30 05:40:15",
     updated_at: "2022-10-30 05:40:15",
   }


 







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::class, function (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>






















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