Error 404, laravel does not get a view with some data sent

0

As I did not manage to use datatables, I managed to improvise a different method. One that opens a form that edits the tables, the one to erase since it is done, can ignore that.

For some reason, the view does not appear. It says 404 error.

The view in home.blade

<table class="table table-striped">
<thead>
  <tr>
    <th>ID</th>
    <th>Nombre</th>
    <th>Precio</th>
    <th>Fecha de inscripción</th>
    <th>Acciones</th>
  </tr>
</thead>
<tbody>     
  @foreach($product as $products)

  <tr>
    <td>{{$products['id']}}</td>
    <td>{{$products['producto']}}</td>
    <td>{{$products['precio']}}</td>
    <td>{{$products['empresa']}}</td>

    <td>
      <form action="{{route('home.destroy', $products->id)}}" method="POST">
        @csrf
        {{ method_field('DELETE') }} 
        <input name="_method" type="hidden" value="DELETE">
        <button class="btn btn-danger" type="submit">Borrar</button>
      </form>
    </td>
    <td><a href="{{action('HomeController@edit', $products['id'])}" class="btn btn-warning">Editar</a></td>
  </tr>
  @endforeach
</tbody>
</table>

The Controller.

class HomeController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $product=\App\Product::all();
    return view('home',compact('product'));
}

public function sendData(Request $request){
    $this->validate($request, [
        'nombre'    =>  'required',
        'precio'     =>  'required',
        'empresa'     =>  'required'
    ]);

    $product = new product([
        'nombre'    =>  $request->get('nombre'),
        'precio'     =>  $request->get('precio'),
        'empresa'     =>  $request->get('empresa')
    ]);
    $product->save();
    return redirect()->route('home')->with('success', 'Data Added');
}

public function edit($id)
{
    $products = Product::find($id);
    return view('edit',compact('products','id'));
}

public function update(Request $request, $id)
{
    $this->validate($request, [
        'producto'    =>  'required',
        'precio'     =>  'required',
        'empresa'     =>  'required'
    ]);
    $products = Product::find($id);
    $products->producto = $request->get('producto');
    $products->precio = $request->get('precio');
    $products->empresa = $request->get('empresa');
    $products->save();
    return redirect()->route('home')->with('success', 'Data Added');
}

public function destroy($id)
{
    $products = Product::findOrFail($id);
    $products->delete();
    return redirect('home');
}


}

The routes:

Route::get('/', function () {
return view('welcome');
});

//Route::resource('product', 'ProductController');

Auth::routes();
Route::resource('home', 'HomeController');
Route::get('/home', 'HomeController@index')->name('home'); //Vista de usuario
Route::post('/home/sendData', 'HomeController@sendData')->name('home/sendData');

The view you have to go to:

@extends('layouts.app')
@section('content')
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 5.6 CRUD Tutorial With Example </title>
</head>
<body>
<div class="container">
  <h2>Edit A Form</h2><br  />
  <form method="post" action="{{action('HomeController@update', $id)}}">
  <input type="hidden" name="_method" value="PATCH" />
  <div class="form-group">
   <input type="text" name="producto" class="form-control" value="{{$products['producto']}}" placeholder="Producto" />
   </div>
   <div class="form-group">
   <input type="text" name="precio" class="form-control" value="{{$products['precio']}}" placeholder="Precio" />
   </div>
   <div class="form-group">
   <input type="text" name="empresa" class="form-control" value="{{$products['empresa']}}" placeholder="Empresa" />
   </div>
   <div class="form-group">
   <input type="submit" class="btn btn-primary" value="Edit" />
   </div>

</form>
</body>
</html>
    
asked by Shredder 14.11.2018 в 23:01
source

2 answers

0

I do it in the following way for a call to a view

 return view('Arboles.index', [
            "Argrupo"    => $Argrupo,
            'listmysql'  => $Arboles]);

on the router part

Route::get('Arbole_especie/pdf', 'Arbole_especieController@pdf');
Route::resource('Arbole_especie', 'Arbole_especieController');
Route::post('Arbole_especie/changeStatus', array('as' => 'changeStatus', 'uses' => 'Arbole_especieController@changeStatus'));

at the time of editing a query is made in the function

public function edit($id)
    {
        $pruebas_marcos = pruebas_marcosModel::findOrFail($id);
        return view('Arboles.editar', [
        "Argrupo"    => $pruebas_marcos,
        'listmysql'  => $pruebas_marcos]);
    }

and will send it to the function

public function store(Request $request)
    {
        $validator = Validator::make(Input::all(), $this->rules);
        if ($validator->fails()) {
            return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
        } else {
            $pruebas_marcos = new pruebas_marcosModel();

             $pruebas_marcos->nombre=$request->nombre;
                 $pruebas_marcos->nombre_cientifico=$request->nombre_cientifico;
                 $pruebas_marcos->especie_arbolescol=$request->especie_arbolescol;
                 $pruebas_marcos->tasa_crecimiento_id=$request->tasa_crecimiento_id;
                 $pruebas_marcos->dimension_tasacrecimiento=$request->dimension_tasacrecimiento;
                 $pruebas_marcos->created_at=$request->created_at;
                 $pruebas_marcos->updated_at=$request->updated_at;

            $pruebas_marcos->save();
            return response()->json($pruebas_marcos);
        }
    }

in the part of the validations the function $ rules is used at the beginning of the controller

 protected $rules =
        [

                    'id' => 'required|min:1|max:99999999',
                    'nombre' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-]*)*)+$/',
                    'nombre_cientifico' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-]*)*)+$/',
                    'forma_habito' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-]*)*)+$/',
                    'toleranciapodas' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-]*)*)+$/',
                    'especie_arbolescol' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.,()_-]*)*)+$/',
                    'tasa_crecimiento_id' => 'required|min:1|max:99999999',

        ];
    
answered by 15.11.2018 в 02:45
0

Your problem is that you are stating twice your route home

Route::resource('home', 'HomeController');
Route::get('/home', 'HomeController@index')->name('home'); //Vista de usuario

The 2 you have the same url to access home , this makes laravel generate this type of errors. Solution changes the name of the route

    
answered by 18.11.2018 в 16:22