如何将表格数据从小时页面发送到草稿页面

时间:2018-02-13 15:30:13

标签: laravel

我修了一次,然后又把它搞砸了......我不知道怎么修好它。 它与$ test变量有关。我使用Laravel 5.5进行编程。我已经在virtualbox,OS Ubuntu 16.04上安装了所有东西。

我原以为从我保存代码到可以看到网站上的更改的那一刻可能会有延迟。这可能是我的问题,为什么我再次搞砸了它。

public function change(Hour $hour, Request $request) {
    //$test = 0;
    //dd($request->checkarray);
    if($request->has('checkarray')){

        $hourList = $request->checkarray;
        $hours = hour::whereIn('id', $hourList)->get()->toArray();

        $end_hours = 0;
        $end_minutes = 0;

        //dd(strtotime($start_time));
        foreach ($hours as $hour){
            $dt = Carbon::parse(($hour['hours']));
            $temporary_hours = $dt->hour;
            $temporary_minutes = $dt->minute;
            $end_hours += $temporary_hours;
            $end_minutes += $temporary_minutes; 
        }

        while($end_minutes >= 60){
            $end_minutes = $end_minutes - 60;
            $end_hours = $end_hours + 1; 
        }
        $end_result4 = $end_minutes * 100;
        $end_result3 = $end_result4 /= 60;
        $end_result2 = $end_result3 /= 100;
        $end_result1 = round($end_result2, 2);
        $end_result = $end_result1 + $end_hours;
        //dd($end_result);

        $count = 0;
        $customer_id = 0;

        foreach($hours as $hour){

            if($count == 0){
                $customer_id = $hour['customer_id'];
                //dd($customer_id);
                $count++;
                continue;
            }
            if($customer_id != $hour['customer_id']){
                $value = Cache::get('key', 'default');

                return view('admin-hours.show', [
                    'hours' => $value,
                ]);
            }
            else{
            //dd('laskdfjadsl;kjf;lasdkjf');


            $customer = Customer::where('id', $customer_id)->get();
            $pass = array_merge($request->all(), $customer->toArray());
            $descr = DB::table('articles')->where('id', '1032')->get();

                $test=DraftHeader::create([
                    'customer_id' => $pass[0]['id'],
                    'name' => $pass[0]['name'],
                    'name2' => $pass[0]['name2'],
                    'address' => $pass[0]['address'],
                    'postcode' => $pass[0]['postcode'],
                    'town' => $pass[0]['town'],
                    'country' => $pass[0]['country'],
                    'reference' => null,
                ]);

                DraftLine::create([
                    'draft_header_id' => $test->id,
                    'article_id' => $descr[0]->id,
                    'descr' => $descr[0]->descr,
                    'qty' => $end_result,
                    'grossamount' => 0,
                    'discount' => 0,
                    'netamount' => 0,
                    'taxrate' => $descr[0]->taxrate,
                    'addition' => 0,
                ]);

                return redirect ('drafts/' . $test->id . '/edit');

            }
            //dd($hour['customer_id']);
            //dd($customer_id);

        }

    }

2 个答案:

答案 0 :(得分:0)

您试图比$ pass数组所需的深度更深一级。它应该是关联的,所以只需使用键来调用适当的值。

只需更改$ test实例:

$test=DraftHeader::create([
    'customer_id' => $pass['id'],
    'name' => $pass['name'],
    'name2' => $pass['name2'],
    'address' => $pass['address'],
    'postcode' => $pass['postcode'],
    'town' => $pass['town'],
    'country' => $pass['country'],
    'reference' => null,
]);

答案 1 :(得分:0)

<强>更新 我想出了错误的位置。我删除了这个:

DraftLine::create([
                'draft_header_id' => $test->id,
                'article_id' => $descr[0]->id,
                'descr' => $descr[0]->descr,
                'qty' => $end_result,
                'grossamount' => 0,
                'discount' => 0,
                'netamount' => 0,
                'taxrate' => $descr[0]->taxrate,
                'addition' => 0,
            ]);

现在我没有得到错误未定义索引0.代码有'article_id'=&gt;的问题$ descr [0] - &gt; id,这就是错误来自的地方。所以现在错误消失了,但我不认为我的老板要我删除该部分。我仍然不明白为什么它会给我这个错误。阵列丢失了吗?

更新2.0: 变量$ descr正在寻找一个ID为1032的文章。该文章在数据库中缺失。这样就解决了未定义索引0的错误。