duanchensou8685 2018-08-17 08:33
浏览 48
已采纳

Laravel:插入tarificationtache的条件(语法错误或访问冲突)

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'))

form to inset tache and tarif

  • 写回答

1条回答 默认 最新

  • dongzouh51192 2018-08-17 09:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?