Skip to content

Commit

Permalink
finish radius search, fix #64
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu1997 committed Sep 2, 2019
1 parent d23b73a commit 8710e69
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
56 changes: 33 additions & 23 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use App\User;

use App\Spot;
Expand Down Expand Up @@ -85,35 +87,43 @@ public function spotSearch(Request $request)
public function spotInfo($spotId)
{
// return with info and comments
$spotInfo = Spot::where('id', $spotId)->get();
if ($spotInfo->isEmpty()) {
$spotInfo = Spot::where('id', $spotId)->get();
if ($spotInfo->isEmpty()) {
return response()->json([
'code' => 404,
'message' => 'this spot does not exist'
]);
} else {
$spotComment = Spot::find($spotId)->Comments()->latest()->get();
$spotLocation = $spotInfo->pluck('location')->first();
$shopNearby = Shop::where('location', $spotLocation)
->inRandomOrder()
->take(5)
->select([
'id',
'name',
'county',
'district',
'img1'
])
// ->first()
->get();
$spotComment = Spot::find($spotId)->Comments()->latest()->get();
$lat = $spotInfo->pluck('latitude');
$lat = floatval($lat[0]);
$long = $spotInfo->pluck('longitude');
$long = floatval($long[0]);
$radius = 20;
$commentTotal = $spotComment->count();
return response()->json([
'item' => $spotInfo,
'comment' => $spotComment,
// 'location' => $spotLocation,
'Nearby' => $shopNearby,
'commentTotal' => $commentTotal
]);
$shopNearby = DB::select('SELECT * FROM(
SELECT
`id`, `name`,
3956 * ACOS(COS(RADIANS( '.$lat.' )) * COS(RADIANS(`latitude`)) * COS(RADIANS( '.$long.' ) - RADIANS(`longitude`)) + SIN(RADIANS( '.$lat.' )) * SIN(RADIANS(`latitude`))) AS `distance`
FROM `shops`
WHERE
`latitude`
BETWEEN '.$lat.' - ('.$radius.'/69)
AND '.$lat.' + ('.$radius.'/69)
AND `longitude`
BETWEEN '.$long.' - '.$radius.'/(69 * COS(RADIANS('.$lat.')))
AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r
WHERE `distance` < '. $radius .'
ORDER BY `distance` ASC');
return response()->json([
'item' => $spotInfo,
'comment' => $spotComment,
'lat' => $lat,
'long' => $long,
// 'location' => $spotLocation,
'Nearby' => $shopNearby,
'commentTotal' => $commentTotal
]);
}
}

Expand Down
15 changes: 7 additions & 8 deletions database/migrations/2019_07_19_075325_rebuild__d_b.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function up()
$table->text('description');
$table->string('county')->nullable();
$table->string('district')->nullable();
$table->char('longitude', 10)->nullable();;
$table->char('latitude', 10)->nullable();;
$table->tinyInteger('avg_rate')->nullable();;
$table->char('longitude', 10)->nullable();
$table->char('latitude', 10)->nullable();
$table->tinyInteger('avg_rate')->nullable()->default(0);
$table->longText('img1')->nullable();
$table->longText('img2')->nullable();
$table->longText('img3')->nullable();
Expand All @@ -44,13 +44,12 @@ public function up()
$table->string('location', 5);
$table->string('name');
$table->text('description');
$table->tinyInteger('avg_rate')->nullable();
$table->text('bh')->nullable();
$table->tinyInteger('avg_rate')->nullable()->default(0);
$table->text('service_hour')->nullable();
$table->string('county')->nullable();
$table->string('district')->nullable();
$table->string('address')->nullable();
$table->char('phone1', 12)->nullable();
$table->char('phone2', 12)->nullable();
$table->char('phone', 12)->nullable();
$table->char('cell', 12)->nullable();
$table->longText('url')->nullable();
$table->longText('fb')->nullable();
$table->char('longitude', 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public function up()
Schema::create('articles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->char('category',10);
$table->longText('img');
$table->char('date',10);
$table->char('category',10)->nullable();
$table->longText('img')->nullable();
$table->integer('date');
$table->string('author',50);
$table->string('title');
$table->string('content');
$table->longText('content');
$table->longText('url');
});
}
Expand Down

0 comments on commit 8710e69

Please sign in to comment.