Inicio » Cursos » Laravel Página Web Administrable

CRUD Blog | Curso Laravel Página Web Administrable

Podemos programar index, create, store, edit, update, show y destroy, propios de Laravel, pero adicionalmente podemos personalizar otros. Nos ceñiremos.

Lección 20: CRUD Blog

Podemos programar index, create, store, edit, update, show y destroy, propios de Laravel, pero adicionalmente podemos personalizar otros. Nos ceñiremos.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Categoria;
use Illuminate\Support\Str;
use Image;

class PostController extends Controller
{
    public function index(){
        $posts = Post::all();
        return view("admin.post.index",compact('posts'));
    }
    public function create(){
        $categorias=Categoria::orderBy('nombre','ASC')->pluck('nombre','id');
        return view('admin.post.create', compact('categorias'));
    }
    public function store(Request $request){

        $post = new Post($request->all());
        
        if($request->hasFile('urlfoto')){

            $imagen = $request->file('urlfoto');
            $nuevonombre = Str::slug($request->nombre).'.'.$imagen->guessExtension();
            Image::make($imagen->getRealPath())
            ->resize(1024,null,function($constraint){ $constraint->aspectRatio();  })
            ->save( public_path('/img/post/'.$nuevonombre));

            $post->urlfoto = $nuevonombre;
        }
        $post->slug    =   Str::slug($request->nombre);
        $post->save();
        return redirect('/admin/post');

    }
    public function update(Request $request,$id){

        $post = Post::findOrFail($id);
        $post->fill($request->all());
        $foto_anterior     = $post->urlfoto;


        if($request->hasFile('urlfoto')){

            $rutaAnterior = public_path('/img/post/'.$foto_anterior);
            if(file_exists($rutaAnterior)){ unlink(realpath($rutaAnterior)); }

            $imagen = $request->file('urlfoto');
            $nuevonombre = Str::slug($request->nombre).'.'.$imagen->guessExtension();
            Image::make($imagen->getRealPath())
            ->resize(1024,null,function($constraint){ $constraint->aspectRatio();  })
            ->save( public_path('/img/post/'.$nuevonombre));

            $post->urlfoto = $nuevonombre;
        }
        $post->slug    =   Str::slug($request->nombre);
        $post->save();
        return redirect('/admin/post');
    }

    public function edit($id){
        $post = Post::findOrFail($id);
        $categorias=Categoria::orderBy('nombre','ASC')->pluck('nombre','id');
        return view('admin.post.edit',compact('post','categorias'));
    }

    

    public function destroy($id){
        $post = Post::findOrFail($id);
        $borrar = public_path('/img/post/'.$post->urlfoto);
        if(file_exists($borrar)){ unlink(realpath($borrar)); }
        $post->delete();
        return redirect('/admin/post');
    }
}

VISTAS BLADE

index.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        @include('admin.submenu')
        <div class="col-sm-10">
            <a href="{{route('post.create')}}" class="btn btn-success">NUEVO</a>
            <table class="table table-striped">
                <thead>
                    <th>Orden</th>
                    <th>Nombre</th>                
                    <th>Acción</th>
                </thead>
                <tbody>
                    @forelse ($posts as $item)
                    <tr>
                        <td>{{$item->orden}}</td>
                        <td>{{$item->nombre}}</td>
                        <td>
                            <a href="{{ route('post.edit',$item->id)}}" class="btn btn-success">EDITAR</a>
                            {!! Form::open(['method'=>'DELETE','route'=>['post.destroy',$item->id],'style'=>'display:inline']) !!}
                            {!! Form::submit('ELIMINAR',['class'=>'btn btn-success','onclick'=>'return confirm("ESTA SEGURO DE ELIMINAR")']) !!}
                            {!! Form::close() !!}
                        </td>
                    </tr>
                    @empty
                        
                    @endforelse
                </tbody>

            </table>
        </div>
    </div>
</div>
@endsection

create.blade.php

@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        @include('admin.submenu')
        <div class="col-sm-10">

            {!! Form::open(['route'=>['post.store'],'method'=>'POST','files'=>true]) !!}

            <div class="jumbotron">
                <div class="form-group">
                    <label for="title">INGRESE TITLE</label>
                    {!! Form::text('title',null ,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>
                <div class="form-group">
                    <label for="description">INGRESE DESCRIPTION</label>
                    {!! Form::text('description',null ,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="nombre">INGRESE NOMBRE</label>
                    {!! Form::text('nombre',null ,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="descripcion">INGRESE DESCRIPCIÓN</label>
                    {!! Form::text('descripcion',null ,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="orden">INGRESE ORDEN</label>
                    {!! Form::text('orden',null ,['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    <label for="categoria_id">ELIJA CATEGORÍA</label>
                    {!! Form::select('categoria_id',$categorias,null ,['class'=>'form-control']) !!}
                </div>



                <div class="form-group">
                    <label for="urlfoto">IMAGEN</label> <br>
                    <img src="/img/post/foto.jpg">
                    {!! Form::file('urlfoto') !!}
                </div>

            </div>
            {!! Form::submit('GUARDAR',['class'=>'btn btn-success']) !!}
            {!! Form::close() !!}
        </div>
    </div>
</div>
@endsection

edit.blade.php

@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        @include('admin.submenu')
        <div class="col-sm-10">

            {!! Form::open(['route'=>['post.update',$post],'method'=>'PUT','files'=>true]) !!}

            <div class="jumbotron">
                <div class="form-group">
                    <label for="title">INGRESE TITLE</label>
                    {!! Form::text('title',$post->title,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>
                <div class="form-group">
                    <label for="description">INGRESE description</label>
                    {!! Form::text('description',$post->description,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="nombre">INGRESE NOMBRE</label>
                    {!! Form::text('nombre',$post->nombre,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="descripcion">INGRESE DESCRIPCIÓN</label>
                    {!! Form::text('descripcion',$post->descripcion,['class'=>'form-control','maxlength'=>'67']) !!}
                </div>

                <div class="form-group">
                    <label for="orden">INGRESE ORDEN</label>
                    {!! Form::text('orden',$post->orden,['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    <label for="categoria_id">ELIJA CATEGORÍA</label>
                    {!! Form::select('categoria_id',$categorias,$post->categoria_id,['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    <label for="urlfoto">IMAGEN</label> <br>
                    <img src="/img/post/{{$post->urlfoto}}">
                    {!! Form::file('urlfoto') !!}
                </div>
            </div>           
            {!! Form::submit('GUARDAR',['class'=>'btn btn-success']) !!}
            {!! Form::close() !!}
        </div>
    </div>
</div>
@endsection

 


1094 visitas

Curso Laravel Página Web Administrable

Curso Laravel Página Web Administrable

Descarga el código fuente del proyecto

USD 17.00 37.00


COMPRAR CURSO

Codea App
Codea App FullStack

Perú, México, Colombia, España, Venezuela, Argentina, Bolivia

You Fb Tik Pin

© Todos los derechos reservados Codea App | Cursos de programación | 2020 - 2022