An open API service indexing awesome lists of open source software.

https://github.com/atiqurcode/laravel-livewire-crud

laravel livewire crud
https://github.com/atiqurcode/laravel-livewire-crud

Last synced: 5 months ago
JSON representation

laravel livewire crud

Awesome Lists containing this project

README

          

Laravel Logo


Build Status
Total Downloads
Latest Stable Version
License

## Clone and App run process

Basic requirements
- PHP versions 8.1
- composer
- MySQL 8.* (any versions)

```
git clone
composer install
or
composer install --ignore-platform-reqs
```
Copy the **.env.example** file in ***.env***
or just run this command
```
cp .env.example .env
```
Next update the ***.env*** file

Change these keys in the **.env** file

```
APP_URL=http://127.0.0.1:8000 # change this line app run URL

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_livewire_crud
DB_USERNAME={user_name}
DB_PASSWORD={password}
```

Now run the migrate and app run command via the terminal
```
php artisan migrate
php artisan serve
```

## Code line

First, install the livewire package
```
composer require livewire/livewire
```

Then create a component for users
```
php artisan make:livewire users
```

Now they created fies on both path:
```
app/Http/Livewire/Users.php
resources/views/livewire/users.blade.php
```

Update Component File

#### Here, we will write render(), resetInputFields(), store(), edit(), cancel(), update() and delete() method for our crud app.

So, let's update the following file ```App\Livewire\Users```.

```
users = User::all();
return view('livewire.users');
}

private function resetInputFields(){
$this->name = '';
$this->email = '';
}

public function store()
{
$validatedDate = $this->validate([
'name' => 'required',
'email' => 'required|email',
]);

User::create($validatedDate);

session()->flash('message', 'Users Created Successfully.');

$this->resetInputFields();

// $this->emit('userStore'); // Close model to using to jquery

}

public function edit($id)
{
$this->updateMode = true;
$user = User::where('id',$id)->first();
$this->user_id = $id;
$this->name = $user->name;
$this->email = $user->email;

}

public function cancel()
{
$this->updateMode = false;
$this->resetInputFields();

}

public function update()
{
$validatedDate = $this->validate([
'name' => 'required',
'email' => 'required|email',
]);

if ($this->user_id) {
$user = User::find($this->user_id);
$user->update([
'name' => $this->name,
'email' => $this->email,
]);
$this->updateMode = false;
session()->flash('message', 'Users Updated Successfully.');
$this->resetInputFields();

}
}

public function delete($id)
{
if($id){
User::where('id',$id)->delete();
session()->flash('message', 'Users Deleted Successfully.');
}
}
}
```

#### You can check the blade files code from here
- [blade template](https://github.com/AtiqurCode/laravel-livewire-crud/tree/master/resources/views/livewire)

You need to add home, users, create, update .blade.php file for full crud operations

#### update the web.php file

```
Route::view('users', 'livewire.home');
```

Now you can do crud operations from
**APP_URL/users** or **127.0.0.1:8000/users**