Laravel Jobs - How Jobs works in Laravel?Laravel

Laravel Jobs - How Jobs works in Laravel?Laravel
Out of 100 only 30/40 people know about Laravel jobs and how its works or where we can use it.
When there is a need to use it specifically or the senior team asked that "You have to use Laravel Jobs", generally then people come to the web and search :
- "How Laravel Jobs works ?" Or "Laravel Jobs works in Local Environment?"
In this tutorial, we are going to understand the basic functionality of jobs and their usage in simple words without any complex examples.
What is a Job?
Jobs generally contain a portion of code that runs in the background without blocking any other code.
You first need to create a job and you can write a portion of code into it.
Then you have to dispatch that job.
How Job works?
Let's say we want to send emails to 100 users. In general, terms what we do is: Execute a loop over each user and send emails one by one.
That will definitely take time and we also have to wait for the response.
Overcome that issue by using jobs.
First, we need to create a job that accepts 10 users and sends emails.
Now we will execute a loop over 100 users, so basically we have to dispatch the job 10 times.
So basically 10 times jobs will be dispatched and immediately we will get a response, as jobs run in the background without waiting for any response.
Hope its more clear now.
A look at what coming to Laravel 10Laravel

A look at what coming to Laravel 10Laravel
I am personally really excited about Laravel 10, are you also excited :) ?. the expected release date for Laravel 10 is 7th February 2023.
In this tutorial, we are going to cover new features and changes from Laravel 10.
Release Date
Laravel 10 will be released by 7th February 2023 and below is a list of annual release dates.
- Laravel 9: February 8th, 2022
- Laravel 10: February 7th, 2023
- Laravel 11: February 6th, 2024
Drop Support for PHP 8.0
Laravel 10 dropping support for PHP <= 8.0, the minimum requirement to run Laravel 10 is PHP 8.1
Deprecations from Laravel 9
In Laravel 10 some of Laravel 9 methods will be deprecated, here is the list.
- Remove various deprecations
- Remove deprecated dates property
- Remove handleDeprecation method
- Remove deprecated assertTimesSent method
- Remove deprecated ScheduleListCommand's $defaultName property
- Remove deprecated Route::home method
- Remove deprecated dispatchNow functionality
Native type declarations in Laravel 10 skeleton
In Laravel 10 the code userland generated by framework will contain the type-hints and return types.
Types will be added into latest PHP type-hinting features to Laravel projects without breaking backward compatibility at the framework level:
- Return types
- Method arguments
- Redundant annotations are removed where possible
- Allow user land types in closure arguments
- Does not include typed properties
Any Many more
Hope that will be usefull.
Laravel hasOneThough & Laravel hasManyThough RelationshipsLaravel

Laravel hasOneThough & Laravel hasManyThough RelationshipsLaravel
Laravel HasOneTHough & Laravel HasManyThough Relationships
We generally know about the 4 basics Laravel Relations ships that laravel provides:
Now we are going to see the major missing relationships that we are thinking are "Hard to use", but in real cases, it's needed while developing a project.
Even though I was thinking that HasOneThough and HasManyThough Relationships were will be hard to understand, I was wrong. it's really easy to understand.
Understanding HasOneThough
Let's say there is a Library that wants to know the book issuer's name directly, in this case, we generally do :
- Fetch Books
- Fetch its related issuer by using the HasOne Relation
What if I say I can directly access the issuer name into the Library model, yes we can achieve this by using the has one though.
Library
- id
- name
Book
- id
- name
- library_id
Issuer
- id
- name
- book_id
public function issuer() {
return hasOneThough(App\Issuer, App\Book, library_id, book_id)
}
Understanding HasManyThough
Let's say in the same example that there are multiple issuers of the same books, we can achieve it as follow.
public function issuer() {
return hasManyThough(App\Issuer, App\Book, library_id, book_id)
}
Hope it will be helpful.
Thanks
How to integrate Paypal payment gateway with Laravel / PHP ?Laravel

