I have two tables 'travel.request' table and 'traveling_details' table.
travel_request table
id name
1 aa
2 bb
traveling_details table
id travel_request_id(FK from travel_request) country
1 1 UK
2 1 CL
3 2 Australia
4 2 India
I want to list only one records for travel_request_id 1 record for travel_request_id=1 and travel_request_id=2 along with travel_request table details and others
$request = DB::table('travel_request')
->join('department', 'travel_request.department_id', '=', 'department.id')
->join('users', 'travel_request.approver_id', '=', 'users.id')
->join('travel_purpose', 'travel_request.travel_purpose_id', '=', 'travel_purpose.id')
->join('traveling_details', 'travel_request.id','=','traveling_details.travel_request_id' )
->join('country','country.id', '=', 'traveling_details.country_id')
->join('travel_request_status','travel_request.status_id', '=', 'travel_request_status.id')
->select('travel_request.*', 'travel_request.id as travel_id','department.name as dept_name','users.firstname as approver_name','travel_purpose.purpose','country.name as country_name','traveling_details.from_date','traveling_details.to_date','travel_request_status.status')->where('travel_request.requestor_id',$user_id)->paginate(2);
this query displays all records in traveling_details table
You might've using any kind fo ORM for your DB transaction. Try to use LIMIT 1
in that query.
SELECT * FROM `travel_request`
join `travel_details` ON `travel_details`.`request_id` = `travel_request`.`request_id`
group by `travel_request`.`request_id`
I've created a sample database and gone through the same way. You've to understand that the inner join will select all rows from the tables when the 'ON' condition matches. If you want only one row from travel_details, you can use GROUP BY
for travel_request.id. This will select only the first row of the matching condition.