Open a terminal and navigate to the directory where you want to create your Laravel project. Run the following command:
composer create-project --prefer-dist laravel/laravel api-project
Replace api-project with your desired project name.
Open PHPMyAdmin and create a new database for your project.
Rename the .env.example file in your project root directory to .env. Update the database connection settings in the .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Run the following command to generate an application key:
php artisan key:generate
Run the following command to create a user model and migration:
php artisan make:model User -m
Open the migration file created for the user table and define the schema:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Run the following command to migrate the user table to your database:
php artisan migrate
Open the routes/api.php file and define the API routes for CRUD operations:
use App\Http\Controllers\UserController;
Route::resource('users', UserController::class);
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Run the following command to create a UserController:
php artisan make:controller UserController
Open the UserController and implement the CRUD operations:
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
return User::all();
}
public function store(Request $request)
{
return User::create($request->all());
}
public function show(User $user)
{
return $user;
}
public function update(Request $request, User $user)
{
$user->update($request->all());
return $user;
}
public function destroy(User $user)
{
$user->delete();
return response()->json(['message' => 'User deleted']);
}
}
Run the following command:
php artisan serve
Use a tool like Postman to test your API endpoints (GET /users, POST /users, PUT /users/{id}, DELETE /users/{id}).
http://127.0.0.1:8000/api/users.http://127.0.0.1:8000/api/users. Add a JSON body with user data.http://127.0.0.1:8000/api/users/{id}. Add a JSON body with updated user data.http://127.0.0.1:8000/api/users/{id}.This error occurs because the default string length in Laravel for creating unique indexes is longer than the maximum allowed key length in your MySQL database. To fix this issue:
App\Providers\AppServiceProvider.php and add the following code to the boot method:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
This should resolve the issue, and your migration should run without errors.
The error message indicates that the users table already exists in your database, so Laravel is trying to create it again when you run the migration. To resolve this issue, you have a few options:
If the users table is not needed or can be dropped, you can manually drop it using PHPMyAdmin or the MySQL command line:
DROP TABLE users;
php artisan migrate
This error occurs when you try to insert data into a table, but a field is not provided with a default value and is not nullable. Here's how you can resolve this:
$table->string('field_name')->default('default_value');
$table->string('field_name')->nullable();
After making these changes, rerun the migration.
This error occurs when a foreign key constraint fails. It means that you're trying to insert or update a value in a table that does not exist in the referenced table. To fix this, ensure that the foreign key value exists in the parent table before inserting or updating data.
This error can occur due to the MySQL server timing out. The most common reason for this is an overly large packet. You can increase the max_allowed_packet size in your MySQL configuration:
max_allowed_packet and increase the value (e.g., max_allowed_packet=64M).