简体   繁体   中英

How can i get name from id table

I have a problem, I need to show my data if the status(String) is 1 in my table PO

This is my PO table

id | book_name            | stock |status
1  | lala land            | 10    | 0
2  | beauty and the beast | 25    | 1
3  | wawasan              | 15    | 1

This that i have tried

..prev code
@if($po->status == '1')
@foreach($po as $p)
  <tr>
     <td> {{$p->book_name) </td>
     <td> {{$p->stok}} </td>
  </tr>
@endforeach
@endif    




The @if should be inside the @foreach

@foreach($po as $p)
  @if($p->status == '1')
    <tr>
       <td> {{$p->book_name}} </td>
       <td> {{$p->stok}} </td>
    </tr>
  @endif
@endforeach

Alternatively, you could loop over the filtered collection.

@foreach($po->where('status', 1) as $p)
  <tr>
     <td> {{$p->book_name}} </td>
     <td> {{$p->stok}} </td>
  </tr>
@endforeach

I think if moves well with this

@foreach($po as $p)
  @if($p->status == '1')
    <tr>
       <td>{{ $p->book_name }}</td>
       <td>{{ $p->stok }}</td>
    </tr>
  @endif    
@endforeach

for example your model name is PO,

pass this from controller function use App\PO;

 $data= PO::where('status',1)->get();
 return view('bladefilename',compact('data');

and then in your blade file

 @foreach($data as $datas)
  <tr>
     <td>{{ $datas->book_name }}</td>
     <td>{{ $datas->stok }}</td>
  </tr>
 @endforeach

Or perhaps get the po that status is only true to avoid having a logic in your blade.

$po = PO::whereStatus(true)->get() or PO::where('status',true)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM