Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
9.3k views
in Technique[技术] by (71.8m points)

php - Join tables in laravel and show data in view

Currently I'm working on project with Laravel(Im newbie to Laravel), where I have to take data from two tables and show them at Google Marker info window. The structure of the tables is:

Table A

companyID | companyName | companyAddress | ...

Table B

productID | companyID | productPrice | ...

Controller

$table = DB::table('tableA')
            ->join('tableB', 'tableB.companyID', 'tableA.companyID')
            ->get();

                // return this to view
                
                $array['table'][] = array(
                    'compID'=>$table->companyID,
                    'price'=>$table->productPrice
                     ...

    

                );

            }

            return view('maps')->with($array);

                   

Blade.php

@foreach($table as $key)  
var html = '<h2>Company Name : ' + '{{$key['compName']}}' + '</h2><p>' + '{{$key['price']}}' + '</p>';
    
    var markerLatlng = new google.maps.LatLng({{$key['lat']}}, {{$key['long']}});
     

    var mark = new google.maps.Marker({
        map: map,
        position: markerLatlng,
        icon: '{{$key['icon']}}',
    });

    bindInfoWindow(mark, map, infoWindow, html);

@endforeach

I joined two tables but when I go to view file and use foreach to take every company info and product price, it shows company info and the last price of every company (e.g. if a company has 4 products, I will take only the last product and price as well).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It'll likely be losing the data when you are adding it to $array, try passing $table instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...