Ejemplo tabla alumnos

Resumen ejemplo

Instalar Laravel 9

composer create-project laravel/laravel colegio
Version laravel
php artisan --version
image.png

Configuración BD

.env

image.png

Crear Migración

php artisan make:migration create_alumnos_table --create=alumnos
image.png
php artisan migrate
Crear controlador y modelo
php artisan make:controller AlumnoController --resource --model=Alumno
Se crearán los métodos en AlumnoController.php en la carpeta app/http:

1 )index()
2)create()
3)store()
4)show()
5)edit()
6)update()
7)destroy()

app/Http/Controllers/AlumnoController.php

image.png
<?php
namespace App\Http\Controllers;
use App\Models\Alumno;
use Illuminate\Http\Request;

class AlumnoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$alumnos = Alumno::orderBy('id','desc')->paginate(15);
return view('alumnos.index', compact('alumnos'));
//Sin paginar


$alumnos = Alumno::all();
return view('alumnos.index', ['alumnos' => $alumnos]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('alumnos.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'nombre_apellido' => 'required|max:75',
'edad' => 'required|integer',
]);
Alumno::create($request->all());
return redirect()->route('alumno.index')
->with('success','Alumno created successfully.');
}


/**
* Display the specified resource.
*
* @param \App\Models\Alumno $alumno
* @return \Illuminate\Http\Response
*/
public function show(Alumno $alumno)
{
return view('alumnos.show',compact('alumno'));
}


/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Alumno $alumno
* @return \Illuminate\Http\Response
*/
public function edit(Alumno $alumno)
{
return view('alumnos.edit',compact('alumno'));
}


/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Alumno $alumno
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Alumno $alumno)
{
$request->validate([
'nombre_apellido' => 'required|max:75',
'edad' => 'required|integer',
]);
$alumno->update($request->all());
return redirect()->route('alumnos.index')
->with('success','Alumno updated successfully');
}


/**
* Remove the specified resource from storage.
*
* @param \App\Models\Alumno $alumno
* @return \Illuminate\Http\Response
*/
public function destroy(Alumno $alumno)
{
$alumno->delete();
return redirect()->route('alumno.index')
->with('success','Alumno deleted successfully');
}
}
app/Models/Alumno.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Alumno extends Model
{
use HasFactory;
protected $fillable =['nombre_apellido', 'edad', 'telefono','direccion'];
}

Agregar Rutas Resource

Aquí, necesitamos agregar una ruta de recursos para la aplicación de productos crudos. así que abra su archivo "routes/web.php" y agregue la siguiente ruta.

routes/web.php

image.png

Ficheros Blade

Tenemos que crear las vistas que necesitamos para nuestro CRUD de alumnos. crearemos la carpeta alumnos
image.png

resources/views/products/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>colegio</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>

resources/views/products/index.blade.php

@extends('alumnos.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Alumnos</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('alumnos.create') }}"> Crear Buevo Alumno</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>Nombre y Apellido</th>
<th>Edad</th>
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.