I'm a beginner in Laravel and really hope i can get help with this problem i am facing. I already have been reading the Laravel docs a lot but cant seem to get any further.
What i have done is add a new column to my existing table of members. This column is called 'vacation' and is a boolean with the value of 0 or 1 or true or false what ever way you want to see it.
I did this by doing:
php artisan make:migration add_vacation_to_members_table
then i edited the migration file like this:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddVacationToMembersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('members', function (Blueprint $table) {
$table->boolean('vacation')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('members', function (Blueprint $table) {
$table->dropColumn('vacation');
});
}
}
Now i ran:
php artisan migrate
All good i could see the column has been added to the members table in PhpMyAdmin.
Now the question:
I wish to have a button in a laravel blade file that can change this value from 0 to 1 or from true to false what ever way you see it. I understand i need to make a controller for this and define a route in web.php However i just cant seem to get it right and keep starting over. So now i thought it's time to ask for some help and pointers.
Button could even be a checkbox with a save change button.
Using Bootstrap 4 as CSS.
Any help will be very much appreciated.
Thanks :)
CodePudding user response:
I understand and totally get the confusion, and so instead of getting you the exact code, I'll give you the steps and maybe from this you can try and have your implementation:
- Create a view file say vacation.blade.php and create your form there.
- The form will post to a route you need to define in you web.php e.g.
Route::post('/your-route-name-here', [ToYourController::class, 'store'])->name('store-vacation'); // here ensure you import the controller
- From the above route, you will notice, we've defined a controller which is actually our next step, Create your controller and define the store method which is meant to process or in this case store the vacation to the database of course
- Sample store from controller:
public function store () {
YourModelClass::create([
'vacation' => request('vacation');
]);
/* if your action above is update, you can just switch
* create with update and provide the necessary fields
*/
}
CodePudding user response:
What I realy think you need to know is just the Logic how you are going to do Things there. in your blade you need to have Link
<a href="{{url('/vacation'}}" class="btn btn-sm btn-primary">Vacation</a>
This will add button to your blade which call the route /Vacation so in your route/web you have to register that route so that when you press the button to find it there
Route::get('Vacation','VacationController@index');
so this means when user press the Link it calls route vacation but find the function index in the controller VactionController
After that you have to create the Vacation Controller just by writting in your terminal
php artisan make:controller VacationController
the controller will help you to add the logic. you have to add the function you declared in the Route index
public function index()
{
//
$variable=Model::create([
'vacation'=>true,
]);
remember in you Model you need to add Vacation in Fillable