Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.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 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...