I want to do in laravel that shows a User depending on the day of the calendar.
Suppose I have a DB with N users, and they must cover one day of guard in the L-V job in order of 1-N, ex:
-Thursday 2 August: "It is up to you to guard the User with ID 1"
-Friday 3 August: "" "with ID 2"
-Lunes 6 August: "" "with ID 3"
Tuesday August 7: "" "with ID 4"
... Until you reach user N, and the cycle repeats. 1, 2, 3. Depending on the day, only L-V.
With this function I determine the days of the week excluding S-D
public function getDiasHabiles($fechainicio, $fechafin, $diasferiados = array())
{
// Convirtiendo en timestamp las fechas
$fechainicio = strtotime($fechainicio);
$fechafin = strtotime($fechafin);
// Incremento en 1 dia
$diainc = 24 * 60 * 60;
// Arreglo de dias habiles, inicianlizacion
$diashabiles = array();
// Se recorre desde la fecha de inicio a la fecha fin, incrementando en 1 dia
for ($midia = $fechainicio; $midia <= $fechafin; $midia += $diainc) {
// Si el dia indicado, no es sabado o domingo es habil
if (!in_array(date('N', $midia), array(6, 7))) { // DOC: http://www.php.net/manual/es/function.date.php
// Si no es un dia feriado entonces es habil
if (!in_array(date('Y-m-d', $midia), $diasferiados)) {
array_push($diashabiles, date('Y-m-d', $midia));
}
}
}
return $diashabiles;
}
In the part of my controller in the edit function is what I have to be able to add the days of the week in my database
public function edit(User $user)
{
$dias = $this->getDiasHabiles('2018-07-30', '2018-08-06', ['2013-12-16']);
$usuarios = User::select('id')->get()->toArray();
for ($j = 0; $j < count($dias); $j++) {
for ($i = 0; $i < count($usuarios); $i++) {
DiaAsignado::create([
'tddia' => $dias[$j],
'tdusuario' => '1'
]);
}
}
return view('users.edit', ['user' => $user]);
}
Note: I set a default value of "1" as user. What it does is create the dates in my table 1, but for each user that I have in my table 2 it assigns the same date, so it creates 5 times the same date for each user. The next problem is that I want to place the ID of my table 2, in this case I have 5 users, and I want to make that when I add the date also add the ID of my other table.
this is my model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class DiaAsignado extends Model
{
protected $table = "TDASIGN";
protected $primaryKey = 'id';
public $timestamps = false;
protected $fillable = [
'id', 'tddia', 'tdusuario'
];
}
Table 1 contains the fields id, tddia and user. From my table 2 I only occupy the ID. This is the result when that function is performed:
As you realize, it duplicates the dates, and what I want is for each date to assign a user, or for each user assign a date.