samedi 22 septembre 2018

Method App\Http\Controllers\todocontroller::validate does not exist error in laravel

I am using Laravel 5.7 and while validating my form fields I am getting this error:

"Method App\Http\Controllers\todocontroller::validate does not exist error."

Here is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\todo;

class todocontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $todos= todo::all();
        return view('todo.home',compact('todos'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $todo = new todo;
        $this->validate($request,[
            'body'=>'required',
            'title'=>'required|unique:todos',
        ]);
        $todo->body = $request->body;
        $todo->title = $request->title;
        $todo->save();
        return redirect('/todo');
    }

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $todo = todo::find($id);
        $this->validate($request,[
            'body'=>'required',
            'title'=>'required',
        ]);
        $todo->body = $request->body;
        $todo->title = $request->title;
        $todo->updated_at = now();
        $todo->save();
        session()->flash('message','Updated Successfully!');
        return redirect('/todo');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $todo = todo::find($id);
        $todo -> delete();
        session()->flash('message','Deleted Successfully!');
        return redirect('/todo');
    }
}

error is in this code

$this->validate($request,[
                'body'=>'required',
                'title'=>'required|unique:todos',
            ]);

but as per laravel documentation my validate method is right. Please let me know what I am doing wrong in my controller. I have also googled for this error but no success.

I have also tried using these lines :

use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Validator;

But no success. Please help me since i am getting this error for last 10 days. I don't understand what to do now. Your help will be appreciated. thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire