Php 特拉维尔·奥姆的工作很奇怪
我还有一个特点:Php 特拉维尔·奥姆的工作很奇怪,php,laravel,orm,eloquent,laravel-5.2,Php,Laravel,Orm,Eloquent,Laravel 5.2,我还有一个特点: <?php namespace App\Traits; trait LastElement { public function scopeLast($query) { return $query->orderBy('id', 'desc') ->first(); } } 不管这个模型做什么-但是在check()方法中,我的$lastvar是illumb\Database\e
<?php
namespace App\Traits;
trait LastElement {
public function scopeLast($query)
{
return $query->orderBy('id', 'desc')
->first();
}
}
不管这个模型做什么-但是在
check()
方法中,我的$last
var是illumb\Database\elount\Builder
的实例。在我的另一个模型中,这个特性运行良好-我错了什么?Scope应该返回一个Builder实例,因为它意味着可以链接。换句话说,scope永远不应该执行查询
现在在你的情况下,我认为你不需要一个特质。只需使用内置范围:
受保护的静态功能检查()
{
...
$last=self::lastest('id')->first();
...
}
Scope应该返回一个Builder实例,因为它意味着可以链接。换句话说,scope永远不应该执行查询
现在在你的情况下,我认为你不需要一个特质。只需使用内置范围:
受保护的静态功能检查()
{
...
$last=self::lastest('id')->first();
...
}
我想创建类似于first()
的东西,所以我使用了trait。但我不明白为什么在另一个模型中,这个特性工作得很好,但在这个模型中不行。我想创建类似于first()
的东西,所以我使用了这个特性。但我不明白为什么在另一个模型中,这种特性是有效的,但在这个模型中不是。
<?php
namespace App;
use App\Traits\LastElement;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Daemon extends Model
{
use LastElement;
use SoftDeletes;
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected static function check()
{
$last = self::last();
if (!empty($last)) {
sleep(1);
return empty(self::where('id', $last->id)
->where('updated_at', $last->updated_at)
->first());
}
return true;
}
public static function start()
{
if (self::check()) {
$me = new self;
$me->save();
$i = 0;
while (true) {
$i++;
usleep(1 / 3 * 1000);
if (($i % 3) == 0) {
$i = 0;
$me->save();
}
}
}
}
}