瀏覽代碼

user controller, returns jsons

Carello Qwerty 5 年之前
父節點
當前提交
e951d10946
共有 3 個文件被更改,包括 33 次插入1 次删除
  1. 25 0
      app/Http/Controllers/UserController.php
  2. 4 0
      app/User.php
  3. 4 1
      routes/web.php

+ 25 - 0
app/Http/Controllers/UserController.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\User;
+use Illuminate\Http\Request;
+
+class UserController extends Controller
+{
+    public function index(){
+        $locations = [];
+        foreach (User::all() as $user){
+            $locations[] = $user->locations;
+        }
+        return $locations;
+    }
+
+    public function show(User $id){
+        return $id->locations;
+    }
+
+    public function showLast(User $id){
+        return $id->lastCoordinates;
+    }
+}

+ 4 - 0
app/User.php

@@ -48,4 +48,8 @@ class User extends Authenticatable
     public function groups(){
         return $this->belongsToMany(Group::class);
     }
+
+    public function getLastCoordinatesAttribute(){
+        return self::locations()->orderBy('created_at', 'desc')->first();
+    }
 }

+ 4 - 1
routes/web.php

@@ -19,4 +19,7 @@ Auth::routes();
 
 Route::get('/home', 'HomeController@index')->name('home');
 
-Route::resource('contacts', 'ContactController');
+Route::get('/user', 'UserController@index')->name('all.positions');
+Route::get('/user/{id}', 'UserController@show')->where('id', '[0-9]+')->name('user.position');
+Route::get('/user/{id}/last', 'UserController@showLast')->where('id', '[0-9]+')->name('last.position');
+