I've been trying to query whereHas and I can't retrieve the data. Look how I tried. I made a whereHas where I have a query function after I put another where how to take the data. Keep in mind that where refers to bias and not discount_codes
public function showPopupData(Request $request)
{
$id = Auth::user()->company_id;
$data = Popup::getPopupByCompanyId($id);
$biases = Bias::whereHas('discount_codes', function ($query) {
$query->where('discount_code.id', '=', 'bias.discount_code_id');
})->get();
return response()->json(['data' => $data,
'biases' => $biases
]);
}
It should be maintained that, with the next relationship, I also receive the data for which I have a relationship.
$biases = Bias::with('discount_codes')->where('company_id', '=', $id)->get();
Models
public function discount_codes()
{
return $this->belongsTo('App\Models\DiscountCode', 'discount_code_id', 'id');
}
public function biases()
{
return $this->hasMany('App\Models\Bias', 'id', 'discount_code_id');
}
Tables
Discount code
$table->increments('id');
$table->string('discount_code')->nullable();
Bias
$table->integer('discount_code_id')->nullable()->unsigned();
$table->foreign('discount_code_id')->references('id')->on('discount_codes')->onDelete('cascade');
What do you think I'm doing wrong here?