mardi 18 juin 2019

I want to store data from an array and need to store those entities id in another array

I am trying to store data from an array like $locations = "khulna, dhaka" in Laravel. But location shouldn't be duplicate in locations table. Later I need the the ID's of those locations in another array, so that I can attach them with an user.

public function addUser(Request $request)
    {
        $locations = $request->location;

        // need to store in table without duplicating
        // need the IDs of the locations in another array

        if($request->type == 'Admin')
        {
            $role = '2';
        } elseif($request->type == 'Publisher') {
            $role = '5';
        } elseif($request->type == 'Editor') {
            $role = '6';
        } else {
            $role = '10';
        }

        if($request->password == $request->password_confirmation)
        {
            $user = User::create([
                'name'              => $request->name,
                'email'             => $request->email,
                'contact_number'    => $request->contact_number,
                'type'              => $request->type,
                'role'              => $role,
                'password'          => Hash::make($request->password),
            ]);

            // Because I need to attach those location with user
            $user->locations()->attach($locations);
        } else {
            return back();
        }

        return redirect('users');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire