Laravel Jetstream tutorial

Laravel Jetstream tutorial

https://ift.tt/33U4wrz


From laravel 8.0, Tylor Otwell introduced an official package for application scaffolding called Jetstream. It’s free and open-source. By this package, you can choose two stacks Laravel-Livewire or Laravel-Inertia. Jetstream views are designed with Tailwind CSS. Let’s see what are the features of Jetstream offer and how we can install and use it for our laravel application development.

Note If you are thinking, why Jetstream? there is already Laravel UI package for auth scaffolding. The answer is, still you can use Laravel UI package or an updated version Laravel Breeze for auth scaffolding but the Jetstream is another option with a lot of features included. Jetstream released with Laravel 8 and it’s not mandatory to use jetstream with Laravel 8. If you need the features described below then it’s highly recommended.

Laravel Jetstream features

  • Open-source, free and maintained by laravel team.
  • Designed with balde+tailwind CSS.
  • Available stacks (Livewire + Blade and Inertia + Vue)
  • Auth scaffolding
  • Profile management
  • Two-factor authentication
  • API token with Laravel Sanctum
  • Team management
  • Multi-session management
  • and more

 

Note If you are using older composer then update it first by composer self-update command.

Installation

After installing laravel 8 application, run the composer command given below for install the Laravel Jetstream package.

composer require laravel/jetstream

 

Choose your stack

Jetstream offers two stacks (Livewire + Blade and Inertia + Vue). You can choose a stack for your application scaffolding.

for Livewire run the command.

php artisan jetstream:install livewire
php artisan jetstream:install livewire --teams

for Inertia run the command.

php artisan jetstream:install inertia
php artisan jetstream:install inertia --teams

 

Now install the NPM dependencies and migration to finish the installation process for Jetstream.

npm install && npm run dev
php artisan migrate

 

Jetstream Visual

After successful installation of Jetstream, you will get the login, registration, profile management view looks like below which are designed with Tailwind CSS.

jetstream-registration.png

[Jetstream Registration View]

jetstream-login.png

[Jetstream Login View] 

jetstream-profile.png

[Jetstream Profile Management View]

 

Jetstream Features Config

To enable or disable a jetstream features is very easy. All the auth related features are listed in config/fortify.php config file.

'features' => [
    Features::registration(),
    Features::resetPasswords(),
    Features::emailVerification(),
    Features::updateProfileInformation(),
    Features::updatePasswords(),
    Features::twoFactorAuthentication(),
],

...

Jetstream features are listed in config/jetstream.php config file.

'features' => [
    Features::profilePhotos(),
    Features::api(),
    Features::teams(),
],
...

 

Jetstream package is really helpful to make the laravel application faster with popular stacks (Livewire and Inertia) in the Laravel community. Hope this post will help you to get an idea about what is Laravel Jetstream package is? is it important for you? and how you can install it in your laravel application. You can learn more about Laravel Jetstream by reading official Jetstream documentation. If you find this post helpful then please share with others.

programming

via Laravel News Links https://ift.tt/2dvygAJ

December 7, 2020 at 08:12PM

Splitting a large Laravel Livewire Component

Splitting a large Laravel Livewire Component

https://ift.tt/39smt4e


Published: Dec 1, 2020 by C.S. Rhymes

Sometimes, no matter what solution you use to build your system, there comes a point where everything tends to grow a bit big and edges towards being unmanageable. Luckily Livewire offers a solution to this problem.

What is Livewire?

Laravel Livewire offers a way to build dynamic interfaces, but instead of using a JavaScript library such as Vue or React, it allows developers to write their code in PHP components and Blade template files. It’s fantastic. If you haven’t used it before then I suggest you check it out.

What is the problem?

Lots of tutorials and examples tell you how to build something small, like a contact form or a todo list. This is absolutely fine as it allows the example to show the build from start to finish, but sometimes you get given a specification for a very large and complex system and are asked to build it.

Let’s use the example of a holiday car rental site. When you want to make your booking you need to store details of the customer, such as their name and address, the type of car you want to hire, as well as any extras such as additional insurance or even hotels for your stay.

One way of approaching this would be to create a single, large component with all the logic in and then a single view file that just keeps on getting longer and longer. It will probably work just fine, but there are some tools available that let you split this up into more manageable chunks.

Splitting the Component Class with Traits

