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.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看