Last active
August 10, 2021 02:56
-
-
Save herusdianto/bad89ea9d9ae73479172 to your computer and use it in GitHub Desktop.
Laravel Filter JSON Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "laravel/laravel", | |
"description": "The Laravel Framework.", | |
"keywords": ["framework", "laravel"], | |
"license": "MIT", | |
"require": { | |
"laravel/framework": "4.2.*" | |
}, | |
"autoload": { | |
"classmap": [ | |
"app/commands", | |
"app/controllers", | |
"app/models", | |
"app/database/migrations", | |
"app/database/seeds", | |
"app/tests/TestCase.php" | |
], | |
"psr-4": { | |
"Heru\\": "app/" | |
} | |
}, | |
"scripts": { | |
"post-install-cmd": [ | |
"php artisan clear-compiled", | |
"php artisan optimize" | |
], | |
"post-update-cmd": [ | |
"php artisan clear-compiled", | |
"php artisan optimize" | |
], | |
"post-create-project-cmd": [ | |
"php artisan key:generate" | |
] | |
}, | |
"config": { | |
"preferred-install": "dist" | |
}, | |
"minimum-stability": "stable" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/database/seeds/DatabaseSeeder.php | |
/** | |
* DatabaseSeeder | |
*/ | |
class DatabaseSeeder extends Seeder { | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
Eloquent::unguard(); | |
$this->call('KomentarTableSeeder'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/models/Komentar.php | |
/** | |
* Komentar | |
*/ | |
class Komentar extends Eloquent { | |
/** | |
* nama tabel | |
* | |
* @var string | |
*/ | |
protected $table = 'komentar'; | |
/** | |
* getter data komentar | |
* | |
* @return Collection | |
*/ | |
public static function dataKomentar() | |
{ | |
return self::latest()->get()->toArray(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Heru\Controllers\Api; | |
// app/controllers/Api/KomentarController.php | |
use BaseController; | |
use Komentar; | |
use Input; | |
use Response; | |
/** | |
* KomentarController | |
*/ | |
class KomentarController extends BaseController { | |
/** | |
* cari komentar berdasarkan user_id dan post_id | |
* | |
* @return Response | |
*/ | |
public function cari() | |
{ | |
// ambil semua data komentar | |
$komentar = Komentar::dataKomentar(); | |
// ambil input | |
$user_id = Input::get('user_id'); | |
$post_id = Input::get('post_id'); | |
// filter array komentar | |
$data = $this->filterKomentar($komentar, $user_id, $post_id); | |
return Response::json($data, 200); | |
} | |
/** | |
* filter array komentar | |
* | |
* @param array $komentar | |
* @param int $user_id | |
* @param int $post_id | |
* | |
* @return array | |
*/ | |
private function filterKomentar($komentar, $user_id, $post_id) | |
{ | |
// filter array menggunakan fungsi array_where dari laravel helper | |
$data = array_where($komentar, function($key, $value) use ($user_id, $post_id) | |
{ | |
// rubah string json dari kolom data ke object | |
$json = json_decode($value['data']); | |
// hanya ambil array dengan kriteria: | |
// property user_id dari object $json = input user_id dan | |
// property post_id dari object $json = input post_id | |
return $json->user_id == $user_id && $json->post_id == $post_id; | |
}); | |
// ambil nilai dari array data | |
return array_values($data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* KomentarTableSeeder | |
*/ | |
class KomentarTableSeeder extends Seeder { | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
Komentar::truncate(); | |
Komentar::create([ | |
'isi' => 'Komentar Pertama.', | |
'data' => json_encode(array( | |
'user_id' => 1, | |
'post_id' => 1 | |
)) | |
]); | |
Komentar::create([ | |
'isi' => 'Komentar Kedua.', | |
'data' => json_encode(array( | |
'user_id' => 1, | |
'post_id' => 1 | |
)) | |
]); | |
Komentar::create([ | |
'isi' => 'Komentar Ketiga.', | |
'data' => json_encode(array( | |
'user_id' => 1, | |
'post_id' => 2 | |
)) | |
]); | |
Komentar::create([ | |
'isi' => 'Komentar Keempat.', | |
'data' => json_encode(array( | |
'user_id' => 2, | |
'post_id' => 1 | |
)) | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/routes.php | |
/** | |
* api komentar route | |
*/ | |
Route::post('api/komentar', [ | |
'as' => 'api_komentar', | |
'uses' => 'Heru\Controllers\Api\KomentarController@cari' | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/database/migrations/tabel_komentar.php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
/** | |
* TabelKomentar | |
*/ | |
class TabelKomentar extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('komentar', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('isi', 30); | |
$table->text('data'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('komentar'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment