You see, I have to start a table Sector
:
Schema::create('sectors', function (Blueprint $table){
$table->increments('id');
$table->string('nombre');
$table->timestamps();
});
This table is used in a table called Experiencia
. They have no formal relationship, but the value for sector_empresa
is taken from the previous table:
Schema::create('experiencias', function (Blueprint $table) {
$table->increments('id');
$table->string('puesto');
$table->string('funcion_realizada');
$table->string('empresa');
$table->string('sector_empresa');
$table->string('mes_anyo_inicio');
$table->string('mes_anyo_fin');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
And this is related to the users of the table:
Schema::create('users', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->string('second_name')->nullable();
$table->string('provincia')->nullable();
$table->string('localidad')->nullable();
$table->string('direccion')->nullable();
$table->string('telefono');
$table->string('dni');
$table->boolean('vehiculo')->default(false);
$table->string('foto')->default('anonimo.jpg');
$table->boolean('activado')->default(false); // Se marca aqui como falso y ya se pone automaticamente así al hacer el formulario.
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
The story is that I created a function to show, for a certain sector, if a user has experience in that:
public function fichaje(Sector $sector){
$usuarios=User::all()->whereHas('experiencias',function($query){
$query->where('sector_empresa',$sector->nombre);
})->get();
if(count($usuarios)==0)
return back()->with('message',['danger','No hay ningun usuario interesado en este sector']);
else
return back()->with('message',['success','Hay usuarios interesados en este sector']);
}
But when I run the code it gives me this error:
What will I be doing wrong?
Edit: I have removed the "all ()" from the conditional, thus leaving the code:
public function fichaje(Sector $sector){
$usuarios=User::whereHas('experiencias',function($query){
$query->where('sector_empresa',$sector->nombre);
})->get();
if(count($usuarios)==0)
return back()->with('message',['danger','No hay ningun usuario interesado en este sector']);
else
return back()->with('message',['success','Hay usuarios interesados en este sector']);
}
But now I get a new error message:
It seems that what is in the function does not capture the variables from outside.