0% found this document useful (0 votes)
12 views18 pages

Examen Remedial

This document outlines the steps to create a CRUD application using Laravel 11, including installation, database configuration, migration, request validation, controller and model creation, and route setup. It also details the creation of Blade templates for the user interface and concludes with instructions to run the application. The steps are structured sequentially to guide the user through the development process effectively.

Uploaded by

Addi Guerrero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

Examen Remedial

This document outlines the steps to create a CRUD application using Laravel 11, including installation, database configuration, migration, request validation, controller and model creation, and route setup. It also details the creation of Blade templates for the user interface and concludes with instructions to run the application. The steps are structured sequentially to guide the user through the development process effectively.

Uploaded by

Addi Guerrero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

 Paso 1: Instalar Laravel 11

composer create-project laravel/laravel example-app


 Paso 2: Crear la configuración de MySQL Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Remedial
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
 Paso 3: Hacer la migración
php artisan make:migration create_products_table --create=products
<?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('name');
$table->text('detail');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
}
php artisan migrate
 Paso 4: Crear la forma para la validación Class
php artisan make:request ProductStoreRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array|string>
*/
public function rules(): array
{
return [
'name' => 'required',
'detail' => 'required'
];
}
}
php artisan make:request ProductUpdateRequest
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProductUpdateRequest extends FormRequest


{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array|string>
*/
public function rules(): array
{
return [
'name' => 'required',
'detail' => 'required'
];
}
}

 Paso 5: Crear el Controlador y el Modelo


artisan make:controller ProductController --resource --model=Product
<?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;
use App\Http\Requests\ProductStoreRequest;
use App\Http\Requests\ProductUpdateRequest;

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(ProductStoreRequest $request):
RedirectResponse
{
Product::create($request->validated());

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(ProductUpdateRequest $request, Product
$product): RedirectResponse
{
$product->update($request->validated());

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');
}
}
App/model/Product.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model


{
use HasFactory;

protected $fillable = [
'name',
'detail',
];
}

 Paso 6: Agregar el recurso de la ruta


routes/web.php
<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ProductController;
Route::get('/', function () {
return view('welcome');
});

Route::resource('products', ProductController::class);

 Paso 7: Actualizar el AppServiceProvider


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider


{
/**
* Register any application services.
*/
public function register(): void
{

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Paginator::useBootstrapFive();
}
}
 Paso 8: Agregar los archivos Blades
1. layout.blade.php
2. index.blade.php
3. create.blade.php
4. edit.blade.php
5. show.blade.php

resources/views/products/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 CRUD Application - ItSolutionStuff.com</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/6.5.1/css/all.min.css" />
</head>
<body>

<div class="container">
@yield('content')
</div>

</body>
</html>
resources/views/products/index.blade.php
@extends('products.layout')
@section('content')
<div class="card mt-5">
<h2 class="card-header">Laravel 11 CRUD Example from scratch -
ItSolutionStuff.com</h2>
<div class="card-body">

@session('success')
<div class="alert alert-success" role="alert"> {{ $value }} </div>
@endsession
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<a class="btn btn-success btn-sm"
href="{{ route('products.create') }}"> <i class="fa fa-plus"></i> Create New
Product</a>
</div>

<table class="table table-bordered table-striped mt-4">


<thead>
<tr>
<th width="80px">No</th>
<th>Name</th>
<th>Details</th>
<th width="250px">Action</th>
</tr>
</thead>

<tbody>
@forelse ($products as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->detail }}</td>
<td>
<form action="{{ route('products.destroy',$product->id) }}"
method="POST">

<a class="btn btn-info btn-sm"


href="{{ route('products.show',$product->id) }}"><i class="fa-solid fa-
list"></i> Show</a>

<a class="btn btn-primary btn-sm"


href="{{ route('products.edit',$product->id) }}"><i class="fa-solid fa-pen-to-
square"></i> Edit</a>

@csrf
@method('DELETE')

<button type="submit" class="btn btn-danger btn-sm"><i


class="fa-solid fa-trash"></i> Delete</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="4">There are no data.</td>
</tr>
@endforelse
</tbody>

</table>

{!! $products->links() !!}

