I have 3 Tables
Model Url
class Url extends Model
{
public function users(){
return $this->belongsToMany(User::class);
}
public function url_status(){
return $this->hasMany(UrlStatus::class);
}
}
模型 UrlStatus
class UrlStatus extends Model
{
public function url()
{
return $this->belongsTo(Url::class);
}
}
模型用户
class User extends Authenticatable
{
use Notifiable, SoftDeletes, HasRoles;
public function urls(){
return $this->belongsToMany(Url::class);
}
}
在我的控制器中,我正在查询:
$url = Url::with('url_status','users')->where('list_status', true)->get();
我该如何获取最新的url_status?
编辑 ---
这是我的迁移文件中每个表的结构:
URLS表格
Schema::create('urls', function (Blueprint $table) {
$table->increments('id');
$table->string('url');
$table->string('description');
$table->boolean('list_status');
$table->timestamps();
});
URL 状态
Schema::create('url_status', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('url_id');
$table->integer('status_code');
$table->string('status');
$table->boolean('sms');
$table->boolean('email');
$table->timestamps();
});
用户表
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('contacts');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
id
还是date
或者其他? - Zakaria AcharkiUrl
模型并首先附加每个模型的UrlStatus
模型并不会真正为您获取最新的UrlStatus
模型。直接查询UrlStatus
,使用latest
,然后附加Url
可能更有意义。或者先运行一个join
并按url_status.created_at, DESC
排序。 - Tim Lewis