Livewire v2 comes with a great feature of PHP called traits. Traits allow you to create a file that can be used within a class, giving the class the methods and properties of the trait, but allowing you to reuse the same trait from within multiple classes. This allows us to split the logic up between several smaller, more manageable files and ‘use’ them in the main component.

If we go back to our holiday car rental, we might need a trait that contains our methods for getting vehicle data, another for getting location information, such as addresses, or another for storing the customer’s details.

But Livewire traits take things even further too.

Rather than just allowing us to define our own methods we can make use of a naming convention to hook into the Livewire lifecycle hooks too.

We have said we will create a trait that is concentrated on vehicle information, but how do we make that data available to the rest of the component? We could call a method from the mount method of the main component class, but we could also do this in the trait by using the mount method of the trait.

If our trait was called VehicleInformation, then we can create a mountVehicleInformation method in our trait where we set the vehicle data to a public variable and it is then available to the main component.

trait VehicleInformation
{
    public $vehicles;

    public function mountVehicleInformation()
    {
        $this->vehicles = $this->getVehiclesData();
    }

    public function getVehiclesData()
    {
        // Get the vehicles data from somewhere
    }
}

From the example above, we can make use of $this->vehicles from with the main component class and $vehicles from within the blade view.

Splitting validation rules

Livewire gives us a way of creating rules as an array, which are validated when $this->validate(); is called.

class HolidayRental extends Component
{
    use VehicleInformation;

    public $vehicleType;

    protected $rules = [
        'vehicleType' => 'required',
        'startDate' => 'required|date',
    ];

    public function submit()
    {
        $this->validate();
    }
}

This works really nicely, but if we have a very large component, we could end up with may validation rules being defined in the main component class.

To help with the pattern we were using with traits, we could also extract the validation rules to the traits. There doesn’t seem to be anything built in to Livewire traits for this right now, but one method I have discovered is to define separate arrays in each trait and merge them in the constructor for the main component.

trait VehicleInformation
{
    public $vehicles;

    protected $vehicleInformationRules = [
        'vehicleType' => 'required',
    ];

    public function mountVehicleInformation()
    {
        $this->vehicles = $this->getVehiclesData();
    }

    public function getVehiclesData()
    {
        // Get the vehicles data from somewhere
    }
}
class HolidayRental extends Component
{
    use VehicleInformation;

    public $vehicleType;

    protected $rules = [
        'startDate' => 'required|date',
    ];

    public function __construct()
    {
        // Merge the component rules with rules from our trait
        $this->rules = array_merge($this->rules, $this->vehicleInformationRules);

        parent::__construct();
    }

    public function submit()
    {
        $this->validate();
    }
}

This also makes our validation rules more reusable across different components if needed.

Splitting the views

The view for the component is defined in the render method of the component. This is a single blade file.

class HolidayRental extends Component
{
    ...
    public function render()
    {
        return view('livewire.holiday-rental');
    }
}

For some reason it didn’t seem immediately obvious to me that you can use standard blade @include() from within the main livewire.holiday-rental blade view to include partials or sections of the form. I think this was due to my previous experience using Vue components where you have to play around passing state or syncing state between parent and child components. I was over thinking things. Blade files are views and the state is managed in the class so you can include as many subviews as you need and it will render them together.

Reusable sub components

One last thing to consider is how you can use the @include() tag to pass additional settings to a subview. In our example for holiday rentals we may need a search for hotels or car parks. We could build a single search results view that has a search box and displays an image, a title and a description for each result, but we could set what wire model the search box needs and which results to show by setting this in the @include() tag.

// livewire/holiday-rental.blade.php

<h2>Find a Hotel</h2>

@include('livewire.partials.search', ['searchModel' => 'hotelSearch', 'results' => $hotels])

<h2>Find a Car Park</h2>

@include('livewire.partials.search', ['searchModel' => 'carParkSearch', 'results' => $carParks])

// livewire/partials/search.blade.php

<input wire:model=">

@foreach($results as $result)
<div>
    <img src="" alt="" />
    <h3></h3>
    <p></p>
</div>
@endforeach

Planning it all out

Taking sometime to plan out and structure your project before you start diving into the code can bring benefits later on, reducing repetition and making your code more reusable and easier to maintain. I was lucky that I had some time available to spend a few days building a prototype to try out the above techniques before I started building my project for real. Hopefully it will pay off in the long run.