</div>
</div>
@endsection
Resources/views/products/create.blade.php
@extends('products.layout')

@section('content')

<div class="card mt-5">


<h2 class="card-header">Add New Product</h2>
<div class="card-body">

<div class="d-grid gap-2 d-md-flex justify-content-md-end">


<a class="btn btn-primary btn-sm"
href="{{ route('products.index') }}"><i class="fa fa-arrow-left"></i>
Back</a>
</div>

<form action="{{ route('products.store') }}" method="POST">


@csrf

<div class="mb-3">
<label for="inputName"
class="form-label"><strong>Name:</strong></label>
<input
type="text"
name="name"
class="form-control @error('name') is-invalid @enderror"
id="inputName"
placeholder="Name">
@error('name')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>

<div class="mb-3">
<label for="inputDetail"
class="form-label"><strong>Detail:</strong></label>
<textarea
class="form-control @error('detail') is-invalid @enderror"
style="height:150px"
name="detail"
id="inputDetail"
placeholder="Detail"></textarea>
@error('detail')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-success"><i class="fa-solid fa-
floppy-disk"></i> Submit</button>
</form>

</div>
</div>
@endsection
@extends('products.layout')

@section('content')

<div class="card mt-5">


<h2 class="card-header">Add New Product</h2>
<div class="card-body">
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<a class="btn btn-primary btn-sm"
href="{{ route('products.index') }}"><i class="fa fa-arrow-left"></i>
Back</a>
</div>

<form action="{{ route('products.store') }}" method="POST">


@csrf

<div class="mb-3">
<label for="inputName"
class="form-label"><strong>Name:</strong></label>
<input
type="text"
name="name"
class="form-control @error('name') is-invalid @enderror"
id="inputName"
placeholder="Name">
@error('name')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>

<div class="mb-3">
<label for="inputDetail"
class="form-label"><strong>Detail:</strong></label>
<textarea
class="form-control @error('detail') is-invalid @enderror"
style="height:150px"
name="detail"
id="inputDetail"
placeholder="Detail"></textarea>
@error('detail')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-success"><i class="fa-solid fa-
floppy-disk"></i> Submit</button>
</form>

</div>
</div>
@endsection

Resources/views/products/edit.blade.php
@extends('products.layout')

@section('content')

<div class="card mt-5">


<h2 class="card-header">Edit Product</h2>
<div class="card-body">

<div class="d-grid gap-2 d-md-flex justify-content-md-end">


<a class="btn btn-primary btn-sm"
href="{{ route('products.index') }}"><i class="fa fa-arrow-left"></i>
Back</a>
</div>

<form action="{{ route('products.update',$product->id) }}"


method="POST">
@csrf
@method('PUT')

<div class="mb-3">
<label for="inputName"
class="form-label"><strong>Name:</strong></label>
<input
type="text"
name="name"
value="{{ $product->name }}"
class="form-control @error('name') is-invalid @enderror"
id="inputName"
placeholder="Name">
@error('name')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>

<div class="mb-3">
<label for="inputDetail"
class="form-label"><strong>Detail:</strong></label>
<textarea
class="form-control @error('detail') is-invalid @enderror"
style="height:150px"
name="detail"
id="inputDetail"
placeholder="Detail">{{ $product->detail }}</textarea>
@error('detail')
<div class="form-text text-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-success"><i class="fa-solid fa-
floppy-disk"></i> Update</button>
</form>

</div>
</div>
@endsection

Resources/views/products/show.blade.php
@extends('products.layout')

@section('content')

<div class="card mt-5">


<h2 class="card-header">Show Product</h2>
<div class="card-body">

<div class="d-grid gap-2 d-md-flex justify-content-md-end">


<a class="btn btn-primary btn-sm"
href="{{ route('products.index') }}"><i class="fa fa-arrow-left"></i>
Back</a>
</div>

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong> <br/>
{{ $product->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 mt-2">
<div class="form-group">
<strong>Details:</strong> <br/>
{{ $product->detail }}
</div>
</div>
</div>

</div>
</div>
@endsection

 Paso 9.- Correr la aplicación de Laravel


php artisan serve

You might also like