数组计数 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51686090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:57:16  来源:igfitidea点击:

Array Count - Laravel

laravel

提问by zaster

enter image description here

enter image description here

How can i get a count of the number of arrays of the attached screenshot. In this scenario the number i want is 2

我如何获得所附屏幕截图的数组数量。在这种情况下,我想要的数字是2

public function update(Request $request, $jobId)
    {
        //dd($request);
        // cast into a collection use the collect() helper
        $collection = collect($request);

        for ($i=0; $i < $collection->count(); $i++) {
          //dd(count($request));
          $subJob = SubJob::Find($request->get('id')[$i]);
          $subJob->job_id = $jobId;
          $subJob->name = $request->get('name')[$i];
          $subJob->size = $request->get('size')[$i];
          $subJob->medium = $request->get('medium')[$i];
          $subJob->feature = $request->get('feature')[$i];
          $subJob->qty = $request->get('qty')[$i];
          $subJob->price = $request->get('price')[$i];
          $subJob->total = $request->get('qty')[$i] * $request->get('price')[$i];
          $subJob->save();
     }

$collection->count()gives the value 9

$collection->count()给出值9

Is there any other way of running the loop until the end? For example in blade we have $loop->lastbut here , i am working at the Controller level

有没有其他方法可以运行循环直到结束?例如在刀片中我们有$loop->last但在这里,我在控制器级别工作

<form action="{{ route('employee.subjob.update',$job->id) }}" method="post"><br>
      @csrf
      @method('PUT')
      @foreach ($subJobs as $subJob)
        <input type="hidden" name="id[]" value="{{$subJob->id}}">
        <tr>
          <td><input type="text" name="name[]" value="{{$subJob->name}}"></td>
          <td><input type="text" name="size[]" value="{{$subJob->size}}"></td>
          <td>
            <textarea name="medium[]" rows="3" cols="20">{{$subJob->medium}}</textarea>
          </td>
          <td>
            <textarea name="feature[]" rows="3" cols="20">{{$subJob->feature}}</textarea>
          </td>
          <td><input type="text" name="qty[]" value="{{$subJob->qty}}"></td>
          <td><input type="text" name="price[]" value="{{$subJob->price}}"></td>
          <td><input type="text" name="total[]" value="{{$subJob->total}}" disabled></td>
        </tr>
      @endforeach
      <button type="submit" class="btn btn-primary"> Update Job No {{$job->id}}</button>
      </form>

回答by Kenny Horna

The thing is the way how you are sending the data to your backend. As it seems, your are grouping by attribute (an array for idthen an array for nameand so on) instead than by "class" (an array that contains all the attributes id, name, ...).

问题是您将数据发送到后端的方式。因为它似乎,你是通过属性分组(一个数组id,则对于阵列name等),而不是比“类”(即包含了所有属性的数组idname...)。

So, you could solve this issue in two ways.

因此,您可以通过两种方式解决此问题。

  1. Improving the way you are collecting the data in your frontend to send it to your back (recommended)
  2. Handle the data in the back as it is right now.
  1. 改进您在前端收集数据以将其发送到后端的方式(推荐
  2. 按原样处理后面的数据。

For the first way, there are several guides/tutorials that can help you with. So, let's proceed with the second one.

对于第一种方式,有几个指南/教程可以帮助您。那么,让我们继续第二个。

public function update(Request $request, $jobId)
{
    /** This should output 2 */
    $size = count(collect($request)->get('id'));
    // Try: dd($size);

    for ($i = 0; $i < $size; $i++)
    {
        $subJob = SubJob::Find($request->get('id')[$i]);
        $subJob->job_id = $jobId;
        $subJob->name = $request->get('name')[$i];
        $subJob->size = $request->get('size')[$i];
        $subJob->medium = $request->get('medium')[$i];
        $subJob->feature = $request->get('feature')[$i];
        $subJob->qty = $request->get('qty')[$i];
        $subJob->price = $request->get('price')[$i];
        $subJob->total = $request->get('qty')[$i] * $request->get('price')[$i];
        $subJob->save();
    }
}

回答by Hassan Voyeau

Maybe try count($collection->items)

也许试试 count($collection->items)

回答by Sithira

Straight from your question "How can I get a count of the number of arrays of the attached screenshot"

直接来自您的问题“我如何计算所附屏幕截图的数组数量”

here is how you do it, according to your question

根据您的问题,这是您的操作方法

$arrayCount = 0;

foreach ($request->request as $req)
{

    // this will check for array and check the array has 2 elements
    if (is_array($req) && is_array($req) == 2)
    {
        // your logic here
    }
}

What I'm doing here that, I'm getting the request property of the request object. This allows me to loop through each property in the request object. Then I'm simply checking whether the property is an array or not. But here I only showed you how to get into the array. once you are in the array on the loop you can do whatever you want it

我在这里做什么,我正在获取请求对象的请求属性。这允许我遍历请求对象中的每个属性。然后我只是检查该属性是否为数组。但在这里我只向您展示了如何进入数组。一旦你进入循环中的数组,你就可以做任何你想做的事