控制器方法未与自定义表单请求方法一起调用

时间:2019-08-27 08:46:54

标签: laravel laravel-5 laravel-5.8

为了进行表单验证,我通过Request class创建了一个php artisan make:request UpdatePlanRequest

但是在存储区中使用UpdatePlanRequest类之后,该方法不再被调用。

UpdatePlanRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePlanRequest 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()
    {   //TODO: CHECK IF THE PROTOTYPE IDS ARE OWNED BY THE USER (https://stackoverflow.com/questions/42662579/validate-an-array-of-integers/42693970)
        return [
            'start_date' => 'required|date',
            'end_date' => 'required|date|after:start_date',
            'name' => 'required|string'
        ];
    }
}

控制器方法:

use App\Http\Requests\UpdatePlanRequest;

public function store(UpdatePlanRequest $request)
    {
        //
        dd('hello');
    }

如果显示的函数标头为store(Request $request) hello,则该示例中没有显示。

自定义Request类对于稍后根据docs进行验证而调用$request->validated();是必需的。

3 个答案:

答案 0 :(得分:0)

请问您的Request类是抽象的吗?运行ORDER BY时创建的默认类未将其定义为抽象类。这似乎对我有用,但是在将其声明为抽象时不起作用。

答案 1 :(得分:0)

// Register Custom Post Type Our Product function create_ourproduct_cpt() { $labels = array( 'name' => _x( 'Our Products', 'Post Type General Name', 'our-products' ), 'singular_name' => _x( 'Our Product', 'Post Type Singular Name', 'our-products' ), 'menu_name' => _x( 'Our Products', 'Admin Menu text', 'our-products' ), 'name_admin_bar' => _x( 'Our Product', 'Add New on Toolbar', 'our-products' ), 'archives' => __( 'Our Product Archives', 'our-products' ), 'attributes' => __( 'Our Product Attributes', 'our-products' ), 'parent_item_colon' => __( 'Parent Our Product:', 'our-products' ), 'all_items' => __( 'All Our Products', 'our-products' ), 'add_new_item' => __( 'Add New Our Product', 'our-products' ), 'add_new' => __( 'Add New', 'our-products' ), 'new_item' => __( 'New Our Product', 'our-products' ), 'edit_item' => __( 'Edit Our Product', 'our-products' ), 'update_item' => __( 'Update Our Product', 'our-products' ), 'view_item' => __( 'View Our Product', 'our-products' ), 'view_items' => __( 'View Our Products', 'our-products' ), 'search_items' => __( 'Search Our Product', 'our-products' ), 'not_found' => __( 'Not found', 'our-products' ), 'not_found_in_trash' => __( 'Not found in Trash', 'our-products' ), 'featured_image' => __( 'Featured Image', 'our-products' ), 'set_featured_image' => __( 'Set featured image', 'our-products' ), 'remove_featured_image' => __( 'Remove featured image', 'our-products' ), 'use_featured_image' => __( 'Use as featured image', 'our-products' ), 'insert_into_item' => __( 'Insert into Our Product', 'our-products' ), 'uploaded_to_this_item' => __( 'Uploaded to this Our Product', 'our-products' ), 'items_list' => __( 'Our Products list', 'our-products' ), 'items_list_navigation' => __( 'Our Products list navigation', 'our-products' ), 'filter_items_list' => __( 'Filter Our Products list', 'our-products' ), ); $args = array( 'label' => __( 'Our Product', 'our-products' ), 'description' => __( 'Our Products Section', 'our-products' ), 'labels' => $labels, 'menu_icon' => 'dashicons-grid-view', 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'author', 'comments', 'trackbacks', 'page-attributes', 'post-formats', 'custom-fields'), 'taxonomies' => array(), //'taxonomies' => array('categories'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'hierarchical' => true, 'exclude_from_search' => false, 'show_in_rest' => true, 'publicly_queryable' => true, 'capability_type' => 'post', ); register_post_type( 'ourproduct', $args ); } add_action( 'init', 'create_ourproduct_cpt', 0 ); 用于检索经过验证的输入,因此仅通过调用//our product category taxonomy function tr_create_my_taxonomy() { register_taxonomy( 'ourproduct-category', 'ourproduct', array( 'label' => __( 'Prod Category' ), 'rewrite' => array( 'slug' => 'prod-category' ), 'hierarchical' => true, ) ); } add_action( 'init', 'tr_create_my_taxonomy' ); 就应验证请求

答案 2 :(得分:0)

    //Try This 
    use App\Http\Requests\UpdatePlanRequest;

    public function store(UpdatePlanRequest $request)
        {
           $validatedData = $request->validated();
           dd('$validatedData');
           $profile = new Profile([
            'user_id'              => $request->get('user_id'),

        ]);
        $profile->save();
        echo $request->session()->flash('alert-success', 'Profile details Succefully Added!');
        return redirect('create')->with('success', 'Data saved!');

        }

Your route will be.

Route::get('profile','ProfileController@store');
Route::post('profile/create','ProfileController@store')->name('create');