简体   繁体   中英

Laravel create an array in postman and save in database

I am having troubles at this situation. This is what I show in Postman 在此处输入图片说明

Controller

 public function index(Request $request)
    {
     $user =  Auth::guard('api');


        if ($user)
        {
            $parameters=new UserParameters();
            $parameters->id_user=Auth::guard('api')->id();
            $parameters->day=$request->day;

            if($parameters->save())
            {
                $times=$request->time;

                foreach ( $times as $time)
                {
                    $new_time = new TimeParameter();
                    $new_time->id_parameter = $parameters->id;
                    $new_time->time = $time[0];
                    $new_time->hour = $time[1];
                    $new_time->save();
                }

                return response()->json(['true' => false, 'message' => 'Saved!']);

            }


        }

        else
        {

            return response()->json(['error' => false, 'message' => 'User not found!']);

        }

    }

I stuck at for loop. How can I handle this situation ?

This is what i get in database 在此处输入图片说明

Problem

I don't know how your code works, because this for ( $times as $time) is not how for works.

Here is a explanation on for https://secure.php.net/manual/pt_BR/control-structures.for.php

Solution

The as on the for make me think your trying to use the foreach . And here is how it works:

foreach( $times as $time) the $time is already a single record from the array

So when you do a foreach this is how it should look:

foreach ( $times as $time){
    $new_time = new TimeParameter();
    $new_time->id_parameter = $parameters->id;
    $new_time->time = $time->time;
    $new_time->hour = $time->hour;
    $new_time->save();
}

This is because of how your key's are named. time[0].time is not valid and maybe it is reading it to time[0] only. Which is why you are only getting the hours.

time[0].time = 'morning';
time[0].hour = '10:00';

- then it will not count the .time or the .hour because that is not valid. then it would only make it time[0] which then explains why you are only getting the hours.


With the changes on the postman data that I asked you to do. You can try this solution.

public function index(Request $request)
{
 $user =  Auth::guard('api');


    if ($user)
    {
        $parameters=new UserParameters();
        $parameters->id_user=Auth::guard('api')->id();
        $parameters->day=$request->day;

        if($parameters->save())
        {
            $data= $request->all();

            foreach ( $data['times'] as $time)
            {
                $new_time = new TimeParameter();
                $new_time->id_parameter = $parameters->id;
                $new_time->time = $time[0];
                $new_time->hour = $time[1];
                $new_time->save();
            }

            return response()->json(['true' => false, 'message' => 'Saved!']);

        }


    }

    else
    {

        return response()->json(['error' => false, 'message' => 'User not found!']);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM