php - Laravel 5 查询关系不起作用

标签 php mysql laravel-5

我有两个表,如下:

1).我的网址

    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”或未过期记录,它应该只是给我一个例子。 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()); .

然后在您的 Controller (或其他任何地方)中,您可以获取域数据,如下所示

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

希望这就是您的意图,并且它可以帮助您入门。

关于php - Laravel 5 查询关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41029155/

相关文章:

php - Yii,CactiveForm 在数据库中保存多个表

PHP文件上传在客户服务器上不起作用

php - 如何在 curl php 中解决 HTTP/1.1 400 Bad Request

php - 如何修改外键约束以删除级联

php - Laravel 5 使用关系查询导致 "Call to a member function addEagerConstraints() on null"错误

php - 在数据库中添加类别和子类别

mysql - Ruby 和 MySQL : How to handle missing elements while parsing XML file

具有多个子字符串长度的 Mysql CASE

javascript - 如何在 javascript 中编写参数化 SQL 查询?

php - Laravel phpunit 模拟键盘按键