John Davidson

php - get the required data from table

0 comments
Message:


I have the table with fields id,before_value,after_value.I have the array value for parent_id ('11')


I want to get the value in below constraints



  1. before value is OIN and after value is MGL

  2. after value is MGL and before value is not OIN and parent id is in my array ('11','18');


3 before value is MGL and after value is MQL



I have tried as


$audit_table_result=$audit_table_result->orWhere(function ($query) use ($meeting_ids){
return $query->where('after_value_string','=','MGL')->whereNotIn('before_value_string',['OIB'])->whereIn('parent_id',['11','18']);
})->orWhere(function ($query) {
return $query->where('before_value_string','=','MGL')
->where('after_value_string','=','MQL');
})->orWhere(function ($query) {
return $query->where('before_value_string','=','OIB')
->where('after_value_string','=','MGL');
})->where('parent_id',$added_id)
->get();

I expected to get the id 1,3,4,5
but I'm getting 1,2,3,4,5,6



My Answer:
To achieve the desired result, you can modify your query as follows:

php
$audit_table_result = $audit_table_result->where(function ($query) use ($meeting_ids) {
$query->where('after_value_string', 'MGL')
->where('before_value_string', 'OIN')
->whereIn('parent_id', $meeting_ids);
})->orWhere(function ($query) use ($meeting_ids) {
$query->where('after_value_string', 'MGL')
->whereNotIn('before_value_string', ['OIN'])
->whereIn('parent_id', $meeting_ids);
})->orWhere(function ($query) {
$query->where('before_value_string', 'MGL')
->where('after_value_string', 'MQL');
})->get();


This query should return the expected result of ids 1, 3, 4, and 5 based on the constraints you provided.

Rate this post

4 of 5 based on 7760 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us