jeudi 10 novembre 2016

Run mysql query inside a laravel blade file

I have a laravel blade file where there is a table and i had loaded data using my web.php file. Now i want to add a table column for vehicle brand. For that i need to access the brand table's data and i need to print the corresponding column brand in the table following are the codes.

This is my 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 is my spares.blade.php please see the sql query that had written. Other fields are 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 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>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire