Home > Net >  Show ambiguous name in view fetched through DB Builder in Laravel
Show ambiguous name in view fetched through DB Builder in Laravel

Time:01-15

I am fetching data from three tables in which two tables (departmentdata and cities) have same field name. So when i show name field in my view then it display the value of name field at last occurrence of the result set.

//Controller source code
$leads = DB::table('leaddata')
->join('departmentdata', 'departmentdata.id', '=', 'leaddata.department')
->join('cities', 'cities.id', '=', 'leaddata.citydata')
->get();

// View source code
<?php foreach ($leads as $lead) {
?>
<span style="white-space: nowrap;">
    {{ $lead->name }}
<span>
<?php } ?>

How would i pick department name and city name separately to display in the view?

CodePudding user response:

Use aliases:

$leads = DB::table('leaddata')
->select('departmentdata.name AS department', 'cities.name AS city')
->join('departmentdata', 'departmentdata.id', '=', 'leaddata.department')
->join('cities', 'cities.id', '=', 'leaddata.citydata')
->get();

so you can:

<span style="white-space: nowrap;">
    {{$lead->department}} {{ $lead->city}} 
<span>
  •  Tags:  
  • Related