소스 검색

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');
+