adam
adam

Reputation: 638

Forbidden error when creating nested controller object laravel

I'm folowing a tutorial on https://laracasts.com/series/laravel-5-fundamentals then I started digging into nested controllers from this tutorial https://www.flynsarmy.com/2015/02/creating-a-basic-todo-application-in-laravel-5-part-4/

I've got a similar logic Project which has one hypothesis.

So I've setup my nested routes

Route::resource('project','ProjectsController');
Route::resource('project.hypothesis','HypothesisController'); 

Then created a form for adding a hypothesis to a Project

{!! Form::model(new App\Hypothesis, ['route' => ['project.hypothesis.store', $project->id]]) !!}
    @include ('hypothesis.form',['submitButtonText'=>'create']);
{!! Form::close() !!}

I also created a HyphothesisRequest class with basic validation rules

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class HyphothesisRequest extends Request {

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return false;
}

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

Now according to the above tutorials in my controller i've got

public function store(Project $project, HyphothesisRequest $request)
{
    $this->validate($request);
    $h = new Hypothesis;
    $h->description = $request->description;
    $h->project_id = $project->id;
    $h->save();

    return Redirect::route('project.show', $project->id);

}

The problem is that when the HyphothesisRequest $request is passed as an argument I get an forbidden page from laravel. When I remove this it goes to the desired page but without validation.

I'm at the basic level of this so please be patient :)

Upvotes: 0

Views: 351

Answers (1)

Gustavo Gama
Gustavo Gama

Reputation: 91

Try change

public function authorize()
{
    return false;
}

to

public function authorize()
{
    return true;
}

Upvotes: 2

Related Questions