Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel刀片模板文件中运行MySQL查询_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 在Laravel刀片模板文件中运行MySQL查询

Php 在Laravel刀片模板文件中运行MySQL查询,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个LaravelBlade文件,其中有一个表,我使用web.php文件加载了数据。现在我想为车辆品牌添加一个表列。为此,我需要访问brand表的数据,并需要在下表中打印相应的brand列代码 这是我的web.php Route::get('/retailer/spares', function () { $models = DB::table('models')->get(); $brands = DB::table('brands')->get();

我有一个LaravelBlade文件,其中有一个表,我使用
web.php
文件加载了数据。现在我想为车辆品牌添加一个表列。为此,我需要访问brand表的数据,并需要在下表中打印相应的brand列代码

这是我的
web.php

 Route::get('/retailer/spares', function () {
    $models = DB::table('models')->get();
    $brands = DB::table('brands')->get();
    $spares = DB::table('spares')->get();

    return View::make('Retailer/spares')->with('models', $models)->with('brands', $brands)->with('spares', $spares);

});
这是我的
spares.blade.php
请查看编写的SQL查询。其他字段工作正常

<table  class="table ">

<tr>
    <th>Spare Id</th>
    <th>Part Number</th>
    <th>Name</th>
    <th>Brand</th>

    <th>Quantity</th>
    <th>Price</th>
    <th>Warranty</th>
    <th>Spare Image</th>

    <th>


        <input   type="text" class="form-control" id="search" placeholder="Search Spare">
    </th>

</tr>
<tbody id="tableModel">
<?php
foreach($spares as $spare){

?>
<tr>
    <td ><?php echo $spare->id;?></td>
    <td ><?php echo $spare->partNumber;?></td>
    <td ><?php echo $spare->description;?></td>

    <td>
        <?php

        $brand="SELECT brandName FROM brands WHERE id=' $spare->brand_id'";

        ?>
    </td>

    <td ><?php echo $spare->quantity;?></td>
    <td ><?php echo 'Rs.'. $spare->price;?></td>
    <td ><?php echo $spare->warranty;?></td>


    <td><div class="image"><?php echo ' <img  class="img-responsive"  src="data:image/jpeg;base64,'.base64_encode( $spare->image ).'"/>';?></div></td>
    <td>

        <a class=" btn btn-success btn-sm" data-toggle="modal" data-target="#modalEdit" onclick="EditBrand('<?php echo $brand->brandName;?>','<?php echo $brand->id;?>')" >Edit </a>

        <a onclick="DeleteBrand(<?php echo $brand->id;?>)" style="" class=" btn btn-danger btn-sm"  >Delete </a>

    </td>

</tr>

<?php }?>
</tbody>

备用Id
零件号
名称
品牌
量
价格
担保
备用映像
)“style=”“class=“btn btn危险btn sm”>删除

这不是一个好做法。您可以在控制器中加入两个表备件和品牌。试试这个

$data = DB::table('brands')
            ->join('spares', 'brands.id', '=', 'spares.brand_id')
            ->select('brands.*', 'spares.id as spare_id', '',''....so on)
            ->get();

此处为品牌。*获取所有品牌信息您可以手动从品牌表以及备件中获取特定属性。如果品牌和备件之间存在关系,您可以在$data数组中获取品牌和备件的所有信息

避免这种方法,不建议这样做,因为这样做您没有遵循MVC模式……为什么需要使用这种方法。这不是MVC模式好的,你能帮我吗?你创建品牌模型吗?是的,我已经添加了。