Delroy Brown
Delroy Brown

Reputation: 29

Laravel MethodNotAllowedHttpException No message - trying to Incert into mysql table

I'm very new to Laravel, im currently using version 5.7. and as im trying to put some form data into mysql table, im getting this error.

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

However I have no idea what i'm going wrong. please help me if you can. Please see my code below.

My Routes:

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

Also I have a sub folder called admin where i have my views for the dashboard.

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

My Model:

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'
];
}

My Controller:

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');
} 
}

My View:

<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>

Upvotes: 0

Views: 67

Answers (2)

Delroy Brown
Delroy Brown

Reputation: 29

So I got this taken care of.

my routes where all messed up; for example, Never use GET or POST routes, always use Recourse routes

NOT GOOD

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

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

VERY GOOD:

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

the last part of the correct route...

['as'=>'admin']
Is called a prefix and is used when wanting to differentiate between two section of your application, for example imagine if you had a

front facing website
and an
admin panel

In the same Laravel application, but for whatever reason the front end and back-end had the same controller named categories, this will solve that issue... anyhow, here is what you would put into a form action

"admin.vendors.store"

The 'admin" is the prefix, and the "vendor" is the route, and lastly the "store" in the function that is located in your controller. it looks like this.

public function create()
{
    //
}

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

I hope this helps.

Upvotes: 0

Gaurav Khambhala
Gaurav Khambhala

Reputation: 11

Add this to your routes.

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

It accepts post requests and pass it on to "store" method in controller.

Upvotes: 1

Related Questions