Kamarul Adha
Kamarul Adha

Reputation: 153

Declare variable that can store data from the database in blade

The problem is that I am unable to declare a variable which is able to store data that is from a database. I want to display the variable in a table format. The value for the variable is also in an if-else statement. I also have tried making a new controller, however, when I called it, it returned the string, and not the value itself {{route(calcLoss), $fail->id}}

I have tried using multiple tags to declare the variable such as @php @endphp, @var. But, none of them seem to work.

<!-- The code to display the data in the blade file --> 
<tbody>
  @foreach($fails as $fail)
  <tr>
    <td>{{$fail->id}}<br>{{$fail->pro_name}}</td> <!-- Product ID & Product Name -->
    <td>{{$fail->pro_stock}}</td> <!-- Product Stock -->
    <td>{{$fail->pro_dem}}</td> <!--Product Demanded -->
    @if($fail->pro_stock > $fail->pro_dem)
    {
      <td>{{$fail->pro_dem}}</td> <!--Display product demanded-->
      <!-- Calculate loss of profit by subtracting the stock with the demand -->
    }
    @elseif($fail->pro_stock < $fail->pro_dem)
    {
      <td>{{$fail->pro_stock}}</td> <!-- Display Stock -->
      <!-- Calculate loss of profit by subtracting the demand with the stock -->
    }
    <td>{{$fail->pro_sellPrice}}</td> <!-- Product Selling Price -->
    <td>{{$fail->pro_cost}}</td><!--  Product Cost -->
    <td>{{$fail->pro_profit}}</td><!--  Profit -->
  </tr>
  @endforeach
</tbody>


<!-- The controller -->
 public function calcLoss($id)
{

    $fails = Fail::find($id);

    $loss = 0;

    $demand = $request->get('pro_dem');

    $stock = $request->get('pro_stock');

    if($demand>$stock)
        $loss = $request->get('pro_dem') - $request->get('pro_stock');



    return view('fails.displayAnalyze', ($loss));
}


<!--The route -->
Route::get('fails/calcLoss', 'FailController@calcLoss')->name('calcLoss');

The output that I have gotten is the command that was suppose to display the data, however displayed the command as a string.

Upvotes: 0

Views: 45

Answers (1)

salman faris
salman faris

Reputation: 125

Just write some simple php code on view

<?Php
 if (isset($fail->pro_dem) && isset($fail->pro_stock) 
  {
  $newVariable = $fail->pro_dem - $fail->pro_stock; 
  echo "<td>".$newVariable."</td>";
 }
 ?>

Hope it is helpful

Upvotes: 1

Related Questions