Ad
Laravel : Codition To Insert Tarificationtache (Syntax Error Or Access Violation)
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 \n
technicien_id = 'technicien_id' and tache_id = input('tache_id'))
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad