Laravel 5查询关系不起作用

时间:2016-12-07 23:22:09

标签: php mysql laravel-5

我有两张表如下:

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&amp; example.com都是EXPIRED,这是错误的,但不是因为至少有一个end_date&#34; NULL&#34;或者没有在链接表中过期记录,它假设只给我example.com过期因为它的所有链接end_date已过期

如何做到这一点我感谢您的帮助

1 个答案:

答案 0 :(得分:1)

按照我的理解,实现您的目标,可以完成

//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>  

希望这是你想要的,它有助于你开始。