Giter Club home page Giter Club logo

tao-them-table-thanh-vien-laravel's Introduction

Tao-them-table-thanh-vien-laravel

Cách tạo thêm tabale thành viên Để tạo thêm table thành vi nữa trong laravel ta làm như sau: Ví dụ mặc định trong laravel có table là users nhưng ta muốn tạo thêm table khác nữa là members thì làm như sau:

1. Ta nhân bản table users ra thành members

2. Ta tạo model cho table member như sau:

php artisan make:model Thanhvien

Trong code sẽ sửa lại như sau:

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Thanhvien extends Authenticatable

{

   use Notifiable;

    protected $table = 'members';

    protected $fillable = ['email',  'password'];

    protected $hidden = ['password',  'remember_token'];

}

3. Ok giờ ta vào config/auth.php để thêm phần sau:

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
  
    // kéo xuong sua tiep
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'thanhvien'  => [
            'driver'  => 'session',
             'provider' => 'thanhviens',
        ],


        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    
  'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'thanhviens' => [
            'driver' => 'eloquent',
            'model' => \App\Thanhvien::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    
   

4. Tiếp theo tạo middleware với câu lệnh như sau: php artisan make:middleware RedirectIfNotThanhvien

Sau tạo xong và ta sửa lại được như thế này

<?php

namespace App\Http\Middleware;

use Closure;

class RedirectIfNotThanhvien
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard="thanhvien")
    {
        if(!auth()->guard($guard)->check()) {
            return redirect(route('frontend.login'));
        }
        return $next($request);
    }
}

5. Tiếp theo ta vào app/Http/Kernel.php để thêm dòng sau

 protected $routeMiddleware = [
   ......
   'thanhvien' => \App\Http\Middleware\RedirectIfNotThanhvien::class,
 ];

6. Tiếp theo tạo Controller cho member ví dụ mình tạo như này php artisan make:controller MemberControler

Trong đó mình khai báo nhu sau

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class MemberLoginControler extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/profile-thanh-vien';

    
    public function __construct()
    {
      $this->middleware('guest')->except('logout');
    }
    
    public function guard()
    {
     return Auth::guard('thanhvien');
    }

    
    public function showLoginForm()
    {
        if (!\Auth::guard('thanhvien')->check()) {
            return redirect()->route("frontend.login");
        }
        // check thử xem đã đăng nhap chua
        return redirect('/');
    }

}

7. Ok giờ phần Route mình khai báo như sau

Route::get('/thanh-vien/login', 'MemberLoginControler@showLoginForm')->name('thanhvien.login');
Route::post('/thanh-vien/login', 'MemberLoginControler@login')->name('thanhvien.login.post');
Route::post('/thanh-vien/logout', 'MemberLoginControler@logout')->name('thanhvien.logout');


Route::group(['middleware'=>'thanhvien'], function() {
    Route::get('/thanh-vien/home', 'MemberLoginControler@index');
});

8. Gio tạo form đăng nhap login.blade.php

<form class="form-horizontal" method="POST" action="{{ route('thanhvien.login.post') }}">
    {{ csrf_field() }}

    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
        <label for="email" class="col-md-4 control-label">E-Mail Address</label>

        <div class="col-md-6">
            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

            @if ($errors->has('email'))
                <span class="help-block">
                    <strong>{{ $errors->first('email') }}</strong>
                </span>
            @endif
        </div>
    </div>

    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
        <label for="password" class="col-md-4 control-label">Password</label>

        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password" required>

            @if ($errors->has('password'))
                <span class="help-block">
                    <strong>{{ $errors->first('password') }}</strong>
                </span>
            @endif
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="remember"> Remember Me
                </label>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-8 col-md-offset-4">
            <button type="submit" class="btn btn-primary">
                Login
            </button>

        </div>
    </div>
</form>

Chúc cả nhà thành công

tao-them-table-thanh-vien-laravel's People

Contributors

buipho90 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.