Search code examples
phplaravel

How to send form data from hours page to drafts page


I fixed it once and then I messed it up again.. and I don know how I fixed it. It has something to do with the $test variable. Iḿ programming with Laravel 5.5. I have everything installed on virtualbox, OS Ubuntu 16.04.

I was thinking that there might be a delay from the moment when I save my code to where I can see the changes on the website. And that might have been my problem to why I messed it up again.

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);

        }

    }

Solution

  • Update: I figured out where the error is. I deleted this:

    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,
                ]);
    

    and now I dont get the error undefined index 0. The code had a problem with 'article_id' => $descr[0]->id, thats where the error came from. So now the error is gone, but I don think my boss wants me to delete that section. And I still don´t get why it is giving me that error. Is the array missing?

    Update 2.0: The variable $descr was looking for an article with the ID, 1032. That article was missing in the database. So that solved the error of undefined index 0.