Add Place model

This commit is contained in:
Daniel Supernault 2019-08-08 00:06:31 -06:00
parent 1ee80a281b
commit 3ed275df31
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 101 additions and 0 deletions

View file

@ -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
app/Place.php Normal file
View file

@ -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();
}
}

View file

@ -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');
});
}
}