Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

Laravel Controller

I have a TbookingController in User folder and another TbookingController in Admin folder but when I want to insert data into database using TbookingController in User folder, it does not work because it is only using the TbookingController in Admin folder. Please any help?

--------------------------------The Error------------------------------

Method 
App\Http\Controllers\Admin\TbookingController::store does not exist.

-------------------------------Tbooking Route---------------------------

Route::group(['namespace' => 'User'],function(){
  Route::get('trip','TripController@show')->name('trip');
  Route::get('tbooking','TbookingController@create')->name('tbooking');
  Route::post('tbooking','TbookingController@store')->name('tbooking.store');
});

----------------------------TbookingController in User Folder------------

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Model\user\Trip;
use App\Model\user\Tbooking;
use App\Http\Controllers\Controller;
use App\Http\Controllers\User\TbookingController as UserTbookingController;

class TbookingController extends Controller
{
    public function create()
    {
        $trips = Trip::all();
        return view('tbooking',compact('trips'));
    }

    public function store(Request $request)
    {
        $this->validate($request,[
          'name' => 'required',
          'email' => 'required',
          'gender' => 'required',
          'mobile_one' => 'required',
          'mobile_two' => '',
          'activity' => 'required',
          'date' => 'required',
        ]);
        $tbooking = new Tbooking;
        $tbooking->name = $request->name;
        $tbooking->email = $request->email;
        $tbooking->gender = $request->gender;
        $tbooking->mobile_one = $request->mobile_one;
        $tbooking->mobile_two = $request->mobile_two;
        $tbooking->status = $request->status;
        $tbooking->save();
        $tbooking->trips()->sync($request->trips);

        return redirect(route('tbooking'));
    }
}

-----------------TbookingController in Admin Folder------------------------

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TbookingController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
      $this->middleware('auth:admin');
      $this->middleware('admin');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

---------------------------Tbooking form---------------------------

<form action="{{ route('tbooking.store') }}" method="post">
          {{ csrf_field() }}
          <div class="form-group col-md-12">
            <input type="text" name="name" class="form-control" placeholder="Name" required>
          </div>
          <div class="form-group col-md-12">
            <input type="email" name="email" class="form-control" placeholder="Email" required>
          </div>
          <div class="form-group col-md-12">
            <select name="gender" id="gender" class="form-control" required>
              <option value="">Select Gender</option>
              <option value="Male">Male</option>
              <option value="Female">Female</option>
            </select>
          </div>
          <div class="form-group col-md-12">
            <input type="text" name="mobile" class="form-control" placeholder="Mobile Number" required>
          </div>
          <div class="form-group col-md-12">
            <input type="text" name="mobile2" class="form-control" placeholder="Another Mobile Number">
          </div>
          <div class="form-group col-md-12">
            <select name="trips" id="country" class="form-control">
        @foreach ($trips as $trip)
              <option value="{{ $trip->id }}">{{ $trip->title }}</option>
        @endforeach
            </select>
          </div>
          <div class="form-group col-md-12">
            <select name="activity" id="activities" class="form-control" required>
              <option value="">Activity</option>
              <option value="Tour">Tour</option>
              <option value="Visit">Visit</option>
        <option value="Business">Business</option>
              <option value="Stay">Stay</option>
            </select>
          </div>
          <div class="form-group col-md-12">
            <label for="date">Travel Date</label>
            <input type="date" name="date" class="form-control" required>
          </div>
          <div class="form-group col-md-12">
            <button type="submit" class="btn btn-primary">Register</button>
          </div>
        </form>

--------------------------------Tbooking Model---------------------------------------

<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class Tbooking extends Model
{
    public function trips()
    {
        return $this->belongsToMany('App\Model\user\Trip','tbooking_trips')->orderBy('created_at','DESC')->paginate(5);
    }
}
0 likes
27 replies
tykus's avatar

You don't show the routes registered for the Admin TBookingController; are you registering a Route::resource which is then taking the tbooking.store route name?

Alewa's avatar
Level 2

yes but i will not store anything using the TbookingController in Admin Folder

//Admin Routes
Route::group(['namespace' => 'Admin'],function(){
  // Trip Routes
  Route::resource('admin/trip','TripController');
  // Tbooking Routes
  Route::resource('admin/tbooking','TbookingController');
});
tykus's avatar

Your resource route definition is generating the same route names as you have defined manually for the User namespace Controller. You can either (i) change the manual route names, or (ii) give the Admin resource routes an as option.

Route::resource('admin/tbooking','TbookingController', ['as' => 'admin']); // admin.tbooking.store etc

There is a third option, but it doesn't solve the underlying issue, which is to provide either an only or except option for the actions that are used /. not used respectively.

Route::resource('admin/tbooking','TbookingController', ['except' => 'store']);

All of the other route names will still collide however.

Alewa's avatar
Level 2

i have change my admin TbookingController route to

Route::resource('admin/tbooking','TbookingController',['as' => 'admin']);

The error is not showing but noting is stored in the tbookings database.

AddWebContribution's avatar

You should try this:

Route::resource('admin/tbooking','TbookingController', ['except' => 'store']);
tykus's avatar

First, be sure that you are getting to the store method in the User\TBookingController, then be sure that your Validation is not failing.

Alewa's avatar
Level 2

Please still not inserting the data into the tbookings database

tykus's avatar

Ok, your store method will redirect to the create view in one of two ways (i) success a request was validated and a new records created, or (ii) failure request validation failed and no record was created. Given that there is no records persisted, I would suspect that you are getting a validation failure.

Temporarily, comment out the validation part, and see if you can either get a record persisted, or get a QueryException.

Longer term, your create view should display validation errors, e.g.

<div class="form-group col-md-12">
    <input type="text" name="name" class="form-control" placeholder="Name" required>

    @error('name')
        <span>{{ $message }}</span>
    @enderror
</div>
Neven's avatar

Hi, Route::resource will create all the HTTP request type routes (POST, GET, PUT and DELETE) for index, create, store, update and destroy method on your Controller. If in your Admin\TbookingController dont have the store method, Laravel will return an error.

First, since you separate the HTTP request type in two Controllers, if i were you i will separate my routes and dont use Route::resource.

Secondly, if you choose to use Route::resource, you will need to create an empty store method in the Admin\TbookingController and place the Route::resource for User\TbookingController above the routes for the Admin\TbookingController in your routes file.

Personnaly i prefer the first one. I hope i have hepded, Good luck and good day,

Cordially, Neven

tykus's avatar

Just to be clear in case my earlier post was misunderstood by @neven you can use Route::resource, but you need to force modification of the default generated route names to avoid collisions. And removing the store action is not a viable solution because there are other routes with similar names. Placing one Route::resource definition above the other has no effect on route names which is the nub of this issue.

2 likes
Neven's avatar

@tykus Exact, but i my opinion there are too many exception on his Route::ressource. There is not only the store method, so i proposed to cut the Route::ressource. And so it will be easeaier to read the route file for him/her.

I hope i dont make a mistake.

Cordially, Neven

Alewa's avatar
Level 2

please you show me example of what you have posted because i don't understand, should I change the User folder TbookingController name? or?

Neven's avatar

@alexa try the command php artisan route:list. You will obtain the list of all your routes. You have a store route for your User\TbookingController and for the Admin\TbookingController ?

Your problem is, your Route::post (store method) of your User\TbookingController is not use and its the Route::post of your Admin\TbookingController who is called but the store method in Admin\TbookingController doesn't exist. You have an error :)

Route::resource create all the routes for your controller but you don't need all of them. So dont write Route::resource but each route one by one. Six in total.

For User\TbookingController the routes GET (create) and POST (store). For Admin\TbookingController the routes GET (index), GET(edit), PUT(update), DELETE (destroy).

tykus's avatar

The issue is not Controller class names, it is, I expect, that you have validation errors that are not being displayed. I am specifically talking about in the mobile_one (required) and mobile_two rules which appear to have no matching input names - the form has mobile and mobile2

Quicky and temporarily, you can dump $errors in the view using:

@dump($errors->all())

If you are getting validation errors, it will explain why nothing is persisted in the database.

Longer term, add the @error directive like I showed above for every input that is validated according to the rules.

Neven's avatar

@tykus the error is

 Method App\Http\Controllers\Admin\TbookingController::store does not exist.​ 
tykus's avatar

@neven the error was Method App\Http\Controllers\Admin\TbookingController::store does not exist.the OP replied earlier:

The error is not showing but noting is stored in the tbookings database.

The thread has moved on...

Nakov's avatar

@alewa and after this https://laracasts.com/discuss/channels/laravel/laraval-store thread you are again having problems with the same thing. Can you please watch some tutorials, tons of free videos, or just subscribe at least for a month here on Laracasts, read the documentation and try to find the way out. Or make your project open-source on Github, share the link here and someone will help you. You are spending toooo much time on this, and also spending every one elses time trying to figure out what help you need.

Sorry if this sounds rude, don't have anything against you.

2 likes
Alewa's avatar
Level 2

thanks. but can you please send me your email so i will send the project to you.

Alewa's avatar
Level 2

I am having a failure request validation because after even changing the TbookingController name at User folder, I am still not getting any errors but no data is inserted into the database

tykus's avatar

Your form has the following inputs

| Input Name        | Validation Rule           |
| ----------------- |:-------------------------:|
| name              | 'name' => 'required'      |
| email             | 'email' => 'required'     |
| gender            | 'gender' => 'required'    |
| mobile            | 'mobile_one' => 'required'| <<<<<<<<<
| mobile2           | 'mobile_two' => ''        |
| trips             |                           |
| activity          | 'activity' => 'required'  |
| date              | 'date' => 'required'      |

Do you see why there is a validation error?

Nakov's avatar

The problem is because you don't have a $fillable array in your model, that's used for mass-assignment protection, hence the reason why the fields are ignored.

So your model should be this:

<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class Tbooking extends Model
{
    protected $fillable = ['name', 'email', 'gender', 'mobile_one', 'mobile_two', 'status'];

    public function trips()
    {
        return $this->belongsToMany('App\Model\user\Trip','tbooking_trips')->orderBy('created_at','DESC')->paginate(5);
    }
}

Please go watch some videos before you continue working, you need to learn the basics first. I won't share my email address just like that. The discussion forums are used for helping.

1 like
tykus's avatar

@nakov mass assignment is not an issue - the OP is not mass assigning.

Snapey's avatar

omg so many people off topic....

Alewa's avatar
Level 2

@tykus i don't see the error you are showing me, can you please show me the solution?

tykus's avatar

@alewa You visit the form, fill out all of the required fields, and submit the form; what do you see / where are you?

Please or to participate in this conversation.