0

Currently I have set of array.

and

I can easily insert these data to my database using Laravel without doing any validations

here's the sample array

CODE:

        $excel1 = Importer::make('Excel');
        $excel1->hasHeader(true);
        $excel1->load($savePath.$fileName);
        $excel1->setSheet(2);
        $collection1 = $excel1->getCollection();
        $arr1 = json_decode($collection1,true);

    foreach ($arr1 as $row1) {
                $insert_data1[] = array(
                    'projCode'  =>  $projCode,
                    'emp_id'  =>  $row1['company_id'],
                    'type'  =>  'EMP',
                    'deleted'  =>  0,
                    'by_id'  => auth()->user()->id,
                    'updated_by'    =>  auth()->user()->name,
                    'created_at'    =>  now(),
                    'updated_at'    => now(),
                );
            }
  dd($insert_data1);

OUTPUT:

enter image description here

and I'm using this code to insert these data to my table

    DB::table('tbl_emp_proj')->insert($insert_data1);

and this works fine but the problem is,

I'm trying to validate if emp_id exists or not in my users table

Here's my users table

enter image description here

The value of emp_id from array should check if it already exists in my users using company_id field from users. How can I validate it if $insert_data1 is an array and should be check if it exists on database?

UPDATE

currently i have this validator and I tried to add up the $Insert_data1 but gives me undefined var for $insert_data1.

      $validator = Validator::make(
        [
            'file'      => $request->file,
            'extension' => strtolower($request->file->getClientOriginalExtension()),
        ],
        [
            'file'          => 'required|max:5000',
            'extension'      => 'required|in:,csv,xlsx,xls',
        ],
        $insert_data1,
        [
            '*.emp_id' => "required|exists:users,company_id",
        ]
    );
Pablo
  • 1,357
  • 1
  • 11
  • 40

1 Answers1

1

You can use Laravel Validator to validate any arrays as if its a request input.

use Illuminate\Support\Facades\Validator;

$validator = Validator::make(
    $insert_data1,
    [
        '*.emp_id' => "required|integer|exists:users,company_id",
    ]
);

EDIT:

You can receive error messages and error items with the validator APIs.

$failed = $validator->fails(); //boolean

$errors = $validator->errors();

$validated = $validator->validated();

$invalid = $validator->invalid();
Pusparaj
  • 1,469
  • 1
  • 12
  • 19
  • Can I make `integer` to something like string? because the `company_id/emp_id` contains symbols – Pablo Jan 09 '20 at 05:56
  • yes you can. its all as per your need and setup. what I mentioned there is just an example – Pusparaj Jan 09 '20 at 05:57
  • I have undefined var `$insert_data1` I just add up your answer to my current `validator` – Pablo Jan 09 '20 at 06:06
  • put the validator check after the completion of foreach loop preparing the `$insert_data1`. else, verify the variable name is not having any type there. – Pusparaj Jan 09 '20 at 06:09
  • as of now Im using `$validator->fails();` seems like `insert_data1` in my `validator1` doesn't recognize the var `$insert_data1` from my foreach – Pablo Jan 09 '20 at 06:09
  • define the variable prior to foreach start `$insert_data1 = [];` followed by your foreach. – Pusparaj Jan 09 '20 at 06:11
  • This one workssss, I tried to validate and gives me `The selected 1.emp_id is invalid.` it works fine. Can I rename the error message? I'm usign this code `if($validator->fails()){ return redirect() ->back() ->with(['errors'=>$validator->errors()->all()]) ->with('modal',$modal); }` – Pablo Jan 09 '20 at 06:23