4 Laravel E-Commerce Application Development - Admin Model and Migration - LaraShout 4
4 Laravel E-Commerce Application Development - Admin Model and Migration - LaraShout 4
TABLE OF CONTENT
https://www.larashout.com/admin-model-migration 1/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
05 Final Word
https://www.larashout.com/admin-model-migration 3/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
https://www.larashout.com/admin-model-migration 4/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
https://www.larashout.com/admin-model-migration 5/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
https://www.larashout.com/admin-model-migration 6/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
Now as you know, Laravel provides a User model out of the box
which resides in the app folder, I want to move this model le
to Models folder so I will create a new folder called Models and
move the le inside this folder. You will also need to change the
namespace of User model class from App to App\Models.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
],
https://www.larashout.com/admin-model-migration 7/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
Now we will start creating the model, migrations, and seed for
the Admin. Go to your terminal and run below artisan
commands to generate the model, migrations and database
seed.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
https://www.larashout.com/admin-model-migration 8/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
https://www.larashout.com/admin-model-migration 9/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Now we will update the seeding class for our Admin model, for
that open the AdminsTableSeeder class from database/seeds
folder and replace with the below content:
use App\Models\Admin;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
Admin::create([
'name' => $faker->name,
https://www.larashout.com/admin-model-migration 10/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
As you can see in this seed class we are adding the Admin
model using the use keyword and also adding Faker to generat
dummy names.
function.
$this->call(AdminsTableSeeder::class);
https://www.larashout.com/admin-model-migration 11/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
After running this command, you will see the following output
in the terminal.
Now if you open your database and check the admins’ table,
you will nd a new entry was added to your admin table.
Final Word
In this part, we simply created the Admin model and migration
with database seed. My plan was to create all models and
migrations for our application, but I changed my mind to create
models and migrations as we go. It will be easier for you to
digest the information and I can explain each and every step.
Code Repository
https://www.larashout.com/admin-model-migration 12/26
2/13/2020 Laravel E-Commerce Application Development - Admin Model and Migration | LaraShout
tks admin
REPLY
Saif says:
For someone who is facing the class AdminstableSeeder does not exist:
when running command php artisan migrate –seed an issue is coming that
https://www.larashout.com/admin-model-migration 13/26