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확인
'라라벨 > livewire_study' 카테고리의 다른 글
laravel livewire turbolinks (0) | 2020.04.17 |
---|---|
라라벨 와이어 동작방법 버튼 카운터 (0) | 2020.04.11 |
데이터바인딩 (0) | 2020.04.11 |
속성사용하기 (0) | 2020.04.11 |
변수값 넘기기 (0) | 2020.04.11 |