簡體   English   中英

如何將舊的MySQL數據庫遷移到Laravel Project?

[英]How to migrate old MySQL database into Laravel Project?

我正在嘗試將舊的PHP / MySQL遷移到Laravel項目。 由於許多原因,我不應該修改數據庫表架構。 我需要修改Laravel框架類。 例如,我想強制Laravel Auth檢查users.USER大寫列以返回用戶,而不是users.user 我需要知道Laravel框架的哪一部分檢查users數據庫表或user列。 如何在不破壞默認Auth功能的情況下進行修改。

更具體:我想知道Laravel如何從數據庫中檢索數據/Vendor/laravel/framework/src/illuminate/Auth/EloquentUserProvider.php線137個validateCredentials()

您將必須為表和關系創建所有模型。 在模型上,您可以指定表名,如果不想,則不必重命名表。

例如。

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class User extends Model{
    protected $table = 'users.USER';
}

數據庫方面的考慮

如果要在laravel應用程序上使用自定義數據庫,只需為數據庫的每個表創建一個模型,然后設置表名,主鍵和可填充屬性。

更多信息在這里定義模型

使用自定義用戶和通過字段進行身份驗證

如果您運行了php artisan make:auth命令,但要使用另一個表或字段代替默認用戶並通過而不破壞創建的Auth功能,則必須使laravel知道,請按照下面列出的步驟進行操作:

首先:為要用於身份驗證的表創建一個模型。 為此,請運行命令php artisan make:model FooUserTable ,其中“ FooUser”是表的名稱。

接下來,您必須在模型上設置表名,主鍵和可填寫字段,例如以下行:

class FooUser extends Authenticatable{

    protected $table = 'FooUser';
    protected $primaryKey = 'foo_id_user'; // Place here the name of the id of your table
    protected $fillable = [ // Insert here all the fillable fields of your users table
        'field1',
        'field2',
        ...
    ];

}

請注意,模型現在從Authenticatable而不是model擴展,它是Illuminate\\Foundation\\Auth\\User類的別名。 為了使其有效,只需在文件頂部use Illuminate\\Foundation\\Auth\\User as Authenticatable

這個很重要!:

為了使用另一個字段名稱作為密碼,請將函數getAuthPassword放入用戶表的模型中,例如下面所示:

public function getAuthPassword(){
    return $this->myPasswordField; // myPasswordField is the field on your users table for password
}

現在,您已准備好所有模型,可以進行下一步。

其次:在文件config > auth.php您必須找到provider數組,並將model屬性值更改為您的模型類名稱,例如:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\FooUser::class, // Place here the name of your model
    ],
],

為下一步做好一切准備。

第三:現在,您必須找到默認Laravel Auth的控制器,位於app > http > controllers > auth > LoginController.php ,並在該類中,只需添加一個名為username的函數即可返回要使用的用戶名對於auth操作,例如下面所示:

class LoginController extends Controller{

    ..... // don't change other functions

    public function username(){ // add the function username
        return 'YourUsernameField';
    }

}

刀片登錄模板注意:在登錄刀片模板中,您應該將用戶名輸入名稱更改為新的名稱,並且密碼字段名稱必須仍為password ,這是因為laravel將其用於驗證,但是在內部它將使用您的自定義密碼字段名稱。

就這樣。 我希望這可以幫助你。

注意:切勿將文件編輯到供應商文件夾中!

有關更多信息,請閱讀“ 手動驗證用戶 ”部分的文檔。

所有這些都在Laravel 5.4和5.7版本上進行了測試。

數據庫相關問題/答案:

要從數據庫方案中提取模型,可以使用以下軟件包:

依賴/ Laravel

該軟件包可以自動生成開始在Laravel中使用數據庫所需的Eloquent模型。

它具有許多易於使用的命令,能夠根據您的數據庫方案創建模型。

用法

讓我們從默認連接中搭建一些模型。

php artisan code:models

您可以像這樣設置一個特定的表:

php artisan code:models --table=users

您還可以指定連接:

php artisan code:models --connection=mysql

如果使用的是MySQL數據庫,則可以指定要支持的架構:

php artisan code:models --schema=shop

擴展/編輯身份驗證功能

查看有關手動身份驗證的官方文檔。

如果您在控制器中放置以下代碼段:

public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

如果需要指定要使用的屬性,則可以使用以下屬性:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
  // The user is active, not suspended, and exists.
}

您也可以自己檢查一下,然后直接使用以下代碼登錄代碼:

Auth::login($user);

// Login and "remember" the given user...
Auth::login($user, true);

我在此處列出的所有內容都在上面有關驗證用戶身份的鏈接中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM