Validation Lumen/Laravel does not work

Asked

Viewed 231 times

1

I am creating an API using Lumen v5.8, and before saving the data in the database I am validating data from an array sent by the user but the data never pass the validation.

Controller

<?php

namespace App\Http\Controllers;

use App\Lista;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class ListaController extends Controller{

    public function index(){

        return response(Lista::all());
    }

    public function store(Request $request){

        $this->validate($request, [
            'nome' => 'required|max:20'
        ]);// o código não passa daqui 

        $request = $request->json()->all();
        $lista = new Lista();
        $lista->nome = $request['nome'];
        try{
            $lista->save();
            return response(['201']);
        }
        catch(Exception $e) {
            return response(['400']);
        }
    }

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

    }  
    public function delet($id){

    }

}

Route

<?php
use Illuminate\Http\Request;

$router->group(['prefix' => 'listas'], function () use ($router) {
    $router->get('/','ListaController@index');
    $router->post('/','ListaController@store');
    $router->put('/','ListaController@update');
    $router->delete('/','ListaController@delete');
});

Postman inserir a descrição da imagem aqui

1 answer

1


In Postman, at the line where you selected raw, the right most is marked as text, switch to JSON (application/json).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.