Laravel关系返回空结果

时间:2019-05-25 10:17:54

标签: laravel-5

我的Laravel关系返回空结果。谁能帮助我做错了事。

型号代码为:

public function title()
{
    $branch_id=session()->get('lbranch','0');
    return $this->hasOne('App\Accounts','code','supp_code')
        ->select('title')->where('branchid','=',$branch_id);
}

并且控制器代码为:

   $data = Purchase::with('title')
       ->where('purchases.branchid',$branch_id)
       ->select('purchases.*',
           DB::raw('(CASE 
                WHEN purchases.posted = "1" THEN "Posted" 
                ELSE "Unposted" 
                END) AS status'))
       ->latest()->get();

生成的查询是:

select `purchases`.*, (CASE
     WHEN purchases.posted = "1" THEN "Posted"
     ELSE "Unposted"
     END) AS status from `purchases` 
     where `purchases`.`branchid` = 22 order by `created_at` desc

select `title` from `accounts` where `branchid` = 22 and `accounts`.`code` in (100)

更新: 如果我使用join,则以下查询工作正常:

$data = DB::table('purchases')
    ->where('purchases.branchid',$branch_id)
    ->where('accounts.branchid',$branch_id)
    ->leftjoin('accounts','purchases.supp_code','=','accounts.code')
    ->select('purchases.*',
        DB::raw('(CASE 
            WHEN purchases.posted = "1" THEN "Posted" 
            ELSE "Unposted" 
            END) AS status'),
        'accounts.title')
    ->latest()->get();

1 个答案:

答案 0 :(得分:1)

似乎您的关系定义有问题。请确认帐户的型号名称。它应该是Account而不是Accounts。但是仍然应该在您的申请中进行确认。

public function title()
{
    ---
    return $this->hasOne('App\Accounts','code','supp_code')
        ---;
}

应该是

public function title()
{
    ---
    return $this->hasOne('App\Account','code','supp_code')
        ---;
}

但是我尝试运行您的代码,对我来说运行正常。

这是我尝试过的。

App \ Models \ Purchase.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
    /**
     *  get model table name.
     *  
     *  @var string
     */
    public $table = "purchases";

    public function title()
    {
        $branch_id=22;
        return $this->hasOne('App\Models\Account','code','supp_code')
            ->select('title')->where('branchid','=',$branch_id);
    }

}

App \ Models \ Account.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Account extends Model
{
    /**
     *  get model table name.
     *  
     *  @var string
     */
    public $table = "accounts";

}

在路由文件中。

<?php

