]I am beginner php developer. I have project in Laravel 5.6.
I have 2 migrations:
class CreateStopwatches extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stopwatches', function (Blueprint $table) {
$table->increments('id');
$table->integer('case_id')->unsigned();
$table->foreign('case_id')->references('id')->on('case_instances')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stopwatches');
}
}
class CreateStopwatchTimeCycles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stopwatch_time_cycles', function (Blueprint $table) {
$table->increments('id');
$table->morphs('timecycle');
$table->boolean('status')->default(0);
$table->integer('worked_time')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stopwatch_time_cycles');
}
}
And models:
class Stopwatch extends Model
{
protected $fillable = [
'case_id'
];
protected $casts = [
'id' => 'int',
'case_id' => 'int',
];
protected $dates = [
'created_at',
'updated_at'
];
public function timeCycle()
{
return $this->morphMany(StopWatchTimeCycle::class, 'timecycle');
}
}
class StopwatchTimeCycle extends Model
{
protected $fillable = [
'case_id'
];
protected $casts = [
'id' => 'int',
'case_id' => 'int',
];
protected $dates = [
'created_at',
'updated_at'
];
public function stopwatch()
{
return $this->morphTo();
}
}
In my controller I need return 1 - when stopwatches has stopwatches_time_cycles. I make this controller:
public function checkActivity(int $caseId)
{
if (Stopwatch::with(['timeCycle'])->where('case_id', $caseId)->withCount('timeCycle')->first() === 0) {
return 0;
} else {
return 1;
}
}
but it's not working correctly. It's always return 1 (I have empty table stopwatch_time_cycles).
How can I repair it?
Please help me