Undefined Index in Laravel Migrate - laravel

I'm new to Laravel. I'm following this tutorial from LinkedIn Learning but I couldn't figure out how to make this work.
Basically, I need to insert new data in the menu_item model. In the tutorial, it uses Category model and they said that I have to create these four categories in the database.
It's very vague and I'm not sure what he means by "in the database", so I assume I should create Starters, Entree, Dessert and Salad columns in the Category model.
When I tried artisan migrate, it said it was successful but when I checked the mysql, I have no such columns! So I created them in mysql and they're there however when I tried to run composer dump-autoload and artisan db:seed --class=MenuItemsTableSeeder it didn't work. All I got was this error: ErrorException : Undefined index: Starters
I'm at loss on what to do.
Here is my menu-items from migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateMenuItemsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('menu_items', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 128);
$table->string('description', 512);
$table->decimal('price');
$table->string('image');
$table->unsignedInteger('category_id');
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('menu_items');
}
}
Here's my category model from migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('Starters');
$table->string('Salads');
$table->string('Entrees');
$table->string('Desserts');
$table->string('name');
$table->string('image');
$table->unsignedTinyInteger('display_order');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
And here's what I want to insert in the menu-item model where the errors occur starting from $startersId = $catIds['Starters'];:
<?php
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class MenuItemsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$categories = DB::select('select * from categories');
$catIds = [];
foreach ($categories as $cat) {
$catIds[$cat->name] = $cat->id;
}
$startersId = $catIds['Starters'];
$saladsId = $catIds['Salads'];
$entreesId = $catIds['Entrees'];
$dessertsId = $catIds['Desserts'];
DB::table('menu_items')->insert([
// Starters
[
'name' => 'Mini Cheeseburgers',
'category_id' => $startersId,
'image' => 'mini_cheeseburgers.jpg',
'description' => 'These mini cheeseburgers are served on a fresh baked pretzel bun with lettuce, tomato, avocado, and your choice of cheese.',
'price' => 8,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'French Onion Soup',
'category_id' => $startersId,
'image' => 'french_onion_soup.jpg',
'description' => 'Caramelized onions slow cooked in a savory broth, topped with sourdough and a provolone cheese blend. Served with sourdough bread.',
'price' => 7,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Artichokes with Garlic Aioli',
'category_id' => $startersId,
'image' => 'artichokes_with_garlic_aioli.jpg',
'description' => 'Our artichokes are brushed with an olive oil and rosemary blend and then broiled to perfection. Served with a side of creamy garlic aioli.',
'price' => 9,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Parmesan Deviled Eggs',
'category_id' => $startersId,
'image' => 'parmesan_deviled_eggs.jpg',
'description' => 'These delectable little bites are made with organic eggs, fresh Parmesan, and chopped pine nuts.',
'price' => 8,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
// Salads
[
'name' => 'Garden Buffet',
'category_id' => $saladsId,
'image' => 'parmesan_deviled_eggs.jpg',
'description' => 'Choose from our fresh local, organically grown ingredients to make a custom salad.',
'price' => 10,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'House Salad',
'category_id' => $saladsId,
'image' => 'house_salad.jpg',
'description' => 'Our house salad is made with romaine lettuce and spinach, topped with tomatoes, cucumbers, red onions and carrots. Served with a dressing of your choice.',
'price' => 7,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => "Chef's Salad",
'category_id' => $saladsId,
'image' => 'chefs_salad.jpg',
'description' => 'The chef’s salad has cucumber, tomatoes, red onions, mushrooms, hard-boiled eggs, cheese, and hot grilled chicken on a bed of romaine lettuce. Served with croutons and your choice of dressing.',
'price' => 9,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Quinoa Salmon Salad',
'category_id' => $saladsId,
'image' => 'quinoa_salmon_salad.jpg',
'description' => 'Our quinoa salad is served with quinoa, tomatoes, cucumber, scallions, and smoked salmon. Served with your choice of dressing.',
'price' => 12,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
// Entrees
[
'name' => 'Classic Burger',
'category_id' => $entreesId,
'image' => 'classic_burger.jpg',
'description' => 'Our classic burger is made with 100% pure angus beef, served with lettuce, tomatoes, onions, pickles, and cheese of your choice. Veggie burger available upon request. Served with French fries, fresh fruit, or a side salad.',
'price' => 10,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Tomato Bruschetta Tortellini',
'category_id' => $entreesId,
'image' => 'tomato_bruschetta_tortellini.jpg',
'description' => 'This classic cheese tortellini is cooked in a sundried tomato sauce. Served with bruschetta topped with a tomato and basil marinara.',
'price' => 14,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Handcrafted Pizza',
'category_id' => $entreesId,
'image' => 'handcrafted_pizza.jpg',
'description' => 'Our thin crust pizzas are made fresh daily and topped with your choices of fresh meats, veggies, cheese, and sauce. Price includes two toppings. Add $1 for each additional topping.',
'price' => 10,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Barbecued Tofu Skewers',
'category_id' => $entreesId,
'image' => 'barbecued_tofu_skewers.jpg',
'description' => 'Our barbecued skewers include tofu, cherry tomatoes, bell peppers, and zucchini marinated in a ginger sesame sauce and charbroiled. Served with steamed rice.',
'price' => 10,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Fiesta Family Platter',
'category_id' => $entreesId,
'image' => 'fiesta_family_platter.jpg',
'description' => 'This platter is perfect for sharing! Enjoy our spicy buffalo wings, traditional nachos, and cheese quesadillas served with freshly made guacamole dip.',
'price' => 16,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
// Desserts
[
'name' => 'Crème Brûlée',
'category_id' => $dessertsId,
'image' => 'creme_brulee.jpg',
'description' => 'Elegantly crafted creamy vanilla custard with a caramelized crunchy layer on top. Served with seasonal fruit.',
'price' => 9,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Cheesecake',
'category_id' => $dessertsId,
'image' => 'cheesecake.jpg',
'description' => 'Our New York Style Cheesecake is rich, smooth, and creamy. Available in various flavors, and with seasonal fruit toppings.',
'price' => 9,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Chocolate Chip Brownie',
'category_id' => $dessertsId,
'image' => 'chocolate_chip_brownie.jpg',
'description' => 'A warm chocolate chip brownie served with chocolate or vanilla ice cream and rich chocolate sauce.',
'price' => 6,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Apple Pie',
'category_id' => $dessertsId,
'image' => 'apple_pie.jpg',
'description' => 'Made with local granny smith apples to bring you the freshest classic apple pie available.',
'price' => 5,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Mixed Berry Tart',
'category_id' => $dessertsId,
'image' => 'mixed_berry_tart.jpg',
'description' => 'Raspberries, blueberries, and strawberries on top of a creamy filling served in a crispy tart.',
'price' => 7,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
]);
}
}

You have added the categories 'Starters','Salads','Entrees','Desserts' in your categories table as columns.
Create a seeder for Categories Table and add these values in name columns.
Update the migration for categories :
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image')->nullable(); // if you do not have images path in seeder
$table->unsignedTinyInteger('display_order');
$table->timestamps();
});
Add the categories table seeder
<?php
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class CategoriesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('categories')->insert([
[
'name' => 'Starters',
'image' => 'image_path',
'display_order' => 1,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Salads',
'image' => 'image_path',
'display_order' => 2,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Entrees',
'image' => 'image_path',
'display_order' => 3,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Desserts',
'image' => 'image_path',
'display_order' => 4,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
]);
}
}

Related

broken column sequence backpack laravel

There was a column order issue. I finished making edits, after a few hours I returned to the project and saw that the columns of the table were mixed up, although before that everything worked correctly.
This is my setup method
public function setup()
{
/*
|--------------------------------------------------------------------------
| CrudPanel Basic Information
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Geozone');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/geozone');
/*
|--------------------------------------------------------------------------
| CrudPanel Configuration
|--------------------------------------------------------------------------
*/
$this->crud->allowAccess('show');
$this->crud->setActionsColumnPriority(10000);
$this->transIt();
$this->addColumnsT([
[
'name' => "polygons_json",
'visibleInTable' => false,
],
[
'name' => 'groups', // the method that defines the relationship in your Model
'type' => "select_multiple",
'entity' => 'groups', // the method that defines the relationship in your Model
'attribute' => "description", // foreign key attribute that is shown to user
'model' => "App\Models\GeozoneGroup", // foreign key model
//'visibleInTable' => false,
'number_columns' => 1,
],
[
'name' => "priority",
'visibleInTable' => false,
],
[
'name' => 'uz',
'type' => 'check',
],
[
'name' => 'frz',
'type' => 'check',
],
[
'name' => 'slz',
'type' => 'check',
],
[
'name' => 'ntz',
'type' => 'check',
]
]);
if(\Request::has('filter')) $this->crud->query = $this->crud->query->where(\Request::get('filter'), true);
if(\Request::has('deleted')){
$this->crud->addClause('onlyTrashed');
$this->crud->removeAllButtonsFromStack('line');
$this->crud->addButtonFromModelFunction('line', 'restore', 'restoreActionButton');
}
$this->addFieldsT([
[
'type' => "textarea",
'name' => 'polygons_json',
'attributes' => [
'readonly'=>'readonly',
],
],
]);
$this->addFieldsT([
[
'type' => "checklist",
'name' => 'groups', // the method that defines the relationship in your Model
'entity' => 'groups', // the method that defines the relationship in your Model
'attribute' => "description", // foreign key attribute that is shown to user
'model' => "App\Models\GeozoneGroup", // foreign key model
'pivot' => true,
'direction' => 'row'
],
]);
$this->addFieldsT([
[
'type' => "color_picker",
'name' => 'fill_color',
'attributes' => [
'pattern' => '^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$'
]
],
]);
$this->addFieldsT([
[
'type' => "color_picker",
'name' => 'stroke_color',
'attributes' => [
'pattern' => '^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$'
]
],
]);
$this->addFieldsT([
[
'name' => 'fill_opacity',
'attributes' => [
'pattern' => '\d{1}(\.\d{1,2})?'
]
],
]);
$this->addFieldsT([
[
'name' => 'stroke_opacity',
'attributes' => [
'pattern' => '\d{1}(\.\d{1,2})?'
]
],
]);
$this->crud->addFilter([ // select2_multiple filter
'type' => 'select2_multiple',
'name' => 'car_group',
'label'=> trans('columns.groups'),
], function() { // the options that show up in the select2
return \App\Models\GeozoneGroup::all()->pluck('description','id')->toArray();
}, function($values) {
$this->crud->addClause('join', 'geozone_to_group', 'id', 'geozone_to_group.geozone_id');
$this->crud->addClause('wherein', 'geozone_to_group.geozone_group_id', json_decode($values));
});
$this->crud->setCreateView('geozone_create');
$this->crud->setEditView('geozone_edit');
// add asterisk for fields that are required in GeozoneRequest
$this->crud->setRequiredFields(StoreRequest::class, 'create');
$this->crud->setRequiredFields(UpdateRequest::class, 'edit');
}
So the columns were arranged
[![enter image description here](https://i.stack.imgur.com/kQ1rP.jpg)](https://i.stack.imgur.com/kQ1rP.jpg)
I tried to change their location in the addColumns method, but it doesn't work. Although it works in other controllers.

Call to a member function find() on array

"message": "Call to a member function find() on array",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
How do I get the id parameter from the array below: When i pass the parameter 1 i want to fetch only first element when id is 2 I fetch item 2 of array
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
return $data->find($id);
}
Since we're using a multi-dimensional array, we can use array_search with array_column.
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$key = array_search($id, array_column($data, 'id'));
echo var_dump($data[$key]);
If you want a "Laravel" alternative solution using collection
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$data = collect($data);
return $data->where('id', $id)->first();
}
public function payments($id){
$data = [
['id' => 1, 'amount' => '22000', 'name' => 'ken'],
['id' => 2, 'amount' => '24000', 'name' => 'ken'],
['id' => 3, 'amount' => '26000', 'name' => 'ken'],
['id' => 4, 'amount' => '2000', 'name' => 'tom'],
];
$data = collect($data);
return $data->where('id', $id)->all();
}

How to override the select2 action that disables subfield options selected in a previous subfield in Backpack for Laravel?

Controller (field definition):
$this->crud->addField([
'name' => 'precios',
'type' => "relationship",
'pivotSelect' => [
'ajax' => true,
'inline_create' => [ 'entity' => 'precio' ],
'data_source' => backpack_url('presupuesto/fetch/precio'),
'minimum_input_length' => 0,
'wrapper' => [
'class' => 'col-md-3 order-2'
],
'attributes' => [
'required' => 'required',
],
'dependencies' => ['categoria_precio_id']
],
'subfields' => [
[
'name' => 'categoria_precio_id',
'type' => 'relationship',
'label' => 'Categoría',
'wrapper' => [
'class' => 'form-group col-md-2 order-1',
],
'dependencies' => ['tipo'],
'ajax' => true,
'data_source' => backpack_url('presupuesto/fetch/categoria-precio'),
'minimum_input_length' => 0
],
[
'name' => 'costo',
'type' => 'text',
'label' => 'Costo Unitario',
'wrapper' => [
'class' => 'form-group col-md-1 order-3',
],
],
[
'name' => 'cantidad',
'type' => 'text',
'wrapper' => [
'class' => 'form-group col-md-1 order-4',
],
],
[
'name' => 'duracion',
'type' => 'text',
'wrapper' => [
'class' => 'form-group col-md-2 order-5',
]
],
[
'name' => 'costo_final',
'type' => 'text',
'label' => 'Costo',
'wrapper' => [
'class' => 'form-group col-md-1 order-6 costo_final',
],
'attributes' => array('readonly' => 'readonly', 'disabled' => 'disabled')
],
],
'tab' => "Precios",
'force_delete' => true]);
My Form capture
When I tried to select an option that I selected previously it does not allow me. For example, in the image "animación 2d" (ID:82).
First I have in my table "precio_presupuesto"
precio_id (PK)
presupuesto_id (PK)
costo
...
created_at
updated_at
And I change that for:
id (PK)
precio_id (FK)
presupuest_id (FK)
costo
...
created_at
updated_at
In my model I had:
public function precios()
{
return $this->belongsToMany(\App\Models\Precio::class)->withPivot('costo', 'cantidad', 'duracion', 'categoria_precio_id', 'costo_final')->withTimestamps();
}
And I change that for:
public function precios()
{
return $this->belongsToMany(\App\Models\Precio::class,'precio_presupuesto', 'presupuesto_id','precio_id', 'id')->withPivot('costo', 'cantidad', 'duracion', 'categoria_precio_id', 'costo_final')->withTimestamps();
}
I'm new to Laravel and Backpack, but I think my definition is fine, I just don't know how to tell Backpack to allow adding two elements with repeated "price_id" and "presupuesto_id", this because there will be cases where you can change the "cantidad" field or the "costo" field for the same combination (precio_id, presupuesto_id).
In other words I just want the "Select2" subfields not to disable previously selected options.
My model
I have tried changing the definition of my models again. Create a class for the pivot table following the documentation and example of:
doc
example
Presupuesto Model
class Presupuesto extends Model implements Auditable{
public function precio_presupuesto(){
return $this->hasMany(PrecioPresupuesto::class, 'presupuesto_id');
}
public function precios(){
return $this->belongsToMany(\App\Models\Precio::class)->using(\App\Models\PrecioPresupuesto::class)->
withPivot('costo', 'cantidad', 'duracion', 'categoria_precio_id', 'costo_final')->withTimestamps();
}
}
Precio Model:
class Precio extends Model{
public function precio_presupuesto()
{
return $this->hasMany(PrecioPresupuesto::class, 'precio_id');
}
public function presupuestos()
{
// return $this->belongsToMany(\App\Models\Presupuesto::class);
return $this->belongsToMany(\App\Models\Presupuesto::class)->using(\App\Models\PrecioPresupuesto::class)->
withPivot('costo', 'cantidad', 'duracion', 'categoria_precio_id', 'costo_final')->withTimestamps();
}}
Pivot Model:
class PrecioPresupuesto extends Pivot{
public function presupuesto(){
return $this->belongsTo(Presupuesto::class, 'presupuesto_id');
}
public function precio(){
return $this->belongsTo(Precio::class, 'precio_id');
}}
Does Backpack allow me to save several pairs of elements like in the image pivot table precio_presupuesto? what is what I need? because it currently does not allow me.

yajra/laravel-datatables filterColumn doesnt work

Page loading correctly and every things work fine while bringing datas. Now I want to filter them but FilterColumn() method doesnt even work. When I tried filter() method it's working but then I won't get $keywords variable. I am missing something while querying leads ?
if (request()->ajax()) {
$leads = Lead::with('country','agent','detail')->orderBy('id','DESC');
$leads->where(function($q) use ($user){
if ($user->hasRole('agent') && !$user->hasRole('admin') && !$user->hasRole('lead')){ //agent isem
$q->where('agent_id',$user->id)
->orWhere('agent_id',null);
}
});
return DataTables::of($leads)->
addColumn('edit_button', function ($lead) {
$link = route('leads.edit',$lead->id);
return ' Edit ';
})->
filterColumn('edit_button', function ($query, $keyword) {
dd($keyword);
return $query->whereHas('patient', function ($query) use ($keyword) {
$query->whereRaw("CONCAT( name, ' ', surname ) like ?", ["%$keyword%"]);
});
})->rawColumns(['edit_button','phone','detail.conversion_notes'])->
toJson();
}
$tableColumn = [
['data' => 'edit_button', 'name' => 'edit_button', 'title' => 'Edit', 'searchable' => false],
['data' => 'full_name', 'name' => 'full_name', 'title' => 'Full Name'],
['data' => 'phone', 'name' => 'phone', 'title' => 'Phone'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'country.name', 'name' => 'country.name', 'title' => 'Country'],
['data' => 'agent.name', 'name' => 'agent.name', 'title' => 'Agent'],
['data' => 'treatment', 'name' => 'treatment', 'title' => 'Treatment'],
['data' => 'find_us', 'name' => 'find_us', 'title' => 'Find Us'],
['data' => 'form_type', 'name' => 'form_type', 'title' => 'Form','visible' => false],
['data' => 'social_account', 'name' => 'social_account', 'title' => 'Sosyal Medya'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'],
['data' => 'detail.conversion_notes', 'name' => 'detail.conversion_notes', 'title' => 'Primary Notes'],
];
$html = $builder->columns($tableColumn)->parameters([
"pageLength" => 25,
"lengthMenu" => [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
'dom' => 'Bfrtip',
'columnDefs' => [
['width' => '2%', 'targets' => 0],
['width' => '7%', 'targets' => 1],
'buttons' => [
'pageLength',
[
'extend' => 'colvis',
'collectionLayout' => 'fixed two-column',
'columns' => ':not(.noVis)'
]
]
]);

These credentials do not match our records

I have an app with laravel in which, I have different users and different roles. I insert some users, roles, role_user, permission and permission_role in my DB using seeder but, when I try to login using email and password already recorded in DB, I got : These credentials do not match our records.
this is UserTableSeeder :
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => 'passwordadmin'
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => 'passwordpresident'
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => 'passworduser'
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => 'passworduser'
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
RoleTableSeeder :
public function run()
{
//
$role = [
[
'name' => 'admin',
'display_name' => 'Administrateur',
'description' => 'Administrateur du système'
],
[
'name' => 'president',
'display_name' => 'Président',
'description' => 'President de la commune'
],
[
'name' => 'utilisateur_normal',
'display_name' => 'membre du conseil',
'description' => 'membre du conseil'
]
];
foreach ($role as $key => $value) {
Role::create($value);
}
}
RoleUserTableSeeder :
public function run()
{
//
DB::table( 'role_user' )->insert([
[ 'user_id' => 6, 'role_id' => 4 ],
[ 'user_id' => 7, 'role_id' => 5 ],
[ 'user_id' => 8, 'role_id' => 6 ],
[ 'user_id' => 9 , 'role_id' => 6 ],
]);
}
Models I have : User, Role, Permission.
Any idea please ?
It looks you are not hashing your passwords into your database, you need to use bycript:
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => bcrypt('passwordadmin')
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => bcrypt('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => bcrypt('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => bcrypt('passworduser')
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
You can see more seeder examples on the oficial seeder Laravel documentation.
As Troyer said, you need to hash them with bcrypt. Laravel provides a handy wrapper with \Hash::make($str).
It is extremely bad practice to have your passwords in plaintext.
You need to run your seeder like this:
public function run()
{
$users = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => \Hash::make('passwordadmin') // Hash them in order to make use of \Auth library (and more importantly, in order not to be a dick to your users)
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => \Hash::make('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => \Hash::make('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => \Hash::make('passworduser')
]
];
User::create($users); // Import them with 1 query for performance
}

Resources