I need to get all Services that are not associated with the requests which are associated with a user .
So basically the models are
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'roles'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token','profesion_id'
];
public function solicitudes(){
return $this->hasMany(SolicitudServicio::class);
}
}
The service request model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SolicitudServicio extends Model
{
protected $table ="solicitudes";
protected $fillable = ['url', 'servicio', 'servicio_id', 'nequipos', 'nequipos_asignados','username','password', 'ndias'];
//Metodo del operador
public function users()
{
return $this->belongsToMany(User::class);
}
public function servicio(){
return $this->belongsTo(Servicio::class);
}
public function estado(){
return $this->belongsTo(Estado::class); //es estado de solicitud
}
public function tipo(){
return $this->belongsTo(Tipo::class); //Es tipo de solicitud
}
public function user(){
return $this->belongsTo(User::class);
}
}
The service model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Servicio extends Model
{
protected $table = 'servicios';
protected $fillable = ['name', 'description'];
public function solicitudesDeServicio(){
return $this->hasMany(SolicitudServicio::class);
}
public function funciones(){
return $this->hasMany(FuncioneServicio::class);
}
}
Then as I said, I need the services that are not associated with the services which are associated with a user.
I usually have this query using Relationships which only returns the services that are associated with the services of a user that in this case is the user with id 1.
So what I need is the opposite, I have reviewed the documentation of laravel and there is an interesting function whereNotIn but I have not managed to make it work in the query
$solicitudes = App\Servicio::whereHas('solicitudesDeServicio',function($query) {
$query->whereHas('user',function($query2) {
$query2->where('id','1');
});
})->get();