简体   繁体   中英

Laravel form route not defined

I am trying to send a route the old way, without using Blade's {{}} tags. I am encountering a problem, because the framework throws my route as not defined. Can someone help me?

This is my form tag:

<form method="POST" action="{{ route('companyStore') }}">

My route

Route::post('companyStore', 'CompanyController@store');

My controller (the function name might help you undertand)

public function store(Request $request){

        $company_name = $request->input('companyname');
        $company_sector = $request->input('companyname');
        $company_address = $request->input('companyaddress');
        $company_phone = $request->input('companyphone');
        $company_website = $request->input('companywebsite');
        $company_representative = Auth::user()->id;

        Company::create([
            'name' => $company_name,
            'sector' => $company_sector,
            'address' => $company_address,
            'phone' => $company_phone,
            'website' => $company_website,
            'representative_id' => $company_representative
        ]);

         $company = Company::where('representative_id', $company_representative)->first();
         User::where('id', $company_representative)->update(array('company_id' => $company->id));

         return redirect('/admin/home');
    }

The error is always:

Route [companyStore] not defined. (View: 

When you use the route helper, it expects a named route. So define your route as this:

Route::post('companyStore', 'CompanyController@store')->name('companyStore');

or use:

<form method="POST" action="{{ url('/companyStore') }}">

or use:

<form method="POST" {{ action('CompanyController@store') }}>

You can define a route.

Route::post('companyStore', 'CompanyController@store')->name('companyStore');

and use this one:

<form method="POST" action="{{ route('companyStore') }}">

I don't know why @nakov has propsed {{ url('/companyStore') }}

Just Change

FORM

Route::post('companyStore', 'CompanyController@store');

TO

Route::post('companyStore', 'CompanyController@store')->name('companyStore');

Will just work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM