towhid
4 months ago

i used form request for custom validation rules and message , done with save method , but i cannot understand how to done update method for this form request validation file

Posted 4 months ago by towhid

this is my Route Code

Route::get('/create','[email protected]');
Route::post('/save','[email protected]');
Route::get('/index','[email protected]')->name('indexBox');
Route::get('/edit/{id}','[email protected]');
Route::patch('/update/{id}','[email protected]');

this is my project controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
Use App\Project;
use App\Http\Requests\ProjectCreate;
use Illuminate\Support\Facades\View;
class ProjectController extends Controller
{

    //create method
    public function create(Request $request)
    {
        return view('project.create');
    }

    //save method
    public function save(ProjectCreate $request)
    {
        $data = $request->validated();
        Project::create($data);
        return back()->with('successMessage','project created Successfully');
    }

    //index method
    public function index()
    {
        $projects = Project::all();
        if(View::exists('project.index')):
            return view('project.index',compact('projects'));
        endif;
    }

    //edit method

    public function edit($id){
        $project = Project::find($id);
        return view('project.edit')->with('project',$project);
    }

    public function update(ProjectCreate $project, $id ){
        $project->update($id->all);
        return redirect('/index')->with('successMessage','project created Successfully');
    }
}

##this is my form request validation code

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
class ProjectCreate extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => ['required','unique:projects'],
            'description' => ['required'],
        ];
    }

    public function messages()
    {
        return [
            'title.required' => 'A title is required mustbe ,please fill up the message',
            'description.required' => 'A description khub dorkar',
        ];
    }
}

##this is my action button

<td><a href="">View</a> | <a href="/edit/{{$project->id}}">Edit</a> | <a href="">Delete</a></td>

this is my edit blade code

<form action="/update/{{$project->id}}" method="post">
    @method('PATCH')
    @csrf
    <input type="text" name="title" placeholder="Title" class="@error('title') is-invalid @enderror" value="{{$project->title}}">
    <br><br>
    <textarea name="description" id="" cols="30" rows="3" placeholder="Description">{{$project->description}}</textarea>
    <br><br>
    <input type="submit" value="UPDATE"> 
</form>

i don't undersatnd how to make update method perfectly

thank you

Please sign in or create an account to participate in this conversation.