Home > Net >  403 Forbidden after using @csrf in laravel 8 form
403 Forbidden after using @csrf in laravel 8 form

Time:11-26

I am new to programming especially laravel. I am trying to make a CRUD and have already added example data in prequel (using Docker). I can see the data, but when I´m trying to create new posts with a form I get Code 419 page expired. I know that´s normal and the solution is to add @csrf to the form. But after doing this I get 403 Forbidden. I tried a lot but can´t find a solution to fix it. I would be really happy if someone could help me fix my problem.

Here is my create.blade.php


@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">{{ __('Alle Gerichte') }}</div>

                <div class="card-body">
                <form action = "/recipe" method="POST">
                @csrf
                            <div class="form-group">
                                <label for="name">Name</label>
                                <input type="text" class="form-control" id="name" name="name">
                            </div>
                            <div class="form-group">
                                <label for="beschreibung">Beschreibung</label>
                                <textarea class="form-control" id="beschreibung" name="beschreibung" rows="5"></textarea>
                            </div>
                            <input class="btn btn-primary mt-4" type="submit" value="absenden">
                        </form>

                  <a class="btn btn-primary btn-sm mt-3 float-right" href="/recipe"><i class="fas fa-circle-up"></i>Zurück</a>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

CodePudding user response:

hi is that you have created validation rules

in StoreRecipeRequest

do that

public function authorize()
    {
        return true;
    }

CodePudding user response:

Controller Code will be like this:

public function store(StoreRecipeRequest $request)
{   
    //dd($request);
    $recipe = new Recipe( [ 
        'name' => $request->name,
        'beschreibung' => $request->beschreibung,
        ]);
        $recipe->save(); 
        return redirect('/recipe'); 
    } 

Also if it's not solved. Then let's try it.

public function store(StoreRecipeRequest $request)
    {   
        $recipe = new Recipe();
        $recipe->name = $request->name;
        $recipe->beschreibung = $request->beschreibung;
        $recipe->save(); 
        return redirect('/recipe');
    } 

also, can you add in your Recipe Model?

protected $fillable = [
        'name',
        'beschreibung',
    ];
  • Related