라라벨/livewire_study

숫자카운터 db비동기

땀모 2020. 4. 12. 01:52

1. users테이블에 count 컬럼 추가

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->integer('count')->nullable()->default(0);
$table->rememberToken();
$table->timestamps();
});


2. 프론트앤드 스캐폴딩 설치 

composer require laravel/ui


-로그인 스캐폴딩

php artisan ui bootstrap --auth


-npm 설치

npm install


-css빌드하기

npm run dev



3. 구성요소 추가

php artisan livewire:make Counter


4. app/Http/Livewire/Counter.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Counter extends Component
{
public $counter;

public function mount()
{
$this->counter = auth()->user()->count;
}


public function increment()
{
$this->counter ++;
auth()->user()->count = $this->counter;
auth()->user()->save();
}

public function decrement()
{
$this->counter--;
auth()->user()->count = $this->counter;
auth()->user()->save();
}

public function render()
{
return view('livewire.counter');
}
}


5. resources/views/layouts/app.blade.php 

1
2
3
4
5
6
7
8
9
10
<html>
<head>
    ...
    @livewireStyles
</head>
<body>
    ...
    @livewireScripts
</body>
</html>
cs


6. views/home.blade.php 추가

@livewire('counter')


7. 회원가입을 한다.


8. 브라우저에서 버튼을 클릭한다.



9. DB확인