php - Run MySQL query inside a Laravel Blade template file -
i have laravel blade file there table , had loaded data using web.php file. want add table column vehicle brand. need access brand table's data , need print corresponding column brand in table following codes.
this 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); }); this spares.blade.php please see sql query had written. other fields working correctly.
<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 $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 brands 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>
this not practice. can join 2 table spares , brands in controller. try this.
$data = db::table('brands') ->join('spares', 'brands.id', '=', 'spares.brand_id') ->select('brands.*', 'spares.id spare_id', '',''....so on) ->get(); here brands.* brands info can manual specific attribute brands tabl spares. can every info of brands , spares in $data array if brands , spares has relationship
Comments
Post a Comment