我是Laravel的新手,我当前使用的是5.7版。并试图将一些表格数据放入mysql表中,我收到此错误。
Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException 没有消息
但是我不知道我要怎么做。如果可以,请你帮助我。请在下面查看我的代码。
我的路线:
Route::get('/invo_admin/create_new_offer', 'CreatenewofferController@index')->name('create_new_offer');
我还有一个名为admin的子文件夹,在该文件夹中我具有仪表板的视图。
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>
答案 0 :(得分:1)
将此添加到您的路线。
Route::post('/invo_admin/create_new_offer', 'CreatenewofferController@store')->name('create_new_offer');
它接受发布请求并将其传递给控制器中的“存储”方法。
答案 1 :(得分:0)
所以我照顾好了。
我的路线全乱了;例如,从不使用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应用程序中,但无论出于什么原因,前端和后端具有相同的名为category的控制器,这将解决该问题……无论如何,这就是您要放入表单操作的内容
"admin.vendors.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)
{
//
}
我希望这会有所帮助。