Skip to main content

Posts

Showing posts with the label Laravel

Create a Seeder: Generate a seeder class using Artisan that will insert the dummy user

To insert a dummy user for testing purposes so you don't need to insert it manually each time you run `php artisan migrate:fresh`, you can use Laravel's seeding feature.  Here's a step-by-step guide to achieve this: 1. **Create a Seeder**: Generate a seeder class using Artisan that will insert the dummy user.    ```bash    php artisan make:seeder UserSeeder    ``` 2. **Define Seeder Logic**: Update the `UserSeeder` class to insert the dummy user. Open `database/seeders/UserSeeder.php` and add the following code:    ```php    <?php    namespace Database\Seeders;    use Illuminate\Database\Seeder;    use Illuminate\Support\Facades\DB;    use Illuminate\Support\Str;    use Illuminate\Support\Facades\Hash;    class UserSeeder extends Seeder    {        /**         * Seed the application's database.        ...

Laravel form validations

 Laravel Validations: List of types "first_name" => 'required|alpha:ascii|min:3|max:100',// alpha:ascii (only accepts a-z) "middle_name" => 'string', "last_name" => 'required|string', "email" => 'required|email|unique:users,email', "password" => 'required|string|confirmed', "sex" => 'required|string', "phone_no" => 'required|string', "account_type" => 'required|string', "dob" => 'required|date_format:d-m-Y', // date with format "nationality" => 'required|string', "company" => 'required|string', "company_sector" => 'required|string', "company_address" => 'required|string'

Laravel API Docs Setup

 Laravel API's Docs setup Step 1: composer require dedoc/scramble Step 2: Publishing config. Optionally, you can publish the package’s config file: php artisan vendor:publish --provider="Dedoc\Scramble\ScrambleServiceProvider" --tag="scramble-config" Step 3: check the endpoint http://127.0.0.1:8000/docs/api more info: check here

Mysql columns creation in laravel

List of columns  $table->id(); // increment value $table->string('title')->comment('this is blog title'); $table->string('slug')->unique(); $table->text('short_desc'); $table->longText('description'); $table->boolean('is_published')->default(false); $table->integer('min_of_read')->nullable(true); $table->enum('status', ['Active', 'Inactive']); $table->float('discount'); $table->smallInteger('type_id'); $table->date('start_date')->nullable(); $table->timestamps(); $table->foreign('created_by')->references('id')->on('users'); // introducing foreign key $table->unsignedBigInteger('user_id'); //? $table->decimal('latitude', 9, 6)->nullable(true); // Let's say you want starting value from 1000 $table->id()->from(1000); // increment value start from 1000 ->nullabl...

Laravel Commands

Laravale commands #Check route list php artisan route:list #Check upload files links php artisan storage:link #Check database connected or not php artisan db #Make Request file php artisan make:request YourNameRequest #Make Controller #(In this statement you used -r -> resources and -m -> model. It will create CustomersController and Customers Model files) php artisan make:controller CustomersController -r -m Customers #Make Resource file php artisan make:resource CustomersResource #To check migration files status that those files are running or not with below commands php artisan migrate:status #To check if there is any pending migrate files to run #(also this command shows us the mysql query before running migration file) php artisan migrate --pretend #To make a database table (in this example Products name as taken) php artisan make:migration create_products_table #To create a Request file php artisan make:request StoreProductRequest php artisan make:request Up...

Laravel Template blade concept

 All Laravel view files need to store at resources folder.  Blade Document: https://laravel.com/docs/9.x/blade Blade template always have blade extension For ex: Home.php as Home.blade.php {{}} ==> it will print variable data simply act like PHP echo statements. @directive it is used for template or nested template, loops and conditions statements By default, Blade  {{ }}  statements are automatically sent through PHP's  htmlspecialchars  function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax: {!! $title !!} As refer above image, you can simply create a layout.blade.php for common page. Let take sample example html template In Layout Yield second parameter will be a default value if you doesn't set any title value. 

Laravel Restful Actions

Laravel Restful Actions We often use controllers for list some products, add product, update product or Delete product. Now we are dig into for make easy understand the flow. For all this actions you can categorize the 7 Restful actions.   php artisan help make:controller Controller options

Laravel Tinker

Laravel Tinker Laravel Tinker is a powerful REPL Laravel Tinker is a powerful REPL(read-eval-print loop) for the Laravel framework. Tinker by default installed when you install Laravel. May be if you wrongly removed the tinker, you can also install tinker manually using composer composer require laravel/tinker If you are looking Graphical UI, you can check this  Tinkerwell   Usages of Tinker Tinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command php artisan tinker Let's do with more examples of running real scenario, as early notes we did the blogs module. Now we can consider the blog module as example here. Running models using tinker Record saved at database At the same time you can call methods, events and controller using tinker Fetch all data using Tinker

Tips of multiple file creation

Tips of multiple file creation Creating all files at a time using commands Till now we created Controller, model and migration files all are in individual. You can also create all at a time for your project. Ex: if you take the help of PHP artisan commands like php artisan help make:model Creating multiple files using commands Laravel if you notice that we have options in image if you notice that we have options in image -a -all will create all files Controller, Model, Migration For suppose you need to build new module for "Products". In this case you can simply run the command like php artisan make:model Products -mc This "m" will create the migration file and "c" will create the controller for Products. In this single command we are created model, controller and migration file.

Create MySQL tables

Create MySQL tables Creating MySQL database tables from Laravel To follow Laravel standards, We should create a table using Laravel artisan commands. It will help to other teammates automatically update database changes just with one command. To create a table use below command. php artisan make:migration create_yourTableName_table It will create a table and you can also check the migrate files in the below path mylaravel\database\migrations\ Let's create sample table name called "posts" php artisan make:migration create_posts_table Creating "posts" Table After run this command, Migration file will be created. you can check the migration folder Migration file Let's add some columns in the posts table public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('slug'); $table->text('body'); $table->timestamp('pub...

Database and Migrations

Database and Migrations You can config Database in the .env file. By default, Laravel has MySQL configuration. For example, I configured my details DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Laravel_tutorials DB_USERNAME=root DB_PASSWORD= So how it connects the database, In your root folder config/database.php file will read the .env file configuration. Migrations: Migrations are most likely a version control for your database. Advantages You can easily allow your team to modify database schema and share to everyone in the application No headache to add a new column in the database manually. This migration will help all teammates into one path. Now check with artisan command php artisan migrate php artisan migrate This command will create basic users,password_resets and migrations tables. Here migrations table will track of all migrates You can undo previous migration using rollback command php artisan ...