Skip to content

Instantly share code, notes, and snippets.

@VirajMadhu
Last active June 12, 2022 14:43
Show Gist options
  • Save VirajMadhu/74621cf3a3037803f8263780a4918f33 to your computer and use it in GitHub Desktop.
Save VirajMadhu/74621cf3a3037803f8263780a4918f33 to your computer and use it in GitHub Desktop.
Axios POST method in Vue JS with Laravel Back-end
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
//create new user view
public function create()
{
return view('tailor.create');
}
//Store the data
public function store(Request $request)
{
//Make validation using Validator supporter
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => ['required', 'email'],
]);
//Check validation fails or not
if ($validator->fails()) {
return response(['error' => $validator->errors()], 401);
} else {
$this->user->store([
'name' => $request->name,
'email' => $request->email,
]);
return response(['message' => 'New User Added Successfully!'], 200)->header('Content-Type', 'text/plain');
}
}
}
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/user/create', [UserController::class, 'create'])->name('user.create');
Route::post('/user/store', [UserController::class, 'store'])->name('user.store');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment