0

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
daisy
  • 3
  • 2
  • Can you see the CSRF field when you inspect the page in your browser ? – thchp Nov 25 '21 at 12:38
  • @thchp no I only get the default '403 this action is unauthorized page' from laravel in my browser – daisy Nov 25 '21 at 12:43
  • Hi @daisy, can you post your `api.php` and the controller that is pointed to your `/recipe` route so we can further help you. Thanks – jrran90 Nov 25 '21 at 13:03

2 Answers2

1

hi is that you have created validation rules

in StoreRecipeRequest

do that

public function authorize()
    {
        return true;
    }
Stevy
  • 76
  • 2
  • 4
0

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',
    ];
Md Atiqur
  • 456
  • 6
  • 10
  • Can you share your **Controller** Code – Md Atiqur Nov 25 '21 at 12:49
  • Tried it but unfortunately doesn´t change anything – daisy Nov 25 '21 at 12:49
  • public function store(StoreRecipeRequest $request) { //dd($request); $recipe = new Recipe( [ 'name' => $request['name'], 'beschreibung' => $request['beschreibung'] ] ); $recipe->save(); return redirect('/recipe'); } – daisy Nov 25 '21 at 12:50
  • Let me know after the update code use. – Md Atiqur Nov 25 '21 at 13:07
  • thank you very much, but it still doesn´t work. before I added the functions in the store function I tried dd() and that didn´t work either. could it be a problem with the database? – daisy Nov 25 '21 at 13:12
  • I am not sure about it but It's can't be a Database problem. It's must be a coding problem please check this [StackQuestion](https://stackoverflow.com/questions/47128903/errors-this-action-is-unauthorized-using-form-request-validations-in-laravel) And If you can try another way then read this latest [Blog](https://www.itsolutionstuff.com/post/laravel-8-crud-application-tutorial-for-beginnersexample.html) Thank You – Md Atiqur Nov 25 '21 at 13:17