Create Laravel Models, Controllers,Migrations,Resrouces and more
This command will create:
- A model file in
app/Models/ - A migration file in
database/migrations/ - A controller file in
app/Http/Controllers/(as a resource controller) - A factory file in
database/factories/
php artisan make:model "Name" --migration --controller --resource --factory
Summary
- Create a model and migration:
php artisan make:model PaymentType -m - Define the migration and run it:
php artisan migrate - Generate a resource controller:
php artisan make:controller PaymentTypeController --resource - Define resource routes in
web.php. - Generate resource views and create the necessary Blade files.
- Implement CRUD methods in the controller and views.
In Laravel 10, you can use the Artisan command-line tool to automatically generate controllers, models, routes, and resource views for typical CRUD operations. Here’s a step-by-step guide to accomplish this:
Step 1: Create a Model and Migration
First, create a model along with a migration file using the Artisan command:
php artisan make:model PaymentType -m
This command creates a PaymentType model and a migration file for creating the payment_types table.
Step 2: Define the Migration
Open the migration file located in the database/migrations directory and define the schema for the payment_types table:
public function up()
{
Schema::create('payment_types', function (Blueprint $table) {
$table->id();
$table->string('type');
$table->longText('description')->nullable();
$table->timestamps();
});
}
Then run the migration to create the table in the database:
php artisan migrate
Step 3: Generate a Resource Controller
Next, generate a resource controller for the PaymentType model:
php artisan make:controller PaymentTypeController --resource
This command creates a PaymentTypeController with methods for typical CRUD operations (index, create, store, show, edit, update, destroy).
Step 4: Define Resource Routes
Open the routes/web.php file and define resource routes for the PaymentTypeController:
Route::resource('payment-types', PaymentTypeController::class);
Step 5: Generate Resource Views
Create a directory for the resource views in the resources/views directory:
mkdir -p resources/views/payment_types
You will have to manually create the necessary Blade view files (index.blade.php, create.blade.php, edit.blade.php, show.blade.php) in the resources/views/payment_types directory.
Step 6: Implement Controller Methods and Views
Controller Methods
In the PaymentTypeController, implement the CRUD methods. Here is a basic implementation:
namespace App\Http\Controllers;
use App\Models\PaymentType;
use Illuminate\Http\Request;
class PaymentTypeController extends Controller
{
public function index()
{
$paymentTypes = PaymentType::all();
return view('payment_types.index', compact('paymentTypes'));
}
public function create()
{
return view('payment_types.create');
}
public function store(Request $request)
{
$request->validate([
'type' => 'required|string|max:255',
'description' => 'nullable|string',
]);
PaymentType::create($request->all());
return redirect()->route('payment-types.index')->with('success', 'Payment Type created successfully.');
}
public function show(PaymentType $paymentType)
{
return view('payment_types.show', compact('paymentType'));
}
public function edit(PaymentType $paymentType)
{
return view('payment_types.edit', compact('paymentType'));
}
public function update(Request $request, PaymentType $paymentType)
{
$request->validate([
'type' => 'required|string|max:255',
'description' => 'nullable|string',
]);
$paymentType->update($request->all());
return redirect()->route('payment-types.index')->with('success', 'Payment Type updated successfully.');
}
public function destroy(PaymentType $paymentType)
{
$paymentType->delete();
return redirect()->route('payment-types.index')->with('success', 'Payment Type deleted successfully.');
}
}
Views
Create the necessary Blade view files for displaying the form and list of payment types.
index.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Payment Types</h1>
<a href="{{ route('payment-types.create') }}" class="btn btn-primary">Create New Payment Type</a>
@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
<table class="table table-bordered mt-3">
<thead>
<tr>
<th>ID</th>
<th>Type</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($paymentTypes as $paymentType)
<tr>
<td>{{ $paymentType->id }}</td>
<td>{{ $paymentType->type }}</td>
<td>{{ $paymentType->description }}</td>
<td>
<a href="{{ route('payment-types.show', $paymentType->id) }}" class="btn btn-info">View</a>
<a href="{{ route('payment-types.edit', $paymentType->id) }}" class="btn btn-warning">Edit</a>
<form action="{{ route('payment-types.destroy', $paymentType->id) }}" method="POST" style="display:inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
create.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Create Payment Type</h1>
<form action="{{ route('payment-types.store') }}" method="POST">
@csrf
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" id="type" name="type" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
@endsection
edit.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Edit Payment Type</h1>
<form action="{{ route('payment-types.update', $paymentType->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" id="type" name="type" value="{{ $paymentType->type }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description">{{ $paymentType->description }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
@endsection
Use Tinker to Check the Database Columns
You can use Laravel Tinker to ensure your payment_types table has the correct columns. Open your terminal and run:
php artisan tinker
Schema::getColumnListing('payment_types');
This will list all columns in the payment_types table and help you verify that type and description are included.
>>> exit
or use ctrl+c
