从 PHP 数组获取值并显示它们

Get Values From PHP array and showing them

我正在做一个名为 FUND 的 crud,我可以使用 PHP implode 方法存储很多东西和数组。在那个数组中,我存储了一些 FUND Id。 这是数组创建代码:

<div class="custom-checkbox">
  <label for="receive[]">Select Receive Funds</label>
    @foreach($funds as $fund)
     <div class="custom-control custom-checkbox">
       <input class="custom-control-input" type="checkbox" name="receive[]" id="{{$fund->id}}" value="{{$fund->id}}">
        <label for="{{$fund->id}}"  class="custom-control-label">{{$fund->title}}</label>
     </div>
   @endforeach

这是商店代码:

public function store(Request $request)
{
    $request->validate([
        'title'=>'required',
        'image'=>'required',
        'description',
        'available'=>'required',
        'buy'=>'required',
        'account',
        'receive',
    ]);
    $image = $request->file('image');
    $new_name = rand() . '.' . $image->getClientOriginalExtension();
    $image->move(public_path('funds'), $new_name);
    $form_data = array(
        'title'=>$request->title,
        'image'=>$new_name,
        'description'=>$request->description,
        'available'=>$request->available,
        'buy'=>$request->buy,
        'buyrate'=>$request->buyrate,
        'sellrate'=>$request->sellrate,
        'account'=>$request->account,
        'receive'=>implode(',', (array)($request->receive)),
    );

    Fund::create($form_data);

    return redirect('/admin/fund');
}

我正在使用 PHP explode 方法在我的索引页上显示该数组。

$receive=[];
foreach($funds as $fund){
    $receive = explode(",",$fund->receive);
}

如前所述,我将基金 ID 存储在该数组中。所以我想通过使用我正在使用查询的基金 ID.For 来显示整行:

@foreach($receive as $r)
  <a href="/multi" class="list-group-item">
    <p>
      <img src="{{$fund=\App\Fund::where(['id'=>$r])->get('image')}}" width="32px" height="32px"> {{$fund=\App\Fund::where(['id'=>$r])->get('title')}}
        <span class="pull-right text text-muted hidden-xs hidden-sm" style="font-size:11px;">
          <small>Reserve: {{$fund=\App\Fund::where(['id'=>$r])->get('available')}}<br>Exchange rate: {{$fund=\App\Fund::where(['id'=>$r])->get('buyrate')}}</small>
        </span>
    </p>
  </a>

@endforeach

但是标题图片是这样显示的

如何只获取标题、图像和其他工作人员而无需任何括号?

尝试使用标签 而不是括号,就像我在下一个示例中所做的那样;

<img src="<?=$fund=\App\Fund::where(['id'=>$r])->get('image')?>"...

如果图像路径正确,应该可以。 如果您在此之后有任何其他问题,请告诉我:)

经过多次研究,我修复了它。答案如下:

@php $receives = \App\Fund::whereIn('id', $receive)->get(); @endphp
   @foreach($receives as $r)
       <a href="/multi" class="list-group-item">
           <p>
              <img src="{{ $r->image_url  }}" width="32px" height="32px"> {{ $r->title  }}
                <span class="pull-right text text-muted hidden-xs hidden-sm" style="font-size:11px;">
                    <small>Reserve: {{ $r->available }}<br>Exchange rate: {{ $r->buyrate }}</small>
                 </span>
            </p>
         </a>
   @endforeach