Laravel
<button data-customid="{{$category->id}}" type="button" data-toggle="modal" data-target="#category" class="btn btn-sm btn-info TheButton" >Edit</button>
<script>
document.addEventListener('DOMContentLoaded', () => {
let buttons = document.querySelectorAll('.TheButton');
addEventToButtons(buttons);
});
function addEventToButtons(buttons) {
buttons.forEach((button) => {
button.addEventListener('click', (e)=>{
let form = document.getElementById('form');
const id = e.target.dataset.customid;
form.action = `http://shoes-store.mc/admin/categories/${id}`;
});
});
}
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script>
@if(Session::has('success'))
toastr.success('{{Session::get("success")}}');
@elseif(Session::has('error'))
toastr.error("{{Session::get('error')}}");
@endif
@if ($errors->any())
@foreach ($errors->all() as $error)
toastr.error("{{$error}}");
@endforeach
@endif
</script>
1. using model factory database/factories/UserFactory.php
$factory->define( App\modelInChargeOfTable::class , function(Faker $faker){
return [
'columnName' => $faker->method()
];
} )
i.e.
$factory->define( App\Todo::class , function(Faker $faker){
return [
'content' => $faker->sentence(10)
];
} )
2. using seeder to import data into table
a. create a new seed
php artisan make:seeder TodosTableSeeder
// naming convention: tableName-Table-Seeder
b. database/seeds/TodosTableSeeder.php
public function run()
{
factory(App\Todo::class, 10)->create();
}
// factory( App\ModelName::class , number of data)->create()
// 'ModelName' is the one you specify within the $factory->define
c. include the TodosTableSeeder inside the DatabaseSeeder.php
$this->call(TodosTableSeeder::class);
3. run the seeder
php artisan db:seed
1. Creating Records: using ::create with Model
method 1:
// create a bear
Bear::create(array(
'name' => 'Super Cool',
'type' => 'Black',
'danger_level' => 1
));
method 2:
// alternatively you can create an object, assign values, then save
$bear = new Bear;
$bear->name = 'Super Cool';
$bear->type = 'Black';
$bear->danger_level = 1;
// save the bear to the database
$bear->save();
method 3:
// find the bear or create it into the database
Bear::firstOrCreate(array('name' => 'Lawly'));
2. Getting and Finding Records
// get all the bears
$bears = Bear::all();
// find a specific bear by id
$bear = Bear::find(1);
// find a bear by a specific attribute
$bearLawly = Bear::where('name', '=', 'Lawly')->first();
// find a bear with danger level greater than 5
$dangerousBears = Bear::where('danger_level', '>', 5)->get();
// First vs Get
// When querying the database and creating where statements, you will have to use get() or first().
// first will return only one record and
// get will return an array of records that you will have to loop over.
3. Updating Records
// find the bear
$lawly = Bear::where('name', '=', 'Lawly')->first();
// change the attribute
$lawly->danger_level = 10;
// save to our database
$lawly->save();
4. Deleting Records
// find and delete a record
$bear = Bear::find(1);
$bear->delete();
// delete a record
Bear::destroy(1);
// delete multiple records
Bear::destroy(1, 2, 3);
// find and delete all bears with a danger level over 5
Bear::where('danger_level', '>', 5)->delete();