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

Alewa's avatar
Level 2

laraval store

I have a TbookingController in a User folder and the another TbookingController in an Admin folder and I have created route for them but when I want to store something with the TbookingController in User folder, I don't get any error but the things are not inserted into the database. Please can someone help?

0 likes
32 replies
munazzil's avatar

Use something like below query for getting your data from correct table and can insert ,

      $admin = $request->all();
      $admin->save();
Nakov's avatar

Please can you share the code that you have? Routes, controller functions all of it will help you to get the answer you are looking for.

Alewa's avatar
Level 2

I am new on this platform, so how do i share the codes?

Nakov's avatar

@alewa you edit your question above and put a code within three back ticks (`).

Alewa's avatar
Level 2

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

---------------------The User TbookingController code---------------------------------

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Trip;
use App\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();
        $post->trips()->sync($request->trips);

        return redirect(route('tbooking'));
    }
}
Nakov's avatar

@alewa so what I can think of for not saving is in your Tbooking model class do you have the fields set in the $fillable array. Something like this:

protected $fillable = ['name', 'email', 'gender', 'mobile_one', 'mobile_two', 'status'];
1 like
Alewa's avatar
Level 2

so please how do i write the code in the Tbooking model?

Nakov's avatar

You copy and paste what I gave you above. If you don't have that array there, just add it. That's it.

1 like
Alewa's avatar
Level 2

i have done it but still not working

Nakov's avatar

Can you please try checking what you get after the saving point, so add this in your controller:

// after this line
$tbooking->save();

dd($tbooking);

and share the result here.

Alewa's avatar
Level 2

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

}
Alewa's avatar
Level 2

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

}
Nakov's avatar

@alewa btw, I don't know how don't you get an error when you use $post which is never initialized.

Can you try changing this line:

$post->trips()->sync($request->trips);

with this:

$tbooking->trips()->sync($request->trips);
Alewa's avatar
Level 2

i have change it and is still not working

Nakov's avatar

@alewa have you tried my comment above: dd($tbooking) after saving, just to check if you ever get to that line, because you might not be hitting this endpoint at all, and we are checking something else.

Alewa's avatar
Level 2

i have try it, but still not working

Nakov's avatar

So the answer is you are not hitting this endpoint at all. Make sure that your routes do not override. Meaning you already have this route:

  Route::post('tbooking','TbookingController@store');

But then as you said in your Admin namespace if you have the same route:

  Route::post('tbooking','TbookingController@store');

Then this will hit whichever route is below the other route in your web.php file. So make sure you add something to make them different, for example in the User group it can be:

  Route::post('users\tbooking','TbookingController@store');
Alewa's avatar
Level 2

------------------error message -----------------------------

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

-------------------error message--------------------------------

 /**
     * Throw a method not allowed HTTP exception.
     *
     * @param  array  $others
     * @return void
     *
     * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
     */
    protected function methodNotAllowed(array $others)
    {
        throw new MethodNotAllowedHttpException($others);
    }
 
    /**
     * Get routes from the collection by method.
     *
     * @param  string|null  $method
     * @return array
     */
    public function get($method = null)
    {
        return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []);
    }
 
    /**
     * Determine if the route collection contains a given named route.
Nakov's avatar

@alewa are you using POST as a method="post" on your form? Have you added the @csrf in your form as well?

Alewa's avatar
Level 2

-------------- The form -------------------------------

<form action="tbooking" method="post" class="form">
          {{ 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>
Nakov's avatar

Did you changed the route as I suggested and forgot to change the action on the form?

Try using action="{{ url('users/tbooking') }}" or whatever your endpoint is for the POST route.

Alewa's avatar
Level 2

i have try it but still not working

Alewa's avatar
Level 2

my tbooking.blade.php file is not in a user folder.

munazzil's avatar

You can define in your user model as like below,

    protected $table = 'tbooking';
Alewa's avatar
Level 2

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

}

please where do i put the protected $table = 'tbooking';

the database table name is tbookings

Nakov's avatar

@alewa Please don't forget the fillable because it will never store the values, that's used for mass-assignment protection.

Your model should be this one:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tbooking extends Model
{
    // you can put this, but you can also avoid it, because Laravel will use the plural form of the model name anyway.
    protected $table = 'tbookings';
 
    protected $fillable = ['name', 'email', 'gender', 'mobile_one', 'mobile_two', 'activity', 'date', 'status'];

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

}
1 like
Next

Please or to participate in this conversation.