在laravel中我们使用迁移来创建表,然后使用Seeders来播种表,我没有得到它的好处,因为我们可以通过PHPMYADMIN以正常的方式做到这一点,然后我们需要它,因为我们为它编写了许多行,但我们如何证明这些代码行?
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function(Blueprint $table)
{
$table->increments('id');
$table->integer('owner_id');
$table->string('name');
$table->boolean('done');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('items');
}
}
Run Code Online (Sandbox Code Playgroud)
确实,php artisan命令正在创建迁移,但它们的好处是什么?因为我们有替代方法吗?对于Seeder文件也是如此,因为我们为它编写了很多行
class ItemTableSeeder extends Seeder{
public function run(){
DB::table('items')->delete();
$items= array(
array(
'owner_id' => '1',
'name' => 'Watch The Spectacular Now',
'done' => True
),
array(
'owner_id' …Run Code Online (Sandbox Code Playgroud) 我试图用给定的数据在Laravel中播放数据库,我看到了一个关于HASH :: make的片段,在模型中提到它而不是在播种器文件中.
TaskerTableSeeder.php
class TaskerTableSeeder extends Seeder{
public function run(){
Tasker::truncate();
Tasker::create([
'username'=>'junni',
'email'=> 'junni@gmail.com',
'password'=> 'Junaid'
]);
Tasker::create([
'username'=>'test',
'email'=> 'test@gmail.com',
'password'=> 'Test'
]);
Tasker::create([
'username'=>'poni',
'email'=>'poni@loni.com',
'password'=>'Poni'
]);
}
}
Run Code Online (Sandbox Code Playgroud)
我把那个代码放在我的Hash :: make的Tasker模型中
class Tasker extends Eloquent{
public function setPasswordAttribute($value){
$this->attributes['password'] = Hash::make($value);
}
}
Run Code Online (Sandbox Code Playgroud)
这是使您的密码HASH加密的方法,但我没有找到任何有关Laravel文档中的setPasswordAttribute函数的信息..以及我们可以使用这种类型的函数有多少其他属性.
我的数据库1中有两个表:项目3:用户并尝试登录用户并且它正常工作,但我的问题是我没有提到它将从哪个表中获取数据,但它是从自动需要的表格?它是Laravel的特征还是我做错了什么?或者我不明白为什么会这样?
形成
{{ Form::open(array('class'=> 'forming')) }}
{{ Form::text('username',null,array( 'placeholder' => 'Username' ,'class' => 'insi')); }}<br>
{{ Form::password('password',array('placeholder'=>'Password', 'class'=>'paswor')); }}<br>
{{ Form::submit('SignIn!',array('class'=> 'but-n')); }}
{{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)
和AuthController
class AuthController extends Controller{
public function getLogin(){
return View::make('login');
}
public function postLogin(){
$rules = array('username'=> 'required', 'password'=>'required');
$validator = Validator::make(Input::all(),$rules);
if($validator->fails()){
return Redirect::route('login')
->withErrors($validator);
}
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), false);
if(!$auth){
return Redirect::route('login')
->withErrors(array(
'Invalid'
));
}
return Redirect::route('home');
}
}
Run Code Online (Sandbox Code Playgroud)