我怎样才能得到另一张桌子的价格

时间:2016-01-27 16:09:04

标签: php laravel-4

我有一张表products

Products
-id
-seller_id
-category_id
-product_name

还有Product_options表:

Product_options 
-id
-product_id
-size_name
-width
-height
-quantity
-price

我这里有一个函数show,这是一个可以在home.blade.php中查看产品的函数:

public function show($id)
{  
    $likes = DB::SELECT(DB::RAW("SELECT count(*) as like_count FROM `likes` WHERE product_id = $id"));

    $products = DB::SELECT(DB::RAW("SELECT p.id as product_id, p.name as product_name, p.price, p.description, p.image, c.id as category_id, c.name as category_name FROM products p, categories c WHERE p.category_id = c.id AND p.id = $id"));

    $product_options = DB::SELECT(DB::RAW("SELECT po.pricee FROM product_options po, products p WHERE p.id = po.product_id AND product_id = $product_id "));

    return View::make('products.show', ['products' => $products, 'like_count' => $likes[0]->like_count,'product_options'=>$product_options]);

Home.blade.php

if(isset($products) && $products) {
    foreach ($products as $product => $value) {

        $new_description = strlen($products[$product]->description) > 50 ? substr($products[$product]->description,0,50)."..." : $products[$product]->description;

        echo '
            <div class="col-sm-6 col-md-4">
                <div class="thumbnail" >
                    <div class="pull-right">';
                        if(Auth::check()) {
                            if(Auth::user()->id == $products[$product]->seller_id) {
                                echo '<a href="'.URL::to('product/editproduct', [$products[$product]->product_id]).'">
                                <span class="glyphicon glyphicon-edit"></span>
                                </a>';

                            }
                            else {
                                echo '<a href="">&nbsp;</a>';
                            }
                        }

                        echo    '</div><a href="'.URL::route('categories', [$products[$product]->category_name]).'"><h4 class="text-center"><span class="label label-info">'.$products[$product]->category_name.'</span></h4></a>

                            <div class="img-container-background">
                                <div class="img-container">

                                    <a href="'.URL::to('product/'.$products[$product]->product_id).'"> <img src="'.URL::asset('assets/images/uploads/'.$products[$product]->image).'" class="img-responsive" /></a>
                                </div>
                            </div>

                            <div class="caption">
                                <div class="row">
                                    <div class="col-md-6 col-xs-6">
                                        <h3 style="font-size:20px">'.$products[$product]->product_name.'</h3>
<span>By <a href="">'.$products[$product]->seller_name.'</a></span>

如何以product_options的价格查看我家刀片的价格?

1 个答案:

答案 0 :(得分:0)

我会在你的控制器中做这样的事情:

$products = DB::table('products')
->join('product_options', 'products.id', '=', 'product_options.product_id')
->get();

return View::make('products.show')
    ->withProducts($products);

让查询生成器为您完成所有艰苦工作,而不是写出所有查询,然后您只需在主刀片中循环遍历$ products变量即可。