Browse Source

Add Place model

Daniel Supernault 6 years ago
parent
commit
3ed275df31

+ 23 - 0
app/Http/Controllers/PlaceController.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use App\{
+	Place,
+	Status
+};
+
+class PlaceController extends Controller
+{
+    public function show(Request $request, $id, $slug)
+    {
+        // TODO: Replace with vue component + apis
+    	$place = Place::whereSlug($slug)->findOrFail($id);
+    	$posts = Status::wherePlaceId($place->id)
+    		->whereScope('public')
+    		->orderByDesc('created_at')
+    		->paginate(10);
+    	return view('discover.places.show', compact('place', 'posts'));
+    }
+}

+ 33 - 0
app/Place.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+use Pixelfed\Snowflake\HasSnowflakePrimary;
+
+class Place extends Model
+{
+	use HasSnowflakePrimary;
+
+    /**
+     * Indicates if the IDs are auto-incrementing.
+     *
+     * @var bool
+     */
+    public $incrementing = false;
+    
+	public function url()
+	{
+		return url('/discover/places/' . $this->id . '/' . $this->slug);
+	}
+
+	public function posts()
+	{
+		return $this->hasMany(Status::class);
+	}
+
+	public function postCount()
+	{
+		return $this->posts()->count();
+	}
+}

+ 45 - 0
database/migrations/2019_08_07_184030_create_places_table.php

@@ -0,0 +1,45 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePlacesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('places', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('slug')->index();
+            $table->string('name')->index();
+            $table->string('country')->index();
+            $table->json('aliases')->nullable();
+            $table->decimal('lat', 9, 6)->nullable();
+            $table->decimal('long', 9, 6)->nullable();
+            $table->unique(['slug', 'country', 'lat', 'long']);
+            $table->timestamps();
+        });
+
+        Schema::table('statuses', function (Blueprint $table) {
+            $table->bigInteger('place_id')->unsigned()->nullable()->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('places');
+        Schema::table('statuses', function (Blueprint $table) {
+            $table->dropColumn('place_id');
+        });
+    }
+}