Back

Create user from external web application


Daniel posted 3 months ago

Hello, everybody,

I'm Daniel, web developer with php experience about 8 years, but I'm new to Laravel. I'm researching how to create a user, from an external web application. Can you help me, what would be the most correct way? I see it is created by the store() function of the app/Http/Controllers/Admin/UserController.php file.

My idea is to create a custom controller, which receives "requests" via url, validates, and creates the user with the data received in the request.

Greetings and thanks,

TorchSK replied 3 months ago

First of all I would create the route in routes/api.php file to have the api namespace and other api (optional) authentications available (check official docs)

https://laravel.com/docs/5.8/routing
https://laravel.com/docs/5.8/api-authentication

To validate the data you can simply create form request validation on store() function

https://laravel.com/docs/5.8/validation#form-request-validation

To store the data in the DB you can create fillable attribute on User model with array of fields that can be populated by mass assignment and just do User::create($request->all())

https://laravel.com/docs/5.8/eloquent#mass-assignment

You don't have to create custom controller (but you can if you want of course)

Laravel is just... beautiful! :)

Cameron replied 3 months ago

I would add some sort of security to your endpoint or anyone could post to create a new user

Make an api post route , example api/create-user

Point it to a controller method Example Public function create(Request $request){

$user = new User; $user->myfield_1 = “my data where “; $user->save();

}

Post your data from your external app to your new endpoint in laravel


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us