I have to insert to 'technicien'='technician' there 'tache'='task' and 'tarification' ='price'
Schema::create('tarificationtaches', function (Blueprint $table) {
$table->increments('id');
$table->float('tarif', 8,2);
$table->integer('tache_id')->unsigned();
$table->foreign('tache_id')->references('id')->on('taches');
$table->integer('technicien_id')->unsigned();
$table->foreign('technicien_id')->references('id')-
>on('techniciens');
$table->datetime('deleted_at')->nullable();
$table->timestamps();
});
each techncien have to make there 'tache' then I would like to add a condition in my function to check the insertion if the 'tache' already exists if yes it displays to me existing' tache' if it does not insert in the database
public function store(Request $request)
{
$tarification = new tarificationtache();
$tarification ->tache_id = $request->input('tache_id');
$tarification ->Tarif =$request->input('Tarif');
$tarification->technicien_id = $request->input('technicien_id');
$tarification->save();
return redirect('technicien');
}
I have tried this function but i have some error
public function store(Request $request)
{
$tarification = new tarificationtache();
$tarification ->tache_id = $request->input('tache_id');
$tarification ->Tarif =$request->input('Tarif');
$tarification->technicien_id = $request->input('technicien_id');
$tarification = DB::select("select * FROM tarificationtaches where
technicien_id = 'technicien_id' and tache_id = input('tache_id')");
if(request($tarification) > 1)
echo "Ce technicien a cette tarification";
else{
$tarification->save();
}}
errors
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION
projet2.input
does not exist (SQL: select * FROM tarificationtaches where
technicien_id = 'technicien_id' and tache_id = input('tache_id'))
Based on what I can understand, maybe you need something like this?
public function store(Request $request)
{
$count = tarificationtache::where('technicien_id', $request->input('technicien_id'))
->where('tache_id', $request->input('tache_id'))
->count();
if ($count > 0) {
// TODO: adjust response according to your need
return response()->json(['error' => "Ce technicien a cette tarification"], 400);
} else {
$tarification = new tarificationtache;
$tarification->tache_id = $request->input('tache_id');
$tarification->Tarif = $request->input('Tarif');
$tarification>technicien_id = $request->input('technicien_id');
$tarification->save();
// TODO: adjust response according to your need
return response()->json($tarification);
}
}
You can check the doc for further details regarding count
or any other query builder methods in Laravel.