xxxxxxxxxx
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return view('products.show',compact('product'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
$product->update($request->all());
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
}
xxxxxxxxxx
Creating a CRUD (Create, Read, Update, Delete) application in Laravel is a common task and can be broken down into several steps. In this example, I'll guide you through creating a simple CRUD application for managing a list of tasks.
Step 1: Set Up Laravel
Before you begin, make sure you have Laravel installed. You can install Laravel using Composer:
bash
Copy code
composer global require laravel/installer
Save to grepper
Step 2: Create a New Laravel Project
Create a new Laravel project using the following command:
bash
Copy code
laravel new task-manager
Save to grepper
This will create a new Laravel project named "task-manager."
Step 3: Create a Task Model and Migration
Next, you'll create a model and migration for the "Task" entity. Run the following commands:
bash
Copy code
php artisan make:model Task -m
Save to grepper
This will generate a Task model and a migration file. You can find the migration file in the database/migrations directory.
Edit the migration file to define the schema for the "tasks" table. For example:
php
Copy code
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->timestamps();
});
}
Save to grepper
Then, run the migration to create the table in your database:
bash
Copy code
php artisan migrate
Save to grepper
Step 4: Create Routes
Define the routes for your CRUD operations in the routes/web.php file:
php
Copy code
Route::get('/tasks', 'TaskController@index');
Route::get('/tasks/create', 'TaskController@create');
Route::post('/tasks', 'TaskController@store');
Route::get('/tasks/{task}', 'TaskController@show');
Route::get('/tasks/{task}/edit', 'TaskController@edit');
Route::put('/tasks/{task}', 'TaskController@update');
Route::delete('/tasks/{task}', 'TaskController@destroy');
Save to grepper
Step 5: Create a Controller
Generate a controller for managing tasks:
bash
Copy code
php artisan make:controller TaskController
Save to grepper
In the TaskController.php file, implement the methods for CRUD operations (index, create, store, show, edit, update, and destroy).
Step 6: Create Views
Create the necessary Blade views in the resources/views/tasks directory for your application. You'll need views for listing tasks, creating a task, displaying a task, editing a task, etc.
Step 7: Implement CRUD Logic
In your TaskController, implement the CRUD logic using Eloquent to interact with the database. Here's a basic example for each method:
php
Copy code
public function index()
{
$tasks = Task::all();
return view('tasks.index', compact('tasks'));
}
public function create()
{
return view('tasks.create');
}
public function store(Request $request)
{
Task::create($request->all());
return redirect('/tasks');
}
public function show(Task $task)
{
return view('tasks.show', compact('task'));
}
public function edit(Task $task)
{
return view('tasks.edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect('/tasks');
}
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks');
}
Save to grepper
Step 8: Create Forms
In your Blade views, create forms for creating and editing tasks using Laravel's form helper functions (Form::open, Form::close, Form::text, Form::textarea, etc.).
Step 9: Set Up Validation
Implement validation rules for your task creation and update methods in the TaskController.
Step 10: Display Task Data
Display task data in your views using Blade templating.
Step 11: Test Your CRUD Operations
Finally, test your CRUD operations by navigating to the appropriate routes in your Laravel application.
This is a high-level overview of creating a CRUD application in Laravel. You can customize it further based on your specific requirements and add authentication, pagination, and more features as needed.Code
xxxxxxxxxx
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Add New Task') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg px-4 py-4">
<x-jet-validation-errors class="mb-4" />
<form method="POST" action="{{ route('tasks.store') }}">
@csrf
<div>
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus autocomplete="name" />
</div>
<div class="flex mt-4">
<x-jet-button>
{{ __('Save Task') }}
</x-jet-button>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
xxxxxxxxxx
public function index()
{
$tasks = Task::all();
return view('tasks.index', compact('tasks'));
}
public function create()
{
return view('tasks.create');
}
public function store(Request $request)
{
Task::create($request->all());
return redirect('/tasks');
}
public function show(Task $task)
{
return view('tasks.show', compact('task'));
}
public function edit(Task $task)
{
return view('tasks.edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect('/tasks');
}
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks');
}
xxxxxxxxxx
Creating a CRUD (Create, Read, Update, Delete) application in Laravel is a common task and can be broken down into several steps. In this example, I'll guide you through creating a simple CRUD application for managing a list of tasks.
Step 1: Set Up Laravel
Before you begin, make sure you have Laravel installed. You can install Laravel using Composer:
composer global require laravel/installer
Step 2: Create a New Laravel Project
Create a new Laravel project using the following command:
laravel new task-manager
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->timestamps();
});
}
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->timestamps();
});
}
xxxxxxxxxx
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->string('name');
$table->integer('quantity');
$table->decimal('price', 8, 2);
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
xxxxxxxxxx
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'code',
'name',
'quantity',
'price',
'description'
];
}
xxxxxxxxxx
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): View
{
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*/
public function show(Product $product): View
{
return view('products.show',compact('product'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Product $product): View
{
return view('products.edit',compact('product'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Product $product): RedirectResponse
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
$product->update($request->all());
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Product $product): RedirectResponse
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
}
xxxxxxxxxx
<!-- create.blade.php -->
@extends('layout')
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="card uper">
<div class="card-header">
Add Games Data
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<form method="post" action="{{ route('games.store') }}">
<div class="form-group">
@csrf
<label for="country_name">Game Name:</label>
<input type="text" class="form-control" name="name"/>
</div>
<div class="form-group">
<label for="cases">Price :</label>
<input type="text" class="form-control" name="price"/>
</div>
<button type="submit" class="btn btn-primary">Add Game</button>
</form>
</div>
</div>
@endsection