#Api laravel issue
37 messages · Page 1 of 1 (latest)
pleasea share your laravel code? from the last two error messages, seems like the path is not defined
https://laravel.com/docs/11.x/routing#api-routes
did you install/register the route?
Ok
Also, just to be sure, run php artisan optimize:clear. Happens a lot that people somehow cache routes, which would then cause issues as new routes wouldn't be registered. So, run that and then never run any of those caching commands locally again.
AuthController.php:
<?php
namespace App\Http\Controllers;
use App\Models\User; // Corrected model to "User"
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; // Import the Hash facade
class AuthController extends Controller
{
public function signup(Request $request)
{
// Validate incoming request data
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed', // Use the "confirmed" rule to match password_confirmation
]);
Create new user
$user = User::create([ // Use the User model
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'password' => Hash::make($validatedData['password']), // Only hash the password
]);
// Return success response
return response()->json(['message' => 'User registered successfully!'], 201);
}
public function login(Request $request)
{
// Validate incoming request data
$validatedData = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
// Find user by email
$user = User::where('email', $validatedData['email'])->first();
// Check credentials
if (!$user || !Hash::check($validatedData['password'], $user->password)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
// Return success response
return response()->json(['message' => 'Login successful'], 200);
}
}
user.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/
* The attributes that should be cast.
*
* @return array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
The problem is probably routes, not the controllers.
I will provide routes
auth.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private baseUrl = 'http://localhost:8000/api';
constructor(private http: HttpClient) {}
signup(data: any) {
return this.http.post(${this.baseUrl}/signup, data);
}
login(data: any) {
return this.http.post(${this.baseUrl}/login, data);
}
}
Now?
@astral solar What do you think?
I still see no route definitions? 🤷♂️
Also, please take note of the #rules, it's explained how to share code properly using code-snippets
Ok, where i define the routes. I cant understand
i m still beginner
@astral solar explain to me
Ah sorry i remember
<?php
use App\Http\Controllers\AuthController;
Route::post('/signup', [AuthController::class, 'signup']);
Route::post('/login', [AuthController::class, 'login']);
The routes are defined here
@astral solar
And in which file are those defined?
@astral solar
so have you done the step(s) mentioned at the link?
@pale shuttle it isnt guide ,it s showing the different methodes that you can follow
- make sure you didn't cache anything
- run
php artisan optimize:clearto clear all caches
- run
- make sure the routes are registered
- run
php artisan route:listto list out all the routes registered at laravel
- run
so, those steps are done?
Yes
After running route:liste @pale shuttle
POST api/login .................................................................... AuthController@login
POST api/signup .................................................................. AuthController@signup
But it isnt updating the data base in xampp
so the routes are accessible right now?
Yes but when i fill the form ,the data arent sent to data base why?
so which solve the original issue? clear caches or registering the api route?
No one ,they were working but they dont send data to database , i think i misunderstand the problem