Home > front end >  Laravel Query Builder hard coded data is not inserted with post method
Laravel Query Builder hard coded data is not inserted with post method

Time:10-29

I am trying to insert hard coded data with QueryBuilder insertGetId() method.

my code is-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{
    public function addStudent()
    {
        $foreign_key = DB::table('students')->insertGetId([
            'id' => 'stu-000002',
            'name' => 'Ahsan',
            'email' => '[email protected]',
        ]);

        echo $foreign_key;
    }
}

My migration file is-

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->string('id', 30)->primary();
            $table->string('name', 100);
            $table->string('email', 100)->unique();
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
};

My route is -

Route::post('/add-student', [StudentController::class, 'addStudent']);

But result is -

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.

But when I try to insert data with get method like-

Route::get('/add-student', [StudentController::class, 'addStudent']);

Data has been inserted . But primary key has been retrived 0 as primary key is custom string.

How can I solve this problem. Thank you.

CodePudding user response:

There are two problems with what you're doing:

Problem 1:

The first is the MethodNotAllowedException. I guess you're trying to use a GET request on a POST URL. This won't work, because Laravel blocks the 'wrong' method.

Use POST when you have data to submit (or if you really want to stick to the 'use post when saving'-creed use a form). Use GET when you want to access an URL.

Problem 2

According to the API (This one) insertGetId returns an integer which is the ID. Since your ID's are strings, you can't use that method.

A solution to that problem would be to change the code like this:

public function addStudent()
{
    $student_id = 'stu-000002'

    $insert = DB::table('students')->insert([
        'id' => $student_id,
        'name' => 'Ahsan',
        'email' => '[email protected]',
    ]);

    if ( ! $insert ) {
        return false;
    }

    echo $student_id;
}

The insert method returns a boolean. Leveraging that you can check whether or not the entry was inserted. If it is, your ID should be good.

CodePudding user response:

run this command in terminal:

php artisan route:cache

So what this command does is, it registers new routes or gives an error if there is something wrong with your route file.

  • Related