【问题标题】:Join not working unless hard code id number除非硬编码 ID 号,否则加入无效
【发布时间】:2022-01-02 05:12:51
【问题描述】:

如果我使用硬编码的 id,这可行,但我想做的是选择 profile.id,其中 profile.user_id 等于 users.id。当我尝试这样做时,它说无效整数,如果我 dd($profile_id) 我得到一个空数组。

$profile_id = DB::table('profile')
        ->join('users', 'profile.user_id', '=', 'users.id') 
        ->select('profile.id')
        ->where('profile.user_id',9)
        ->get();

我将代码更改为以下并收到此错误:

General error: 1366 Incorrect integer value: '[{"id":12}]' for column 'profile_id' 

这是我对应用程序表的迁移

Schema::create('pilgrim_application', function (Blueprint $table) {
            $table->id();
                       
            $table->integer('user_id')->constrained('users')->onDelete('cascade');
            $table->integer('profile_id')->constrained('profile')->onDelete('cascade');
            $table->string('nickname');
            $table->string('marital_status');
            $table->string('special_needs');
            $table->text('why_attend');
            $table->string('pcontact');
            $table->string('phome');
            $table->string('pcell');
            $table->string('acontact');
            $table->string('ahome');
            $table->string('acell');
            $table->date('date_signed');
            $table->timestamps();
        });

这是我在 store 方法中将代码更改为的内容

$user_id = Auth::user()->id;
        $profile_id = DB::table('profile')
        ->select('profile.id')
        ->where('profile.user_id',$user_id)
        ->get();

        $this->validate($request, [
            'nickname' => 'max:255|min:4',
            'why_attend' => 'required',
            'pcontact' => 'required|min:2|max:255',
            'phome' => 'required|min:4|max:255',
            'pcell' => 'required|min:4|max:255',
            'acontact' => 'required|min:4|max:255',
            'ahome' => 'required|min:4|max:255',
            'acell' => 'required|min:4|max:255',
            'date_signed' => 'required|date',
          ]);

          $pilgrimapp = new PilgrimApp;
          $pilgrimapp->user_id = $user_id;
          $pilgrimapp->profile_id = $profile_id;
          $pilgrimapp->nickname = $request->nickname;
          $pilgrimapp->marital_status = $request->marital_status;

        //   $sn_checkbox = implode(",", $request->get('special_needs'));
          $pilgrimapp->special_needs = $request->special_needs;
          $pilgrimapp->why_attend = $request->why_attend;
          $pilgrimapp->pcontact = $request->pcontact;
          $pilgrimapp->phome = $request->phome;
          $pilgrimapp->pcell = $request->pcell;
          $pilgrimapp->acontact = $request->acontact;
          $pilgrimapp->ahome = $request->ahome;
          $pilgrimapp->acell = $request->acell;
          $pilgrimapp->date_signed = $request->date_signed;
          $pilgrimapp->save();

【问题讨论】:

    标签: laravel join


    【解决方案1】:

    要比较列,而不是列和值,您应该使用 whereColumn:

    whereColumn 方法可用于验证两列是否相等

    $profile_id = DB::table('profile')
            ->join('users', 'profile.user_id', '=', 'users.id') 
            ->select('profile.id')
            ->whereColumn('profile.user_id','users.id')
            ->get();
    

    无论如何,这里 无用,因为这个条件已经在 join 子句中设置了。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2021-06-04
      • 2021-05-19
      • 2019-10-15
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多