- Install dependencies
composer install
- Generate application key
php artisan key:generate
- Create a mysql database then add the credentials to .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
- Run migrations
php artisan migrate
- Run seeders
php artisan db:seed
- Create laravel/passport client key
php artisan passport:client --personal
- Models are located at app/Models
- Migrations are located at database/migrations
- Seeders are located at database/seeders
- Controllers are located at app/Http/Controllers
- Routes are located at routes
- /resources is where our frontend reside
- Creating a model
php artisan make:model Store
- Creating a migration file
php artisan make:migration create_stores_table
- Create a model and migration in one command
php artisan make:model ModelName -m
- Creating a controller
php artisan make:controller StoreController
- To migrate the tables
php artisan migrate
- Create a route file example rotues/api.php
php artisan install:api
- Get route list
php artisan route:list
- Creating a seeder
php artisan make:seeder UserSeeder
- Running seeder Running the database seeder (all registered seeder)
php artisan db:seed
Running specific seeder
php artisan db:seed --class=UserSeeder
Reset and Ressed
php artisan migrate:refresh --seed
Certainly! Here are separate examples for accessing different types of data from the $request
object in Laravel without including the whole controller.
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function handleRequest(Request $request, $id)
{
// 1. **Route Parameters**: Accessing route parameters
// Assuming the route is defined as: Route::get('/example/{id}', [ExampleController::class, 'handleRequest']);
$routeParam = $id; // Accessing the route parameter directly
// 2. **Query Parameters**: Accessing query parameters from the URL
// Example: /example?id=123&filter=active
$queryParamId = $request->query('id'); // Get 'id' from query string
$filter = $request->query('filter'); // Get 'filter' from query string
// 3. **Request Body (Form Data)**: Accessing form data sent in the request body
// Example: Form submission with fields 'name' and 'email'
$name = $request->input('name'); // Get 'name' from form data
$email = $request->input('email'); // Get 'email' from form data
// 4. **Retrieving All Input Data**: Getting all input data
$allData = $request->all(); // Get all data from both form and query
// 5. **Retrieving JSON Data**: Accessing JSON data if the request is JSON
// Assuming a JSON payload like: { "key": "value" }
$jsonValue = $request->json('key'); // Get a specific key from JSON
$allJsonData = $request->json()->all(); // Get all JSON data
// Example response
return response()->json([
'route_param' => $routeParam,
'query_param_id' => $queryParamId,
'filter' => $filter,
'name' => $name,
'email' => $email,
'all_data' => $allData,
'json_value' => $jsonValue,
'all_json_data' => $allJsonData,
]);
}
}
// Assuming the route is defined as: Route::get('/example/{id}', [ExampleController::class, 'handleRequest']);
$routeParam = $id; // Accessing the route parameter directly
// Example: /example?id=123&filter=active
$queryParamId = $request->query('id'); // Get 'id' from query string
$filter = $request->query('filter'); // Get 'filter' from query string
// Example: Form submission with fields 'name' and 'email'
$name = $request->input('name'); // Get 'name' from form data
$email = $request->input('email'); // Get 'email' from form data
$allData = $request->all(); // Get all data from both form and query
// Assuming a JSON payload like: { "key": "value" }
$jsonValue = $request->json('key'); // Get a specific key from JSON
$allJsonData = $request->json()->all(); // Get all JSON data