save value of an array

Published 6 months ago by copain

how do i save the value of array into my controller?

result of array using DD

  dd($request->all());

array:4 [▼
  "_token" => "kjYvkKfuOkiBacn1EC0EzfeK5AitRYbXADE2E5RI"
  "student_name" => array:3 [▼
    0 => "ROBERT"
    1 => "cindy"
    2 => "rico"
  ]
  "age" => array:3 [▼
    0 => "12"
    1 => "13"
    2 => "14"
  ]
  "gender" => array:3 [▼
    0 => "MALE"
    1 => "female"
    2 => "MALE"
  ]
]

CONTROLLER:

public function store(Request $request)
    {
     dd($request->all());
    
        $this->validate($request, [
         
          'student_name'=>'required|max:50',
            
            
                ]);
               
           
                
                        $leads=new Lead();
                        $leads->student_name = $request->student_name;
                       $leads->gender = $request->gender;
                       $leads->age = $request->age;
                       $leads->save();
                    
                
                

                //Display a successful message upon save
                return redirect()->route('leads.index')
            ->with('flash_message', 'Success.');
    }

HERE'S my VIEW

 <form action="{{route('leads.store')}}" method="POST">
    {{csrf_field()}}
    
    <div class="col-sm-12">
    
    
    <div class="container">
    <div class="row">
        <div class="col-md-12">
            <div data-role="dynamic-fields">
                <div class="form-inline">
                    <div class="form-group">
                        <label class="sr-only" for="field-name">Field Name</label>
                        <input type="text" class="form-control" id="field-name"  name="student_name[]" placeholder="Field Name">
                    </div>
                    <span>-</span>
                    <div class="form-group">
                        <label class="sr-only" for="field-value">Field Value</label>
                        <input type="text" class="form-control" id="field-value"  name="age[]" placeholder="Field Value">
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="field-agent">Field Agent</label>
                        <input type="text" class="form-control" id="field-agent"  name="gender[]" placeholder="Field Agent">
                    </div>
                    <button class="btn btn-danger" data-role="remove">
                        <span class="glyphicon glyphicon-remove"></span>
                    </button>
                    <button class="btn btn-primary" data-role="add">
                        <span class="glyphicon glyphicon-plus"></span>
                    </button>
                </div>  <!-- /div.form-inline -->
            </div>  <!-- /div[data-role="dynamic-fields"] -->
        </div>  <!-- /div.col-md-12 -->
    </div>  <!-- /div.row -->
</div>
          
<div>
<button type="submit" class="btn btn-success">SUBMIT</button>
<a href="{{route('leads.index')}}" class="btn btn-success">CANCEL</a>
</div>
 </form>


<!--end of container-->
 </div>
 </div>

Best Answer (As Selected By copain)
zymawy

Firstly You Can Validate The Array


$this->validate($request, [
         'student_name.*'=>'required|max:50',
         'age.*'=>'required|integer',
    //Roles Goes Here 
 ]);

Than If You Can Use Php Way To Insert

    $input = $request->all();

        if (count($input['student_name']) > 0) {
            for ($i = 0 ; $i < count($input['student_name'])  ; $i++) {
                   $leads = new Lead();

                  $leads->student_name = $input['student_name'][$i];
                  $leads->gender =  $input['gender'][$i];
                  $leads->age = $input['age'][$i];
                  $leads->save();
            }
        }
Dry7
Dry7
6 months ago (152,030 XP)

@copain use cast https://laravel.com/docs/5.5/eloquent-mutators#array-and-json-casting

class Lead extends Model {
    protected $casts = [
        'student_name' => 'array',
        'age' => 'array',
        'gender' => 'array'
    ];
}
copain

@Dry7 so i don't need to touch my controller? getting errors when i use ur suggested code.

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\wamp64\www\GraceProject\ROLEwithPERMISSION\resources\views\leads\index.blade.php)
Dry7
Dry7
6 months ago (152,030 XP)

@copain delete

$this->validate($request, [
         
          'student_name'=>'required|max:50',
            
            
                ]);
copain

@Dry7 if i delete validation how do i required the field

zymawy

Firstly You Can Validate The Array


$this->validate($request, [
         'student_name.*'=>'required|max:50',
         'age.*'=>'required|integer',
    //Roles Goes Here 
 ]);

Than If You Can Use Php Way To Insert

    $input = $request->all();

        if (count($input['student_name']) > 0) {
            for ($i = 0 ; $i < count($input['student_name'])  ; $i++) {
                   $leads = new Lead();

                  $leads->student_name = $input['student_name'][$i];
                  $leads->gender =  $input['gender'][$i];
                  $leads->age = $input['age'][$i];
                  $leads->save();
            }
        }
copain

@zymawy thanks bro, it works .

Please sign in or create an account to participate in this conversation.