【问题标题】:Laravel MethodNotAllowedHttpException No message - trying to Incert into mysql tableLaravel MethodNotAllowedHttpException 无消息 - 尝试插入 mysql 表
【发布时间】:2018-12-07 06:56:46
【问题描述】:

我对 Laravel 很陌生,我目前使用的是 5.7 版。当我试图将一些表单数据放入 mysql 表时,我得到了这个错误。

Symfony\Component\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>

【问题讨论】:

  • 看起来您正在使用Route::get 来处理发布请求。您需要创建一个与您的store 控制器方法相对应的发布路由。
  • 我试过了,但它不起作用,你介意给出一个代码示例,以便我确保我这样做正确吗?

标签: php mysql laravel-5.7


【解决方案1】:

将此添加到您的路线中。

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

它接受发布请求并将其传递给控制器​​中的“存储”方法。

【讨论】:

  • 这已经停止了错误,但是仍然没有将任何内容添加到数据库中。
  • 是的。你下面的评论很好。我的意思是定义路由以接受发布请求。
【解决方案2】:

所以我解决了这个问题。

我的路线都搞砸了;例如,永远不要使用 GET 或 POST 路由,始终使用 Recourse 路由

不好

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']
称为前缀,用于区分应用程序的两个部分,例如,假设您有一个

正面网站
和一个
管理面板

在同一个 Laravel 应用程序中,但无论出于何种原因,前端和后端具有相同的控制器,命名为类别,这将解决该问题...无论如何,这是您要放入表单操作的内容

“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)
{
    //
}

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    相关资源
    最近更新 更多