Change DB tables (remove temperature) (add sensors and readings Models Controllers and migrations) (and respect reallationships)
This commit is contained in:
11
app/Http/Controllers/ReadingController.php
Normal file
11
app/Http/Controllers/ReadingController.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Reading;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class ReadingController extends Controller
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Temperature;
|
use App\Models\Sensor;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class TemperatureController extends Controller
|
class SensorController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
@@ -5,7 +5,7 @@ namespace App\Models;
|
|||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Temperature extends Model
|
class Reading extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -15,9 +15,12 @@ class Temperature extends Model
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'temperature',
|
'type',
|
||||||
'sensor_status',
|
'value',
|
||||||
'longitude',
|
|
||||||
'latitude',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function sensor()
|
||||||
|
{
|
||||||
|
return $this->belongsTo('App\Models\Sensor');
|
||||||
|
}
|
||||||
}
|
}
|
32
app/Models/Sensor.php
Normal file
32
app/Models/Sensor.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Sensor extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'status',
|
||||||
|
'longitude',
|
||||||
|
'latitude',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo('App\Models\User');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function readings()
|
||||||
|
{
|
||||||
|
return $this->hasMany('App\Models\Reading');
|
||||||
|
}
|
||||||
|
}
|
@@ -37,4 +37,9 @@ class User extends Authenticatable
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'email_verified_at' => 'datetime',
|
'email_verified_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function sensors()
|
||||||
|
{
|
||||||
|
return $this->hasMany('App\Models\Sensor');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
|
|||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
class CreateTemperaturesTable extends Migration
|
class CreateSensorsTable extends Migration
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
@@ -13,13 +13,15 @@ class CreateTemperaturesTable extends Migration
|
|||||||
*/
|
*/
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
Schema::create('temperatures', function (Blueprint $table) {
|
Schema::create('sensors', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->float('temperature', 2, 2);
|
$table->foreignId('user_id');
|
||||||
$table->boolean('sensor_status');
|
$table->boolean('status');
|
||||||
$table->float('longitude', 8, 6);
|
$table->float('longitude', 8, 6);
|
||||||
$table->float('latitude', 8, 6);
|
$table->float('latitude', 8, 6);
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('user_id')->references('id')->on('users');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +32,6 @@ class CreateTemperaturesTable extends Migration
|
|||||||
*/
|
*/
|
||||||
public function down()
|
public function down()
|
||||||
{
|
{
|
||||||
Schema::dropIfExists('temperatures');
|
Schema::dropIfExists('sensors');
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateReadingsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('readings', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('sensor_id');
|
||||||
|
$table->boolean('type');
|
||||||
|
$table->float('value', 2, 2);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('sensor_id')->references('id')->on('sensors');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('readings');
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user