我有3张桌子
模型网址
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?
编辑---
这是我的迁移文件中每个表的结构
用于网址表
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();
});
答案 0 :(得分:4)
您也可以这样尝试
模型网址
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/whacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WHACKS"
android:textSize="24sp"
android:textAlignment="center"/>
<TextView
android:id="@+id/maxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="MAX WHACKS"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="60"
android:textSize="24sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/numberOfWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"/>
<TextView
android:id="@+id/numberOfMaxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="0"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/nl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/sl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
以这种方式获取
public function latestUrlStatus(){
return $this->hasOne(UrlStatus::class)->latest();
}
答案 1 :(得分:1)
尝试将latest()
方法与first()
一起使用:
$url = Url::with('url_status','users')->where('list_status', true)->whereHas('url_status', function($q){
$q->latest()->first();
})->get();
如果您要获取给定URL的最新url_status
,可以这样进行:
$url->url_status->latest()->first();