简体   繁体   English

Laravel数组而不是集合

[英]Laravel array instead of collection

In my Laravel application, I have this model: 在我的Laravel应用程序中,我有这个模型:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Division extends Model
{

    protected $fillable = [
               'name','company_id', 'location_id'
               ];

    protected $casts = [
                'name' => 'string',
                'company_id' => 'integer',
                'location_id' => 'integer'
                ];

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function location()
    {
        return $this->belongsTo(Location::class);
    }

}

$division->company returns a collection $division->company返回一个集合

$division->location returns an array $division->location返回一个数组

Why this two relations has different results? 为什么这两种关系有不同的结果? (Sorry for bad formating....) (对不起格式化......)

As you've just shown in the comments (and then edited), you're using it with get() . 正如您刚刚在评论中显示(然后编辑),您将其与get() That's why you're getting a collection and not an object. 这就是为什么你得到一个集合而不是一个对象。

$division->company returns an object. $division->company返回一个对象。 Then you're running another query with $division->company->anotherRelationship()->get() which returns a collection of related objects. 然后你用$division->company->anotherRelationship()->get()运行另一个查询,它返回一个相关对象的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM