Hi, Gurpreet this side again with some new stuff. I would love share my todays learning in this article today. Before this I was working with Laravel Breeze + JQuery. But after watching many video...
Gurpreet Kait
Author
Hi, Gurpreet this side again with some new stuff. I would love share my todays learning in this article today. Before this I was working with Laravel Breeze + JQuery. But after watching many videos on Youtube and I looked into open source then I saw that most of the developers are using livewire. So, I decided to move on livewire. It's first that I have used livewire and after using livewire I decided that do follow livewire as much as possible because JQuery isn't that much helpful for me(personal Opinion).
This is the biggest reason that I am going to use Livewire that is you don't have to write Javascript in your blade or anywhere else. You just need to use Livewire component methods to perform the actions.
I have used Jetstream with Livewire but the action I am going to perform as a very beginner action you don't have to consider any kind of Authentication package there. So, I would love to share that how I start it from beginning..
I assume that you have installed Laravelwith below command or other command.
composercreate-projectlaravel/laravelblog
If you are going forward with Jetstream. Then do consider that you should not have any other package installed before this, then it might can cause some errors.
Installed Jetstream
Actually, this is the first day as well, I started working with Jetstream I heard that It provides some advance features than Breeze. But If you are a beginner don't get stuck into the Jetstream package, I would recommend to start with Breeze. Because It provides very easy way to setup your project and move further. I installed Jetstream first.
composer require laravel/jetstream
php artisan jetstream:install livewire
OR
php artisan jetstream:install livewire --teams//when you do work with big projects/team.
Install Dependencies and Migrate The Database
So, now after installing Jetstream and livewire, It's time to build your NPM dependencies.
-> npm install
-> npm run build
One more thing that I was going to miss that when you do run this NPM command npm install then it provides you the tailwind CSS UI.
Make Migration and Model
Now, As I have said that, we will insert a post data into database so for that we need a database table and for our Laravel logic we need a migration file and a model. Let's create the migration and model.
php artisan make:model Post --migration
Now, we will have a model called Post and a migration file in under the database directory called create_post_table. Now we will create migration first.
After running these commands don't forget to run the migrate command so that you can have the database ready.
Configure your database details in .Env file also so that below command can work well.
php artisan migrate
So, Now we are moving to the main point in which you will learn about inserting data into database by passing from a form to Livewire component.
Add Livewire Component and Page
In Livewire you basically do play with components and pass data through components. So, now I am going to create a post component. I will show you that how I did this today. It was interesting because without thinking about the Ajax/API I have saved the post data easily using Livewire.
Create Component
First of all, I'll make a livewire component and when you do create a livewire component then you also get a class with that component. It's not a big different task just bit different from as we do in core Laravel.
php artisan make:livewire createpost //this is how we create livewire component
Now we will have class and components of livewire.
you can check whether your application is working or not by running initial "php artisan serve" command.
When you have installed the authentication package then you might have a dashboard so where you will have a page called dashboard.
Now, let's add one more page to there for create a post or insert a post.
Create a View file
Now, let's move on your view folder and create an another folder under your view which will be admin folder.
your-project/resource/view/admin //should be in this way
Create an another file in this folder with the name of posts.blade.phpit will be the page where we will render some of our components related to posts.
your-project/resource/view/admin/posts.blade.php //should be in this way
Now, We will extend this page with our app layout. After extending this page with our app layout we will see how we can render the component.
posts.blade.php
<x-app-layout><x-slotname="header"><h2class="font-semibold text-xl text-gray-800 leading-tight">{{ __('Posts') }}</h2></x-slot><divclass="py-12"><divclass="max-w-7xl mx-auto sm:px-6 lg:px-8"><divclass="bg-white overflow-hidden shadow-xl sm:rounded-lg py-4 px-4">
This is post page.
</div></div></div></x-app-layout>
Jump to your layouts folder in view, which will be your app's layout. Jump to app.blade.php and see it will be rendering the @livewire('navigation-menu') then move to navigation-menu.blade.php component and add one more page with the name of CreatePost.
Now, We almost near to create the content. But Now as you can see "the page above" we have to replace the component with "This is post page" content.
As I said we have a component called createpost.blade.php it's time to render this component and use a button to open the model to create this all stuff.
I have copied some already built component because then I don't need to style them. You also can use if you don't want to write Tailwind.
In above code I have used wire:model="createPostModal" and wire:click="showCreatePostModal". So, what is this?
Let me explain, we have create button component in the top and If we want to open the modal then we have to use wire:click and in this we have targeted the model method in which we will show and hide the model.
After making this small change, I get confident that now I can start building my blog further in using Livewire. See, In this way I forget my jQuery and like how easy it is you can imagine. It has lot of other functionalities. Which I would like to learn and share with you in the next blogs.