Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

php artisan make:model Note --migration

php artisan cache:clear


php artisan config:clear
php artisan config:cache
php artisan migrate:rollback
php artisan migrate
php artisan make:controller NoteController
php artisan make:request NoteRequest

En config/database poner: ‘engine’=> ‘InnoDB’,

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NoteController;

Route::get('/note', [NoteController::class, 'index'])->name('note.index');


Route::get('/note/create', [NoteController::class, 'create'])->name('note.create');
Route::post('/note/store', [NoteController::class, 'store'])->name('note.store');
Route::get('/note/edit/{note}', [NoteController::class, 'edit'])->name('note.edit');
Route::put('/note/update/{note}', [NoteController::class, 'update'])->name('note.update');
Route::get('/note/show/{note}', [NoteController::class, 'show'])->name('note.show');
Route::delete('/note/destroy/{note}', [NoteController::class, 'destroy'])->name('note.destroy');

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Models\Note;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
use App\Http\Requests\NoteRequest;

class NoteController extends Controller


{
public function index(): View
{
$notes = Note::all();
return view('note.index', compact('notes'));
}

public function create(): View


{
return view('note.create');
}

public function store(NoteRequest $request): RedirectResponse


{
Note::create($request->all());
return redirect()->route('note.index')->with('success','Nota Creada');
}

public function edit(Note $note): View


{
return view('note.edit', compact('note'));
}

public function update(NoteRequest $request, Note $note): RedirectResponse


{
$note->update($request->all());
return redirect()->route('note.index')->with('success','Nota Actualizada');
}

public function show(Note $note): View


{
return view('note.show', compact('note'));
}

public function destroy(Note $note): RedirectResponse


{
$note->delete();
return redirect()->route('note.index')->with('danger','Nota Eliminada');
}
}

<?php
namespace App\Models;

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

class Note extends Model


{
use HasFactory;

protected $fillable = [
"title",
"description"
];
}

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class NoteRequest extends FormRequest


{
public function authorize(): bool
{
return true;
}

public function rules(): array


{
return [
'title' => 'required|max:255|min:3',
'description' => 'required|max:255|min:3'
];
}
}

LAYOUTS/APP
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@include('layouts.messages')
@yield('content')
</body>
</html>

LAYOUTS/MESSAGES
@if($message = Session::get('success'))
<div style="padding: 10px; background-color: #4fffad; color: white">
<p>{{ $message }}</p>
</div>
@endif

@if($message = Session::get('danger'))
<div style="padding: 10px; background-color: #ff335a; color: white">
<p>{{ $message }}</p>
</div>
@endif

NOTE/INDEX
@extends('layouts.app')

@section('content')
<a href="{{ route('note.create') }}"> Crear</a>
<ul>
@forelse($notes as $note)
<li>
<a href="{{ route('note.show', $note->id) }}">{{ $note->title }}</a>
<a href="{{ route('note.edit', $note->id) }}">Editar</a>
<form method="POST" action="{{ route('note.destroy', $note->id) }}">
@csrf
@method('DELETE')
<input type="submit" value="Eliminar">
</form>
</li>
@empty
<p>No hay registros.</p>
@endforelse
</ul>
@endsection

NOTE/CREATE
@extends('layouts.app')

@section('content')
<style>
.danger { border-color: #ff232c; }
</style>

<a href="{{ route('note.index') }}"> Atras</a>

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


@csrf
<label for=""> title:</label>
<input type="text" name="title" class="@error('title') danger @enderror"> <br>
@error('title')
<p style="color: red">{{ $message }}</p>
@enderror

<label for="">Descripcion</label>
<input type="text" name="description">
@error('description')
<p style="color: red">{{ $message }}</p>
@enderror

<input type="submit" value="Crear">


</form>
@endsection

NOTE/EDIT
@extends('layouts.app')

@section('content')
<a href="{{ route('note.index') }}"> Atraz</a>

<form method="POST" action="{{route('note.update', $note->id)}}">


@method('PUT')
@csrf
<label for="">Titulo:</label>
<input type="text" name="title" value="{{ $note->title }}">
@error('title')
<p style="color: red">{{ $message }}</p>
@enderror

<label for="">Descripcion:</label>
<input type="text" name="description" value="{{ $note->description }}">
@error('description')
<p style="color: red">{{ $message }}</p>
@enderror

<input type="submit" value="Actualizar">


</form>

@endsection

NOTE/SHOW
@extends('layouts.app')

@section('content')
<a href="{{ route('note.index') }}"> Atraz</a>
<h1>{{ $note->title }}</h1>
<p>{{ $note->description }}</p>
@endsection

You might also like