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.
×
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);
}
}
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.
×
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],
]);
}
}
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.
×
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]);
}
}
This query joins the employee table with the users table using emp_id and fetches the matching data from both tables.
×
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());
}
}
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.
×
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());
}
}
Laravel Migration Creating a Table with Different Column Types
Laravel Migration Creating a Table with Different Column Types
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.
×
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');
}
};
This code logs the user out by clearing all session data and then redirects the user to the home page.
×
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('/');
}
}
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.
×
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
});
}
};
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.
×
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());
}
}