If there are any other techniques that you know of then please share them in the comments and hopefully you will enjoy working with Livewire as much as I do.

Photo by Sasha • Instagram.com/sanfrancisco from StockSnap

programming

via Laravel News Links https://ift.tt/2dvygAJ

December 7, 2020 at 08:12PM

USPTO Patent Center (Beta)

USPTO Patent Center (Beta)

https://ift.tt/3giiKrq

The USPTO has made its new Patent Center available to all users — although still in beta form.

The Patent Center is designed as a more streamlined tool electronic filing of patent applications that combines EFS-Web and PAIR. For now, the PTO is planning to keep those avenues available until the Patent Center achieves full functionality and stability.

legal_stuff

via Patent Law Blog (Patently-O) https://patentlyo.com

December 4, 2020 at 10:18AM

Making a Sphere with Explosives

Making a Sphere with Explosives

https://ift.tt/3mJjbNO

Making a Sphere with Explosives

Link

(Loud) Making a metal sphere usually involves stamping or spinning sheet metal. But this video shows a process where they start out with a shape made out of polygons, then turn it into a sphere by bending the metal with an explosion at its center. We’re not sure of all of the science, but we found a paper on the subject.

fun

via The Awesomer https://theawesomer.com

December 4, 2020 at 02:45PM

How to Move Google Authenticator to a New iPhone

How to Move Google Authenticator to a New iPhone

https://ift.tt/3g9Qas9


Finally, Google has updated its useful Authenticator app on iOS to allow users to export their myriad of tokens and import them on another iPhone directly within the app. This has been my biggest hitch about using Google Authenticator, as it meant that you were in for a world of annoyance when upgrading or switching to a new iPhone—you wouldn’t really “move” Google Authenticator to a new iPhone so much as you would “set it all up from scratch again.” You’d have to visit each service that uses 2FA and either “change” it over by scanning a QR code on your iPhone, or disable 2FA entirely and set it back up again—also by scanning a QR code.

Either way, it was a pain in the ass, and it made my iPhone-owning self incredibly jealous of Android users who could just use the “Export Accounts” feature directly within the app to quickly transfer their Authenticator tokens to a new phone. Why it took Google this long to implement this critical capability in iOS, I’ll never know. But it’s here, and using it is simple:

  • Make sure you’re using the most up-to-date version of Google Authenticator, which you can check via App Store > your Apple ID in the upper-right corner > Swipe down to refresh your list of apps with updates.
  • Launch Google Authenticator.
  • Tap the triple-dot icon in the upper-right corner and select Export accounts.
  • Select up to 10 accounts to export. (Yes, you can only do 10 at a time.) Tap on Export once you’ve made your choice.
  • Launch Google Authenticator on your new iPhone and use it to scan the QR code on your old iPhone.
  • Once you’ve confirmed that the accounts were imported successfully—try logging into one, for example—tap Done on your old iPhone, and then tap Remove accounts.

Repeat the process as necessary, in case you’ve associated more than 10 accounts with Google Authenticator. But that’s it. It’s so easy, and it finally makes Google Authenticator a worthy entrant in the 2FA field—on iOS, at least. I’m not sure I’ll leave Authy to move back to Google’s app, but it’s nice to know that Google Authenticator finally has everything I need.

For what it’s worth, when I tried exporting accounts from my iPhone and importing them into the Android version of Google Authenticator, the process failed. The app instructed me to look for an updated version of Google Authenticator from the Google Play Store, which didn’t make sense, seeing as it was a fresh install of the app. That leads me to believe that this functionality (or fix) is coming soon, but wasn’t ready as of when I wrote this.

G/O Media may get a commission

Tech

via Lifehacker https://lifehacker.com

December 4, 2020 at 03:05PM

DIY Heated Concrete Bench

DIY Heated Concrete Bench

https://ift.tt/3oihV4I

DIY Heated Concrete Bench

Link

If you’re going to park your butt on an outdoor bench in the winter, it can be a jarring experience. HomeMadeModern wanted to improve upon the basic design, so he embedded a heated rubber mat in a DIY concrete bench. Heated seats are great in cars, so we imagine they’d be just as pleasant outdoors.

fun

via The Awesomer https://theawesomer.com

December 2, 2020 at 11:00AM