Laravel - 循环认为$ request和插入数据

时间:2017-02-17 12:01:49

标签: jquery laravel foreach insert clone

我有一个表单来提交作业,在表单中有一个部分,您可以添加额外信息以及克隆字段的功能。

我在将记录保存到我的任务表时遇到问题,该表链接到带有ID的作业表。

当用户克隆字段时,我无法循环数据并将其保存在数据库中。

我正在削减所有验证以节省空间。这是我在JobController中的store方法:

           public function store(Request $request)
                {
                    /*JOB*/
                    $job = new Job();
                    $job->title=$request->title;
                    $job->client_id=$request->client_id;
                    $job->user_id=$request->user_id;
                    $job-> number =$request->number;
                    $job->ponumber =$request->ponumber;
                    $job->status =$request->status;
                    $job->invoicestatus =$request->invoicestatus;
                    $job->save();
                    $lastInsertedId = $job->id;
                    /*TASK*/
                    $task = new Task();
                    $task->job_id = $lastInsertedId;
                    $task->description = $request->description;
                    $task->supplier_hs= $request->supplier_hs;
                    $task->quote_hs= $request->quote_hs;
                    $task->quote_hs_date= $request->quote_hs_date;
                    $task->quote_client= $request->quote_client;
                    $task->quote_client_date=   $request->quote_client_date;
                    $task->readytoinvoice= $request->readytoinvoice;
                    $task->invoiced= $request->invoiced;
                    $result = $task->getAttributes();
                    foreach($result as$key=>$t){
                        $i = 0;
                        foreach($result[$key] as $value){
                            echo"<pre>";
                            print_r($value);
                            echo"</pre>";
                            $i++;
                            echo "counter: " .$i;
                        }

                    }
                    //$task->save();


                    //return redirect('jobs')->with(array('feedback'=>'New Job created successfully','last_insert_id' => $job));


                }

这是我从$ result获得的输出:

       Array
(
[description] => Array
    (
        [0] => job 1
        [1] => Job 2
        [2] => job 3
    )

[supplier_hs] => Array
    (
        [0] => 1000
        [1] => 200
        [2] => 3000
    )

[quote_hs] => Array
    (
        [0] => 1000
        [1] => 2000
        [2] => 300
    )

[quote_hs_date] => Array
    (
        [0] => 2017-02-15 11:50:07
        [1] => 2017-02-01 00:00:00
        [2] => 2017-02-15 11:50:07
    )

[quote_client] => Array
    (
        [0] => 1000
        [1] => 2000
        [2] => 3000
    )

[quote_client_date] => Array
    (
        [0] => 2017-02-15 11:50:07
        [1] => 2017-02-01 00:00:00
        [2] => 2017-02-15 11:50:07
    )

[readytoinvoice] => Array
    (
        [0] => 1
        [1] => 1
        [2] => 1
    )

[invoiced] => Array
    (
        [0] => 1
        [1] => 1
        [2] => 1
    )

)

这就是我想得到的结果:

        Array
        (
            [0] => Array
                (
                    [description] => job 1
                    [supplier_hs]=> Job 2
                    [quote_hs] => job 3
                    [quote_hs_date] => 1000
                    [quote_client] => 2000
                    [quote_client_date] => 2017-02-15 11:50:07
                    [readytoinvoice] => 1
                    [invoiced] => 1
                ),
            [1] => Array
            (
                [description] => job 1
                [supplier_hs]=> Job 2
                [quote_hs] => job 3
                [quote_hs_date] => 1000
                [quote_client] => 2000
                [quote_client_date] => 2017-02-15 11:50:07
                [readytoinvoice] => 1
                [invoiced] => 1
            ),
            [2] => Array
            (
                [description] => job 1
                [supplier_hs]=> Job 2
                [quote_hs] => job 3
                [quote_hs_date] => 1000
                [quote_client] => 2000
                [quote_client_date] => 2017-02-15 11:50:07
                [readytoinvoice] => 1
                [invoiced] => 1
            )
        )

我用这种方式工作了。我不确定这是实现这一目标的最佳方式,但这是我的代码:

$tasks['tasks'] = array() ;
    foreach($result as $key=>$value){
        $i = 0;
        if(!is_null($key)){
            foreach($result as $value){
                if(!is_null($value)){
                    $index = array_keys($value);
                    foreach($index as $ind){
                        if($ind == $i){
                            $tasks['tasks'][$i]['job_id'] = $lastInsertedId;
                            $tasks['tasks'][$i][$key] = $result[$key][$i];
                        }
                    }
                }
                $i++;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果我理解$ result最初是一个数组数组(每个'标题',如quote_client等),那么使用它可能会有所帮助:

$finally = [];
$width = 0;
$keys = array_keys($result);
foreach ($keys as $kkk){
    foreach($result as $res_key => $res_value){                                                                                                                                                                                                      
        foreach($res_value as $key => $value)                                                                                                                                                                                       
        {                                                                                                                                                                                                                           
            $finally[$width][$res_key] = $value;                                                                                                                                                                                                        
        }                                                                                                                                                                                                                           
    }
    $width ++;
}
  

PS:当然这个方法有一些嵌套循环,所以不保证效率。但是我的想法是laravel的集合中可能有一个方法可能会有所帮助。

希望有所帮助:)