Reset
Insert a Record and Get Its ID
Program Info
Program Code
×

Insert a Record and Get Its ID

This query adds a new record to the contact table and returns the ID of the newly inserted record. It also generates a random employee ID and saves the given mobile number.

Footer

×

Insert a Record and Get Its ID

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
         $id = DB::table('contact')
        ->insertGetId(['id'=> mt_rand(11111,99999),'mobile'=>2222255555]);
        print_r($id);
    }
}

Footer

Insert Multiple Records into a Table
Program Info
Program Code
×

Insert Multiple Records into a Table

This query inserts two new records into the contact table. It generates a random ID for each record and stores the mobile number along with it.

Footer

×

Insert Multiple Records into a Table

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
         DB::table('contact')
        ->insert([
            ['id'=> mt_rand(11111,99999),'mobile'=>1234567890],
            ['id'=> mt_rand(11111,99999),'mobile'=>1111122222],
        ]);

    }
}

Footer

Insert Record and Ignore Duplicate ID
Program Info
Program Code
×

Insert Record and Ignore Duplicate ID

This query adds a new record to the contact table with ID 3. If a record with the same ID already exists, it ignores the insert without showing an error.

Footer

×

Insert Record and Ignore Duplicate ID

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
         DB::table('contact')->insertOrIgnore(['id'=> 3]);

    }
}

Footer

Join Employee and User Data
Program Info
Program Code
×

Join Employee and User Data

This query joins the employee table with the users table using emp_id and fetches the matching data from both tables.

Footer

×

Join Employee and User Data

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
        $data = DB::table('employee')->join('users','employee.emp_id','=','users.emp_id')->get();
        echo "<pre>";
        print_r($data->toArray());
    }
}

Footer

Join Employee and User Data 2
Program Info
Program Code
×

Join Employee and User Data 2

This query joins the employee and users tables using the employee ID and fetches the employee's email and address along with the user's display name and username.

Footer

×

Join Employee and User Data 2

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
        $data = DB::table('employee')
        ->join('users','employee.emp_id','=','users.emp_id')
        ->select('employee.email','employee.address','users.displayName','users.username')
        ->get();
        echo "<pre>";
        print_r($data->toArray());
    }
}

Footer

Laravel Migration Creating a Table with Different Column Types
Program Info
Program Code
×

Laravel Migration Creating a Table with Different Column Types

This migration shows how to create a Laravel database table using different column types, default values, indexes, JSON fields, timestamps, soft deletes, polymorphic relationships, and foreign keys.

Footer

×

Laravel Migration Creating a Table with Different Column Types

<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('migration_examples', function (Blueprint $table) {

            // Primary Key
            $table->id();

            // Basic String Fields
            $table->string('first_name');
            $table->string('email')->unique();
            $table->string('password')->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('queue')->index();

            // Fixed Length / Character Field
            $table->char('product_id', 36)->nullable();

            // Different Text Types
            $table->text('note')->nullable();
            $table->tinyText('short_description')->nullable();
            $table->longText('long_description')->nullable();

            // Integer Types
            $table->tinyInteger('status')->default(0)
                ->comment('0 = Inactive, 1 = Active');

            $table->smallInteger('page_number')->nullable();
            $table->mediumInteger('validity')->nullable();
            $table->integer('country_id')->default(0);
            $table->integer('position')->nullable();
            $table->integer('expires_in')->nullable()
                ->comment('Time in seconds');

            $table->bigInteger('external_id')->nullable();

            // Unsigned Integer Types
            $table->unsignedTinyInteger('attempts')->default(0);
            $table->unsignedInteger('order_column')->nullable()->index();
            $table->unsignedBigInteger('file_size')->nullable();

            // UUID Fields
            $table->uuid('faq_category_id')->nullable();
            $table->uuid('parent_id')->nullable();

            // Decimal Fields
            $table->decimal('longitude', 13, 7)->nullable();
            $table->decimal('latitude', 13, 7)->nullable();
            $table->decimal('price', 20, 2)->default(0.00);

            // Boolean Fields
            $table->boolean('payment_status')->nullable();

            // Enum Field
            $table->enum('api_mode', ['test', 'live'])
                ->default('test');

            // JSON Fields
            $table->json('manipulations')->nullable();
            $table->json('test_credential')->nullable();

            // Date and Time Fields
            $table->date('expire_date')->nullable();
            $table->dateTime('expiration_datetime')->nullable();

            // Timestamp Fields
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('email_verified_at')->nullable();

            // Laravel Authentication Token
            $table->rememberToken();

            // Soft Delete
            $table->softDeletes();

            // Standard Laravel Timestamps
            $table->timestamps();

            // Polymorphic Relationship Example
            $table->nullableMorphs('subject');

            // Foreign Key Example
            $table->foreignId('page_category_id')
                ->nullable()
                ->constrained('page_categories')
                ->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('migration_examples');
    }
};

Footer

Laravel User Logout Using Session Flush
Program Info
Program Code
×

Laravel User Logout Using Session Flush

This code logs the user out by clearing all session data and then redirects the user to the home page.

Footer

×

Laravel User Logout Using Session Flush

<?php

namespace App\Http\Controllers;

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


class Dashboard_Controller extends Controller
{
    public function logout()
    {
        Session::flush();
        return redirect('/');

    }

}

Footer

Remove Unique Constraint from Users Email
Program Info
Program Code
×

Remove Unique Constraint from Users Email

This migration removes the unique constraint from the email column in the users table, allowing multiple users to have the same email address. The down() method adds the unique constraint back if the migration is rolled back.

Footer

×

Remove Unique Constraint from Users Email

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropUnique(['email']); // Remove unique constraint
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->unique('email'); // Add unique constraint back if needed
        });
    }
};

Footer

Right Join Between Employee and Users
Program Info
Program Code
×

Right Join Between Employee and Users

This query gets all records from the users table and matching records from the employee table using emp_id.

Footer

×

Right Join Between Employee and Users

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
        $data = DB::table('employee')
        ->rightjoin('users','employee.emp_id','=','users.emp_id')
        ->get();
        echo "<pre>";
        print_r($data->toArray());

    }
}

Footer

Right Join Users with Employee
Program Info
Program Code
×

Right Join Users with Employee

This query returns all employees and matching user records. If an employee does not have a matching user record, the employee is still included in the result.

Footer

×

Right Join Users with Employee

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Query_Controller extends Controller
{
    public function index()
    {
        $data = DB::table('users')
        ->rightjoin('employee','employee.emp_id','=','users.emp_id')
        ->get();
        echo "<pre>";
        print_r($data->toArray());

    }
}

Footer

Showing page  7  of  8  (Total  77 records)
« Prev  1 ... 5  6  7  8  Next »
×
Name : Krishna Verma    |    Father's Name : Mr. Satish Kumar    |    Email ID : krishnaverma28081997@gmail.com    |    Religion : Hindu    |    Address : Dayampur, kanker khera, Meerut Cantt (250001), UP    |    Date of Birth : 28 August 1997    |    Marital Status : Non-Married    |    Place of Birth : Dayampur Kanker Khera Meerut Cantt.    |    Gender : Male    |    Age : 28    |    Contact : 9520335394    |    Father's Occupation : Sports man    |    Mother's Name : Smt. Kuntesh Devi    |    Mother's Occupation : Homemaker    |    Number of Brothers : 1    |    Number of Sisters : 1    |    Current Residence : Dayampur kanker khera meerut cantt    |    Family Type : Nuclear Family    |    Family Name : verma    |    Height : 5' 6"    |    Weight : 70 kg    |    Complexion : Fair    |    Body Type : Athletic    |    Hair Color : Black    |    Health Status : Good    |    Any Physical Disability : No    |    Highest Qualification : B.Tech (CSE)    |    Master Skill : PHP Laravel    |    Total Projects : 40 +    |    Total Skills : 25 +    |    Languages Known : Hindi and English    |    Website : krishnaportfolio.in    |    Siblings : 3    |    Nationality : Indian    |    Total Experience : 5 Years    |    LinkedIn : https://www.linkedin.com/in/krishna-verma-840b71356/    |    Total Programs : 10000    |    Total Projects : 40    |    Total Skills : 25    |    Total Experience : 5    |    Total DSA Problems : 200    |    Total Served Companies : 2    |    Leetcode : https://leetcode.com/u/krishnavermadeveloper/    |    Naukri : https://www.naukri.com/mnjuser/profile?id=&altresid