* Lưu ý: Chiến thuật của tôi có thể về mặt kỹ thuật 'không chính xác' vì nó gây ô nhiễm không gian tên chung. Tuy nhiên, tôi đã gặp khó khăn khi nhận được các câu trả lời khác và chỉ đăng bài này như một phương sách cuối cùng. *
Tôi đã làm điều này một cách hơi khác so với các câu trả lời khác. Tôi cũng đang sử dụng Laravel 5.
1) Tôi đã tạo thư mục app/Models và di chuyển User.php vào thư mục đó.
2) Tôi đã sửa đổi /app/Models/User.php bằng cách xóa không gian tên ở trên cùng. Điều này có thể gây ô nhiễm không gian tên toàn cầu, nhưng đó là cách duy nhất tôi có thể làm cho nó hoạt động.
<?php // !!!! NOTICE no namespace here !!!!!
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['first_name', 'last_name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
3) tôi đã thêm "app/Mô hình" để phần autoload của tôi trong composer.json:
"autoload": {
"classmap": [
"database",
"app/Models" // <==== I added this
],
"psr-4": {
"App\\": "app/"
}
},
4) tôi sửa đổi auth.php như sau:
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => '\User',
Đối tham chiếu, đây là di chuyển bảng người dùng của tôi:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
Và đây là tập tin UsersTableSeeder.php tôi:
<?PHP
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class UsersTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$users = [
[ 'id' => 1, 'first_name' => 'Bret', 'last_name' => 'Lastname', 'email' => '[email protected]', 'password' => Hash::make('my raw dev password')]
];
foreach($users as $user){
\User::create($user);
}
}
}
?>
Sau tất cả những thông tin cập nhật được thực hiện, trên dòng lệnh tôi chạy:
composer dump-autoload
php artisan migrate --seed
Hope this helps!
Bạn đã thử 'nhà soạn nhạc tự đổ autoload'? – fakemeta
@fakemeta có, vẫn cung cấp lỗi: ( – imperium2335
Bạn đang cố gắng truy xuất đối tượng người dùng bằng cách sử dụng xác thực laravel hay bạn đang tạo nó theo cách thủ công? – fakemeta