When I try to make an index.blade page, but I get an error

Issue

Action [email protected] not defined. (View: C:\xampp\htdocs\Lab_13\crudlaravel\resources\views\Libro\index.blade.php)

I have tried to define the route and I feel that I am wrong in that but when I try to change it to another one like Route::resource(‘book’, ‘BookController’);
it just comes out
Target class [BookController] does not exist.

Route

    Route::resource('libro', 'App\Http\Controllers\LibroController');

Blade

@extends('layouts.layout')
@section('content')
<div class="row">
  <section class="content">
    <div class="col-md-8 col-md-offset-2">
      <div class="panel panel-default">
        <div class="panel-body">
          <div class="pull-left"><h3>Lista Libros</h3></div>
          <div class="pull-right">
            <div class="btn-group">
              <a href="{{ route('libro.create') }}" class="btn btn-info" >Añadir Libro</a>
            </div>
          </div>
          <div class="table-container">
            <table id="mytable" class="table table-bordred table-striped">
             <thead>
               <th>Nombre</th>
               <th>Resumen</th>
               <th>No. Páginas</th>
               <th>Edicion</th>
               <th>Autor</th>
               <th>Precio</th>
               <th>Editar</th>
               <th>Eliminar</th>
             </thead>
             <tbody>
              @if($libros->count())  
              @foreach($libros as $libro)  
              <tr>
                <td>{{$libro->nombre}}</td>
                <td>{{$libro->resumen}}</td>
                <td>{{$libro->npagina}}</td>
                <td>{{$libro->edicion}}</td>
                <td>{{$libro->autor}}</td>
                <td>{{$libro->precio}}</td>
                <td><a class="btn btn-primary btn-xs" href="{{action('[email protected]', $libro->id)}}" ><span class="glyphicon glyphicon-pencil"></span></a></td>
                <td>
                  <form action="{{action('[email protected]', $libro->id)}}" method="post">
                   {{csrf_field()}}
                   <input name="_method" type="hidden" value="DELETE">

                   <button class="btn btn-danger btn-xs" type="submit"><span class="glyphicon glyphicon-trash"></span></button>
                 </td>
               </tr>
               @endforeach 
               @else
               <tr>
                <td colspan="8">No hay registro !!</td>
              </tr>
              @endif
            </tbody>

          </table>
        </div>
      </div>
      {{ $libros->links() }}
    </div>
  </div>
</section>

@endsection

Controller

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Libro;

class LibroController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $libros=Libro::orderBy('id','DESC')->paginate(10);
        return view('Libro.index',compact('libros'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[ 'nombre'=>'required', 'resumen'=>'required', 'npagina'=>'required', 'edicion'=>'required', 'autor'=>'required', 'npagina'=>'required', 'precio'=>'required']);
        Libro::create($request->all());
        return redirect()->route('libro.index')->with('success','Registro creado satisfactoriamente');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $libros=Libro::find($id);
        return  view('libro.show',compact('libros'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $libro=libro::find($id);
        return view('libro.edit',compact('libro'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[ 'nombre'=>'required', 'resumen'=>'required', 'npagina'=>'required', 'edicion'=>'required', 'autor'=>'required', 'npagina'=>'required', 'precio'=>'required']);

        libro::find($id)->update($request->all());
        return redirect()->route('libro.index')->with('success','Registro actualizado satisfactoriamente');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Libro::find($id)->delete();
        return redirect()->route('libro.index')->with('success','Registro eliminado satisfactoriamente');
    }
}

Solution

Guess there’s an issue with the syntax for action helper in the code

<td>
    <a class="btn btn-primary btn-xs" href="{{action(['App\Http\Controllers\LibroController', 'edit'], ['libro' => $libro->id])}}" >
        <span class="glyphicon glyphicon-pencil"></span>
    </a>
</td>

Laravel Docs – Helpers – Method action

However, since you are using the Route::resource the generated routes will be named routes so instead of action you can use the route helper


<td>
    <a class="btn btn-primary btn-xs" href="{{route('libro.edit', ['libro' => $libro->id])}}" >
        <span class="glyphicon glyphicon-pencil"></span>
    </a>
</td>

Laravel Docs – Helpers – Method route

When using Route::resource Route::resource('book', 'BookController') won’t work because you need to provide the FQCN of the controller.

Need to either import the FCQN via use statement or provide a string.

use App\Http\Controllers\BookController;

Route::resource('book', BookController::class);

//Or without the use statement
Route::resource('book', 'App\Http\Controllers\BookController')

Answered By – Donkarnash

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published