Laravel 5.5 - Validate method does not exist?

1

I'm having trouble performing a validation of data in 5.5 , can you tell me where I'm wrong?

Error:

  

Here is the code for Controller

<?php

namespace App\Http\Controllers\PainelAdmin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\tbl_categoria;
use Validator;
use Illuminate\Validation;


    class CategoriaController extends Controller
    {

        private $tblcategoria;

        public function __construct(tbl_categoria $categoria)
        {

            $this->categoria = $categoria;

        }

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

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {   
            // Obtendo todos os campos do Formulário 
            $dataForm = $request->all(); 

            //Validação de Dados            
            $this->validate($request, $this->categoria->rules);

            // realizando a inserção
            $insert =  $this->categoria->create($dataForm);

            // se inserir vai redirecionar para a pagina de consulta
            if ( $insert ){
                return redirect()->route('categoriaIndex');
            } else { 
                return redirect()->back();
            }
        }

Here is the code for Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class tbl_categoria extends Model
{
    // campos que podem ser inseridos pelo usuário
    protected $fillable = ['titulo', 'descricao'];

    // protegendo os campos e  a tabela de inserções 
    protected $guarded = ['id_categoria', 'created_at', 'update_at'];
    protected $table = 'tbl_categoria';

    // validando os campos
    public $rules = [
        'titulo'    => 'required|min:3|100',
        'descricao' => 'required|min:3|max:1000',
    ];

}
    
asked by anonymous 12.10.2017 / 23:02

1 answer

1

In class $request of 5.5 that has the method that needs to be validate , then

Change your code:

//Validação de Dados            
$this->validate($request, $this->categoria->rules); // Errado

for this code:

//Validação de Dados            
$request->validate($this->categoria->rules); // Correto

End method code

public function store(Request $request)
{   
    // Obtendo todos os campos do Formulário 
    $dataForm = $request->all(); 

    //Validação de Dados            
    $request->validate($this->categoria->rules);

    // realizando a inserção
    $insert =  $this->categoria->create($dataForm);

    // se inserir vai redirecionar para a pagina de consulta
    if ( $insert ){
        return redirect()->route('categoriaIndex');
    } else { 
        return redirect()->back();
    }
}

In addition to this encoding error also in the array fault of validation you put in the tbl_categoria class has a value without saying what validation is to be done, the number 100 validates what, note :

// validando os campos
public $rules = [
    'titulo'    => 'required|min:3|100',
    'descricao' => 'required|min:3|max:1000',
];

that is, you forgot to enter the validation before the number 100, what is the validation you need?

Note in the documentation Writing The Validation Logic explains well how this would be, and it is worth saying that there are previous methods of validation with a good explanation

13.10.2017 / 01:25