How to integrate Paypal payment gateway with Laravel / PHP ?Laravel
How to integrate paypal payment gateway with Laravel / PHP ?
In this tutorial we are going to see to integrate the paypal payment gateway with checkout method using the Laravel.
We are going to use package : https://github.com/srmklive/laravel-paypal
Install the package
composer require srmklive/paypal:~3.0
Publish Assets
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
Set Paypal credentials into config/paypal.php
return [
'mode' => env('PAYPAL_MODE', 'sandbox'), // Can only be 'sandbox' Or 'live'.
'sandbox' => [
'client_id' => env('PAYPAL_SANDBOX_CLIENT_ID', ''),
'client_secret' => env('PAYPAL_SANDBOX_CLIENT_SECRET', ''),
'app_id' => '',
],
......
......
];
Create Routes
routes/web.php
Route::get('paypal-onboard', [PaypalController::class, 'onBoard'])->name('paypal.init');
Route::get('paypal-payment-success', [PaypalController::class, 'success'])->name('paypal.success');
Route::get('paypal-payment-failed', [PaypalController::class, 'failed'])->name('paypal.failed');
Create Controller
app\Http\Controllers\PaypalController.php
setCurrency('EUR');
$provider->getAccessToken();
$data = [
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"value" => 100,
"currency_code" => getCurrencyCode(),
],
],
],
"application_context" => [
"cancel_url" => route('user.paypal.failed'),
"return_url" => route('user.paypal.success'),
],
];
$order = $provider->createOrder($data);
return redirect($order['links'][1]['href']);
}
public function failed()
{
dd('Your payment has been declend. The payment cancelation page goes here!');
}
public function success(Request $request)
{
$provider = new PayPal(); // To use express checkout.
$provider->getAccessToken();
$token = $request->get('token');
$orderInfo = $provider->showOrderDetails($token);
$response = $provider->capturePaymentOrder($token);
dump($orderInfo);
dd($response);
}
}
That's it. Enjoy.
Difference between Eager Loading and Lazy LoadingLaravel

Difference between Eager Loading and Lazy LoadingLaravel
We often listen to the words "Eager Loading" & "Lazy Loading" in Laravel. but maybe some of how still don't know what that actually stands for.
What Lazy Loading means?
I worked with many projects that is developed by some other developers and the common problems in code I found us Lazy Loading queries everywhere.
To understand it more easily let's take one simple example.
Let's say There is Post
model and Comments
Model.
So basically post->hasMany('comments')
So let's say we are fetching 10 posts and now we want the comments of each post. what we will do is :
$post->comments()->get()
(LAZY LOADING)
Lazy loading cause N+1 queries issues as every time we are fetching comments of each post and it will block the execution too for while as its queries from the DB.
What Eager Loading means?
Eager loading is very useful when we are working with large-scale projects. it saves lot's of execution time and even DB queries too :)
Let's take the above example to understand the Eager loading.
$posts = Post::with('comments')->get()
$post->comments
(EAGER LOADING)
here when we retrieve the posts at that time we are fetching its comments too on the same query. so when we do $post->comments
it will not again do query into DB or not even block execution as the comments are already there in model instance.
So this is how Eager loading saves your time and also prevents N+1 Query.
Hope that helps.
Implement Bootstrap Laravel Livewire tablesLaravel

