Skip to main content

Posts

Showing posts with the label Laravel

Laravel Sail

 Laravel Sail అంటే ఏమిటి? Laravel Sail అనేది Docker Compose wrapper. సాధారణ Dockerలో: docker run ... docker compose up ... docker compose down ... ఇలా commands ఇవ్వాలి. కానీ Sailలో: ./vendor/bin/sail up ./vendor/bin/sail down ./vendor/bin/sail artisan migrate అంటే Docker commands ని simplify చేస్తుంది. 📋 Prerequisites Windowsలో: ✅ Docker Desktop Installed Check: docker --version docker compose version Expected: Docker version 28 .x Docker Compose version v2.x 🚀 Existing Laravel Project Run చేయడం నీ project folder కి వెళ్ళు. ఉదాహరణ: cd C:\xampp\htdocs\myproject Step 1: .env Configure DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=sail DB_PASSWORD=password ⚠️ ముఖ్యమైన విషయం: Dockerలో MySQL container name mysql అందుకే: ❌ DB_HOST=127.0.0.1 కాదు ✅ DB_HOST=mysql Step 2: Check docker-compose.yml Laravel Sail install అయితే ఇలా ఉంటుంది: services: laravel.test: build: context: './vendor/laravel/sail/runtimes/8.3' ports: ...

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' "bank_account_no" => 'required|min_digits:3|max_digits:5', "role" => 'required|in:admin,editor,viewer', ...

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