I am doing a project with Laravel 8, which has a database with a boolean column, this column registers 0 and 1.
Attached the migration of the BD:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('tipo_usuario')->default(0); //0 == editor && 1 == administrador
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I would like to perform a check in the update of the edition of my users, so that if they change the value from 0 to 1 in the edition form and there are no more values of type 1 in the mysql table, it does not allow editing the form and returns the user to the CRUD index page.
I have tried the following, but I don't know how to check this.
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'surname' => 'required',
'email' => 'required | email',
'password' => 'required | min:8',
]);
$users = User::find($id);
$users->name = $request->get('name');
$users->surname = $request->get('surname');
$users->email = $request->get('email');
$users->tipo_usuario = $request->get('tipo_usuario');
$users->password = Hash::make($request->get('password'));
if ($users->tipo_usuario != null) {
$users->update();
return redirect('/users')->with('success', 'Usuario editado correctamente.');
} else {
return redirect('/users')->with('message', 'No puede eliminar todos los administradores.');
}
}
Basically I think I would need help on this line to make it work properly:
if ($users->tipo_usuario != null) {
To understand how it works, I'll briefly explain what I'm trying to do. We have administrator users (value 1) and editor users (value 0). If an administrator changes his role from 1 to 0 in the editing form, he is automatically logged out of his account and becomes an editor. To prevent the page from being left without administrators. My idea was to not allow changing the last value of 1 that remains in the column to a 0 so that there is always an administrator.
It should also be possible to change the values from 0 (editor) to 1 (administrator) later.
Thank you very much in advance.
Edit
I've tried the following, but it doesn't seem to work:
$admin = User::where('tipo_usuario', '=', 1)->first();
if ($admin != null) {
$users->update();
return redirect('/users')->with('success', 'Usuario editado correctamente.');
} else {
return redirect('/users')->with('message', 'No puede eliminar todos los administradores.');
}
I tell you the following ( as an example and starting from the same you could adjust ):
First you must evaluate if in the request data there is a value 0 for the type of user
If it does indeed bring a value of 0 then you should proceed with the following:
I consider that the query should not be focused on finding the data of a specific user but on a count of elements based on a condition of evaluating if it
tipo_usuario
is equal to 1, since with what you have at the moment you would not really know how many administrators are left evenIf said count is greater than 1 then allow the update, otherwise it would not proceed.
If it does not have a value of 0 for the user type, then allow the update since the user type has not changed and it would continue to exist as an administrator.
Clarification: