[英]Laravel 5 Query relationship not working
我有两个表,如下所示:
1)。 myurls
id domain
----------------------
1 google.com
2 test.com
3 example.com etc
2)。 链接
id myurls_id end_date created_at
---------------------------------------------------
1 2 2016-11-30 00:00:00 2016-11-30 00:00:00
2 2 2016-12-01 00:00:00 2016-11-30 00:00:00
3 2 NULL 2016-11-30 00:00:00
4 2 2017-01-01 00:00:00 2016-12-01 00:00:00
5 3 2016-11-24 00:00:00 2016-12-01 00:00:00
6 3 2016-10-01 00:00:00 2016-12-01 00:00:00
等等...如您所见,它们与myurls_id有一对多关系
在此示例中,对于myurls_id(2),我们有4条记录:2条记录,其中end_date过期1条记录,其中end_date为NULL 1条记录,其中end_date未过期
我想编写一个laravel关系查询,它将使我获得过期的URL,这意味着查询结果,如果您发现至少一个NULL或一个未过期的未列出,我们只想列出所有4条记录是否都已过期。 我怎样才能做到这一点
这是我所拥有的但不起作用:
$expired_domains = Myurls::with(['links' => function ($query) {
$query->whereNotNull('end_date')
->where('end_date','<',Carbon::now())
->where('created_at', '<', Carbon::now())
->orderBy('created_at', 'asc')
->first();
}])->get();
这向我显示test.com和example.com都已过期,这是错误的,但不是因为链接表中至少有一个end_date“ NULL”或未过期的记录,所以它只给了example.com已过期,因为其所有链接end_date已过期
怎么办,谢谢您的帮助
据我所知,要实现您的意图,可以通过
//In your MyUrls model define two relationships
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class MyUrls extends Model
{
protected $table="myurls";
protected $fillable = ['domain'];
public function links()
{
return $this->hasMany(Link::class, 'myurls_id');
}
public function active_links()
{
return $this->hasMany(Link::class, 'myurls_id')
->whereApproved(1)
->where('end_date', '>', Carbon::now());
}
public function null_links()
{
return $this->hasMany(Link::class, 'myurls_id')
->whereApproved(1)
->whereNull('end_date');
}
public function unapproved_active_links()
{
return $this->hasMany(Link::class, 'myurls_id')
->whereApproved(0)
->where('end_date', '>', Carbon::now());
}
public function unapproved_null_links()
{
return $this->hasMany(Link::class, 'myurls_id')
->whereApproved(0)
->whereNull('end_date');
}
}
您可以在关系中应用所需的任何顺序,例如return $this->hasMany('active_links', 'myurls_id')->where('end_date', '>', Carbon::now())->orderBy('end_date', 'dsc');
。
如果在迁移中使用默认的timestamps()
,则可能无需检查where('created_at', '<', Carbon::now());
。
然后在您的控制器(或其他任何地方)中,您可以将域数据获取为
$allDomains = MyUrls::all(); //list of all domains
//All domains which have links but do not have active_links are expired.
//Domains without any links will not be listed here.
$expiredDomains = MyUrls::has('links')
->doesntHave('active_links')
->doesntHave('null_links')
->doesntHave('unapproved_active_links')
->doesntHave('unapproved_null_links')
->get();
//All domains which have active_links are active.
//Domains without any links will not be listed here.
$activeDomains = MyUrls::has('active_links')->get();
然后,您可以使用
<h4>All Domains</h4>
<ul>
@foreach($allDomains as $domain)
<li>{{$domain->domain}}</li>
@endforeach
</ul>
<hr />
<h4>Expired Domains</h4>
<ul>
@foreach($expiredDomains as $domain)
<li>{{$domain->domain .' is an expired domain '}}</li>
@endforeach
</ul>
<hr />
<h4>Active Domains</h4>
<ul>
@foreach($activeDomains as $domain)
<li>{{$domain->domain .' has an end date of '.$domain->active_links[0]->end_date}}</li>
@endforeach
</ul>
希望这是您想要的,并且可以帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.