Implement Bootstrap Laravel Livewire tablesLaravel
It's 2022 and people are still using the old jquery tables with Laravel. As laravel have the livewire why do we have to use the jquery tables ??
In this tutorial, we are going to use the livewire tables and gonna see the benefits of it.
The main problem I see with Jquery Datatable is :
- Page will flicker when we do any search, as it will fire the server-side query and fetch results
- HTML Appending into JS for action column
- It's not easy to customize the row, we have to write the HTML into JS
The main benefits of using Laravel Livewire tables are:
- After searching results will be quickly updated on-page, without flickering
- As the Livewire table is JS less, Of course, you don't have to append HTML into it. you can do it via blade files :)
- You can easily customize the row and tables view by adding your custom blade views.
How to integrate Bootstrap Livewire tables?
For that we are going to use the following package :
https://github.com/rappasoft/laravel-livewire-tables
Install Package
composer require rappasoft/laravel-livewire-tables
Publish Assets
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-config
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-views
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-translations
`
Choosing Bootstrap 5 theme
Into the published config file you can choose/change theme to bootstrap-5
return [
/**
* Options: tailwind | bootstrap-4 | bootstrap-5.
*/
'theme' => 'bootstrap-5',
];
Render the components
<livewire:members-table />
Create Component
namespace App\Http\Livewire;
use App\Models\User;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
class MembersTable extends DataTableComponent
{
protected $model = User::class;
public function configure(): void
{
$this->setPrimaryKey('id');
}
public function columns(): array
{
return [
Column::make('ID', 'id')
->sortable(),
Column::make('Name')
->sortable(),
];
}
}
That's It :)
That's it, and you will see the bootstrap-5 Laravel livewire table. it have other lot's of fucntionality too, you can use or disable it as per your need.
Top Laravel packages that you need in 2022Laravel

Top Laravel packages that you need in 2022Laravel
What is Laravel?
Laravel is the most popular PHP framework right now to develop web applications, it offers a very easy environment and services for developers.
In this blog, we are going to know about the packages that we must have to use while developing any laravel application.
Best Laravel Packages
Here we are going to see some best and top laravel packages that will help you to optimize your application performance and it's also very useful while doing the development.
IDE Helper
Github: https://github.com/barryvdh/laravel-ide-helper
It's a very helpful package and saves lots of time for the developer.
It will generate the helper file which enables our IDE to provide accurate autocompletion while doing the development.
Laravel Debugbar
Github : https://github.com/barryvdh/laravel-debugbar
This is very helpful when we have to check the page performance, in sense of how many queries are firing on the specific page? , how many models are loading? etc.
We can show the total processing time of the page, and the query results time too. by using that results we can do some refactor to our code and make our application more optimized.
Spatie Medialibrary
Github : https://github.com/spatie/laravel-medialibrary
This package is very useful when we are doing file uploads. also, it allows us to upload files to the s3 (AWS) very easily by changing just the file system driver.
The main functionality it has is it allows us to associate files with the Eloquent models.
Spatie Role Permission
Github : https://github.com/spatie/laravel-permission
It's 2022 and still, lots of developers are using the custom roles/permissions management. they even didn't familiar that this package have capabilities to manage each role/permissions management with a specific Eloquent model too.
We can assign roles or permissions to the user model or even any model. later we can check it via the middleware that this package is providing.
Ziggy
Github : https://github.com/tighten/ziggy
Before using this package you must need to implement the named routes into your laravel application.
Normally people can just provide a hardcoded URL into the JS file while doing the AJAX calls. But with this package, you can use the route we are using in blade files.
This allows us to use the route()
helper method in the JS files.
How to use Multi Tenant with Multi Databases Into Any Laravel Application ?Laravel

How to use Multi Tenant with Multi Databases Into Any Laravel Application ?Laravel
People are quite afraid :), including me :) when it’s about the developing system that works with multi-tenant / multi-database.
In this tutorial, we will implement a multi-tenant system that will create a separate database when the new tenant will create.
Install Package
composer require stancl/tenancy
Then run the following command :
php artisan tenancy:install
Then add the service provider TenancyServiceProvider
to your config/app.php file:
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\TenancyServiceProvider::class, // <-- here
Setup Tenant Model
namespace App;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
class Tenant extends BaseTenant implements TenantWithDatabase
{
use HasDatabase, HasDomains;
}
Then, configure the package to use this model in config/tenancy.php:
'tenant_model' => \App\Tenant::class,
Create Migrations For tenant
Create one migration and move that migration file to migrations/tenant
. So when we are going to create new tenant this migarations files will run for that new tenant.
You can do the same for seeders. if you want to change the seeder file then you can change it from the config/tenancy.php
Create Actual Tenant
$tenant = Tenant::create([
'id' => time(),
]);
Result
Now when we run above code it will create new tenant and also create new database with related prefix and given id value.
So it will create following things :
- New Tenant will be created in main database
- New tenant database will be created
- New migrations and seeders will be executed into new tenant database.
Hope that will helps a lot.