Meter data in Laravel framework

0

I have problems with Laravel framework.

My problem is that for some reason, when I try to put data in a way, they are not sent and send a 404 "page not found" error.

This is the code I have.

web (In the routes folder):

<?php

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

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::post('/home/sendData', 'HomeController@sendData')->name('home/sendData');

HomeController (In the controllers folder):

public function index()
{
    return view('home');
}
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');
}

home.blade (In the views folder):

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">Dashboard</div>

            <div class="card-body">
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif

                Has iniciado sesion
             </div>
         </div>
     </div>
   </div>
 </div>

<div class="row">
<div class="col-md-12">
<br />
<h3>Meter datos del producto</h3>
<br />
<form method="post" action="{{url('home')}}">
{{csrf_field()}}
<div class="form-group" align="center">
<input type="text" name="nombre" class="form-control" placeholder="Coloque el nombre del producto" />
</div>
<div class="form-group" align="center">
<input type="number" name="precio" class="form-control" placeholder="Coloque el precio" />
</div>
<div class="form-group" align="center">
<input type="text" name="empresa" class="form-control" placeholder="Coloque el nombre de la empresa" />
</div>
<div class="form-group" align="center">
<input type="submit" class="btn btn-primary" />
</div>
</form>
</div>
</div>

@endsection

This is my database. I want you to connect to products

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Servidor: 127.0.0.1
-- Tiempo de generación: 20-10-2018 a las 23:00:35
-- Versión del servidor: 10.1.36-MariaDB
-- Versión de PHP: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Base de datos: 'lava_prueba'
--

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'migrations'
--

CREATE TABLE 'migrations' (
'id' int(10) UNSIGNED NOT NULL,
'migration' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'batch' int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Volcado de datos para la tabla 'migrations'
--

INSERT INTO 'migrations' ('id', 'migration', 'batch') VALUES
(1, '2014_10_12_000000_create_users_table', 1),
(2, '2014_10_12_100000_create_password_resets_table', 1),
(3, '2018_10_13_032430_create_products_table', 1);

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'password_resets'
--

CREATE TABLE 'password_resets' (
'email' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'token' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'created_at' timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'products'
--

CREATE TABLE 'products' (
'id' int(10) UNSIGNED NOT NULL,
'producto' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'precio' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'empresa' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'created_at' timestamp NULL DEFAULT NULL,
'updated_at' timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla 'users'
--

CREATE TABLE 'users' (
'id' int(10) UNSIGNED NOT NULL,
'name' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'email' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'email_verified_at' timestamp NULL DEFAULT NULL,
'password' varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
'remember_token' varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
'created_at' timestamp NULL DEFAULT NULL,
'updated_at' timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- Índices para tablas volcadas
--

--
-- Indices de la tabla 'migrations'
--
ALTER TABLE 'migrations'
ADD PRIMARY KEY ('id');

--
-- Indices de la tabla 'password_resets'
--
ALTER TABLE 'password_resets'
ADD KEY 'password_resets_email_index' ('email');

--
-- Indices de la tabla 'products'
--
ALTER TABLE 'products'
ADD PRIMARY KEY ('id');

--
-- Indices de la tabla 'users'
--
ALTER TABLE 'users'
ADD PRIMARY KEY ('id'),
ADD UNIQUE KEY 'users_email_unique' ('email');

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla 'migrations'
--
ALTER TABLE 'migrations'
MODIFY 'id' int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT de la tabla 'products'
--
ALTER TABLE 'products'
MODIFY 'id' int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla 'users'
 --
 ALTER TABLE 'users'
 MODIFY 'id' int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
 COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    
asked by Shredder 20.10.2018 в 23:21
source

1 answer

1

I think you're sending the form to the wrong action.

Error

<form method="post" action="{{url('home')}}">

You must point it to the route where you want to send the data

<form method="post" action="{{url('home/sendData')}}">

I recommend that you remove the slash to the name leave it as "home_sendData".

Greetings

    
answered by 21.10.2018 в 05:21