Eloquent with JSON Data Columns Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use App\Models\Test;
use Illuminate\Http\Request;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// Read first record from database
// $test = Test::find(1);
// return $test->meta_data;
// Read all records from database
// $test = Test::get();
// return $test;
// Order all records based on name
// $test = Test::orderBy("meta_data->name")->get();
// return $test;
// Read only "name" value of any particular record from database
// $test = Test::find(1);
// return $test->meta_data["name"];
// Read only "address" value of any particular record from database
// $test = Test::find(3);
// // return $test->meta_data["address"];
// return $test->meta_data["address"]["city"];
// $test = Test::where("meta_data->name","Katrina Kaif")->get();
// return $test;
// $test = Test::where("meta_data->name","LIKE","He%")->get();
// return $test;
// we can use "whereJsonContains()" method instead of "where" method as shown below :
// $test = Test::whereJsonContains("meta_data->name","Hello World")->get();
// return $test;
// Usage of "whereJsonLength()" method :
// $test = Test::whereJsonLength("meta_data->name",1)->get(); //showing results in which "name" has value
$test = Test::whereJsonLength("meta_data->name", 0)->get(); //showing results in which "name" has no value
return $test;
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
// 1st way for inserting JSON data in database
// $test = new Test();
// $test->meta_data = [
// "name" => "Hello World",
// "email" => "helloworld345@gmail.com",
// "mobile_number" => "4566983321"
// ];
// $test->save();
// 2nd way for inserting JSON data in database
// $test = Test::create([
// "meta_data" => [
// "name" => "John Abraham",
// "email" => "john@gmail.com",
// "mobile_number" => "7699832210"
// ]
// ]);
// Insert another JSON data in database
// $test = Test::create([
// "meta_data" => [
// "name" => "Katrina Kaif",
// "email" => "katrina@gmail.com",
// "mobile_number" => "9322458876",
// "address" => [
// "street" => "#123 KK Road",
// "city" => "Mumbai",
// "country" => "India"
// ]
// ]
// ]);
// Update any particular record First way
// $test = Test::where("id",2)->update([
// "meta_data->name" => "Shahid Kapoor"
// ]);
// Updating nested JSON data record value in database
// $test = Test::where("id",3)->update([
// "meta_data->address->city" => "Delhi"
// ]);
// Update any particular record Second way
// $test = Test::find(2);
// $test->meta_data["name"] = "John Abraham";
// $test->save();
// Delete record from database
// $test = Test::find(2);
// $test->meta_data = collect($test->meta_data)->forget("email"); //"forget()" method will remove Array's key
// $test->save();
$test = Test::where("id", 2)->update([
"meta_data->email" => "john@gmail.com"
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
// $test = Test::find(2);
// $test->meta_data = collect($test->meta_data)->forget("email"); //"forget()" method will remove Array's key
// $test->save();
}
}
Above File is app\Http\Controllers\TestController FileBelow File is app\Models\Test File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
public $timestamps = true;
protected $table = "tests";
protected $fillable = ["meta_data"];
protected $casts = [
"meta_data" => "json"
// "meta_data" => AsArrayObject::class
];
}
Below File is database\migrations\2024_12_17_111118_create_tests_table File
<?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::create('tests', function (Blueprint $table) {
$table->id();
$table->json("meta_data")->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tests');
}
};
Below File is routes\web File
.png)
.png)
.png)
Comments
Post a Comment