Route::get('/', function(){

    $branch_id = 22;

    $data = Purchase::with('title')
    ->where('purchases.branchid',$branch_id)
    ->select('purchases.*',
        \DB::raw('(CASE 
             WHEN purchases.posted = "1" THEN "Posted" 
             ELSE "Unposted" 
             END) AS status'))
    ->latest()->get();

    dd($data->toArray());

    $data = \DB::table('purchases')
    ->where('purchases.branchid',$branch_id)
    ->where('accounts.branchid',$branch_id)
    ->leftjoin('accounts','purchases.supp_code','=','accounts.code')
    ->select('purchases.*',
        \DB::raw('(CASE 
            WHEN purchases.posted = "1" THEN "Posted" 
            ELSE "Unposted" 
            END) AS status'),
        'accounts.title')
    ->latest()->get();

    dd($data);
});

两个查询都对我有用。并返回期望的结果。

查询1

array:2 [▼
  0 => {#436 ▼
    +"id": 6
    +"refno": 21
    +"supp_code": 100
    +"total": 41241
    +"posted": 1
    +"date": "2019-05-25 22:53:00"
    +"branchid": 22
    +"created_at": "2019-05-25 22:53:00"
    +"updated_at": "2019-05-25 22:53:00"
    +"status": "Posted"
    +"title": "test"
  }
  1 => {#438 ▼
    +"id": 3
    +"refno": null
    +"supp_code": 100
    +"total": 3114
    +"posted": null
    +"date": "2019-05-25 22:53:00"
    +"branchid": 22
    +"created_at": "2019-05-25 22:53:00"
    +"updated_at": "2019-05-25 22:53:00"
    +"status": "Unposted"
    +"title": "test"
  }
]

Query2

array:2 [▼
  0 => {#436 ▼
    +"id": 3
    +"refno": null
    +"supp_code": 100
    +"total": 3114
    +"posted": null
    +"date": "2019-05-25 22:53:00"
    +"branchid": 22
    +"created_at": "2019-05-25 22:53:00"
    +"updated_at": "2019-05-25 22:53:00"
    +"status": "Unposted"
    +"title": "test"
  }
  1 => {#438 ▼
    +"id": 6
    +"refno": 21
    +"supp_code": 100
    +"total": 41241
    +"posted": 1
    +"date": "2019-05-25 22:53:00"
    +"branchid": 22
    +"created_at": "2019-05-25 22:53:00"
    +"updated_at": "2019-05-25 22:53:00"
    +"status": "Posted"
    +"title": "test"
  }
]

数据库SQL

CREATE TABLE `accounts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `branchid` int(11) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `accounts` VALUES (3,100,'test',22,NULL,NULL),(4,2,'testsetestes',34,NULL,NULL),(5,2,'testest seat',456,NULL,NULL),(6,3,'testse aeta ',22,NULL,NULL);


CREATE TABLE `purchases` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `refno` int(11) DEFAULT NULL,
  `supp_code` int(11) DEFAULT NULL,
  `total` int(11) DEFAULT NULL,
  `posted` int(11) DEFAULT NULL,
  `date` timestamp NULL DEFAULT NULL,
  `branchid` int(11) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



INSERT INTO `purchases` VALUES (3,NULL,100,3114,NULL,'2019-05-25 22:53:00',22,'2019-05-25 22:53:00','2019-05-25 22:53:00'),(4,23,101,4324,1,'2019-05-25 22:53:00',23,'2019-05-25 22:53:00','2019-05-25 22:53:00'),(5,22,101,32424,0,'2019-05-25 22:53:00',21,'2019-05-25 22:53:00','2019-05-25 22:53:00'),(6,21,100,41241,1,'2019-05-25 22:53:00',22,'2019-05-25 22:53:00','2019-05-25 22:53:00');

编辑:

尝试运行您的代码。这是建议的更改。

PurchaseController

public function index()
    {
        //
        $branch_id = 22;


        $data = Purchase::with('account')
            ->where('purchases.branchid',$branch_id)
            ->select('purchases.*',
                \DB::raw('(CASE 
             WHEN purchases.posted = "1" THEN "Posted" 
             ELSE "Unposted" 
             END) AS status'))
            ->latest()->get();

        dd($data->toArray());

        /*$data = \DB::table('purchases')
            ->where('purchases.branchid',$branch_id)
            ->where('accounts.branchid',$branch_id)
            ->leftjoin('accounts','purchases.supp_code','=','accounts.code')
            ->select('purchases.*',
                \DB::raw('(CASE 
            WHEN purchases.posted = "1" THEN "Posted" 
            ELSE "Unposted" 
            END) AS status'),
                'accounts.title')
            ->latest()->get();

        dd($data);*/
    }

购买模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
    //
    public $table = "purchases";

    public function account()
    {
        $branch_id=22;

        return $this->hasOne('App\Account','code','supp_code')
            ->where('branchid', $branch_id);
    }
}

结果:

array:2 [▼
  0 => array:11 [▼
    "id" => 3
    "refno" => 24
    "supp_code" => 100
    "total" => 3114
    "posted" => null
    "date" => "2019-05-25 22:53:00"
    "branchid" => 22
    "created_at" => "2019-05-25 22:53:00"
    "updated_at" => "2019-05-25 22:53:00"
    "status" => "Unposted"
    "account" => array:6 [▼
      "id" => 3
      "code" => 100
      "title" => "test"
      "branchid" => 22
      "created_at" => null
      "updated_at" => null
    ]
  ]
  1 => array:11 [▼
    "id" => 6
    "refno" => 21
    "supp_code" => 100
    "total" => 41241
    "posted" => 1
    "date" => "2019-05-25 22:53:00"
    "branchid" => 22
    "created_at" => "2019-05-25 22:53:00"
    "updated_at" => "2019-05-25 22:53:00"
    "status" => "Posted"
    "account" => array:6 [▼
      "id" => 3
      "code" => 100
      "title" => "test"
      "branchid" => 22
      "created_at" => null
      "updated_at" => null
    ]
  ]
]