laravel methodnotallowedhttpexception no message-尝试插入mysql表

kpbwa7wx  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(361)

我对拉威尔很陌生,我现在用的是5.7版。当我试图将一些表单数据放入mysql表时,我得到了这个错误。
symfony\component\httpkernel\exception\methodnotallowedhttpexception无消息
但是我不知道我出了什么问题。如果可以的话,请帮帮我。请看下面我的代码。
我的路线:

Route::get('/invo_admin/create_new_offer', 'CreatenewofferController@index')->name('create_new_offer');

我还有一个名为admin的子文件夹,其中有我的 Jmeter 板视图。

Route::resource('admin', 'CreatenewofferController');

我的型号:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Offers extends Model
{
protected $fillable =[
    'offer_name', 
    'offer_image', 
    'offer_discription', 
    'offer_vendor', 
    'offer_reward_amount', 
    'offer_limit', 
    'offer_duration',
    'offer_status'
];
}

我的控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Offers;

class CreatenewofferController extends Controller
{
 /**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $title = 'this is a title';
    return view('admin.create_new_offer')->with('title',$title);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('admin.create_new_offer');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request,[
        'offer_name' => 'required', 
        'offer_image' => 'required', 
        'offer_discription' => 'required', 
        'offer_vendor' => 'required', 
        'offer_reward_amount' => 'required', 
        'offer_limit' => 'required', 
        'offer_duration' => 'required',
        'offer_status' => 'required'
    ]);
    $offers = new Offers([
        'offer_name' => $request->get('offer_name'), 
        'offer_image' => $request->get('offer_image'), 
        'offer_discription' => $request->get('offer_discription'), 
        'offer_vendor' => $request->get('offer_vendor'), 
        'offer_reward_amount' => $request->get('offer_reward_amount'), 
        'offer_limit' => $request->get('offer_limit'), 
        'offer_duration' => $request->get('offer_duration'),
        'offer_status' => $request->get('offer_status')
    ]);
    $offers->save();
    return redirect()->route('admin.create_new_offer')->with('success', 'You have successfully added a new offer');
} 
}

我的观点:

<form role="form" method="POST" action="{{ url('invo_admin/create_new_offer') }}">
            {{csrf_field()}}
        <!-- text input -->
        <div class="form-group">
            @if(count($errors) > 0)
                <ul>
                @foreach ($errors ->all as $error)
                    <li class="text-danger">{{error}}</li>
                @endforeach
                </ul>
            @endif
            @if(\Session::has('success'))
                <p>{{\Session::get('success')}}</p>
            @endif
            <label>Name</label>
            <input type="text" class="form-control" name="offer_name" placeholder="Offer Name">
        </div> 
</form>
zfciruhq

zfciruhq1#

把这个加到你的路线上。

Route::post('/invo_admin/create_new_offer', 'CreatenewofferController@store')->name('create_new_offer');

它接受post请求并将其传递给控制器中的“store”方法。

0h4hbjxa

0h4hbjxa2#

所以我处理好了。
我的路线混乱不堪;例如,从不使用get或post路由,始终使用追索路由
不好的

Route::get('admin/vendors', 'VendorController@index')->name('whatever_name');

Route::post('admin/vendors','VendorController@index')->name('whatever_name');

非常好:

Route::resource('admin/vendors', 'VendorController', ['as'=>'admin']);

正确路线的最后一部分。。。

['as'=>'admin']

被称为前缀,在想要区分应用程序的两个部分时使用,例如,假设您有一个

front facing website

和一个

admin panel

在同一个laravel应用程序中,但无论出于何种原因,前端和后端都有相同的控制器名为categories,这将解决该问题。。。不管怎么说,这是你在形式行动中要做的

"admin.vendors.store"

“admin”是前缀,“vendor”是路由,最后是位于控制器中的函数中的“store”。看起来像这样。

public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

我希望这有帮助。

相关问题