小编Vic*_*Vic的帖子

使用存储库模式将 Eloquent\Collection (Laravel) 转换为 stdClass 数组

我正在尝试按照这篇文章在Laravel 5应用程序中实现存储库模式。其中,存储库实现将特定数据源(在本例中为 Eloquent)的对象转换为 stdClass,以便应用程序使用标准格式并且不关心数据源。

为了转换单个 Eloquent 对象,他们这样做:

/**
* Converting the Eloquent object to a standard format
* 
* @param mixed $pokemon
* @return stdClass
*/
protected function convertFormat($pokemon)
{
    if ($pokemon == null)
    {
        return null;
    }

    $object = new stdClass();
    $object->id = $pokemon->id;
    $object->name = $pokemon->name;

    return $object;
}
Run Code Online (Sandbox Code Playgroud)

或者,正如评论中有人指出的那样,这也可行:

protected function convertFormat($pokemon)
{
    return $pokemon ? (object) $pokemon->toArray() : null;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我想将整个 Eloquent 对象集合转换为 ** ** 数组时会发生什么stdClass?我是否必须循环遍历集合并分别转换每个元素?我觉得这会对性能造成很大影响,每次我需要收集一些东西时都必须循环并投射每个元素,而且感觉很脏。

Laravel 提供了Eloquent\Collection::toArray() …

php casting repository-pattern laravel eloquent

5
推荐指数
1
解决办法
1万
查看次数

一对一的关系,只在Laravel单向走

是否有可能/权利在表之间建立只有一种方式的关系?我有一个invoices表,我需要在其他表中引用喜欢commission_payments,membership_paymentsinvoices表不需要a commission_payment_id或a membership_payment_id.换句话说,可能发生不同类型的交易,并且它们都可能附有发票,但发票不需要引用这些交易表.

invoices           commission_payments      membership_payments
---------------    ---------------------    ---------------------
-id                -id                      -id
...                -invoice_id              -invoice_id
                   ...                      ...
Run Code Online (Sandbox Code Playgroud)

我为每个表创建了Eloquent模型.我在其他两个模型上添加了一个hasOne关系invoices.

class CommissionPayment extends Model{
    public function invoice(){
        return $this->hasOne('App\Models\Invoice');
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试访问Comission Payment的附加发票,如下所示:

$com = CommissionPayment::first();
$com->invoice->id;
Run Code Online (Sandbox Code Playgroud)

然后我得到这个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'invoices.commission_payment_id' in 'where clause' (SQL: select * from `invoices` 
where `invoices`.`commission_payment_id` = 15 and `invoices`.`commission_payment_id` is not
null limit 1) 
Run Code Online (Sandbox Code Playgroud)

为什么要 …

entity-relationship database-relations one-to-one has-one laravel

2
推荐指数
1
解决办法
1368
查看次数

Angular-cli:将较少的文件编译为全局css

我已经设置了我的应用程序less.我可以将lessfiless用于我的组件就好了.但是,我需要将一个less文件编译为全局css,以包含在根index.html中.

我读到最新的angular-cli版本(我正在使用"angular-cli": "1.0.0-beta.19-3",)使用webpack,但是我找不到关于它如何实现的文档,或者用于添加该指令的webpack文件.

如何设置我的angular-cli应用程序以编译一个较少的文件以供全局使用?

less webpack angular-cli angular

2
推荐指数
1
解决办法
2532
查看次数

Angular2:在生产中使用gzip文件

我正在使用angular-cli来编译我的应用程序.运行ng build --prodgzip包时会创建文件,但index.html仍在使用完整大小的文件.我试过跑步ng build --prod --aot并得到了同样的结果.如何告诉angular-cli在生产中使用.gz文件?

gzip production-environment angular-cli angular

2
推荐指数
1
解决办法
7215
查看次数

模型中的Laravel 5.1 bool属性在模型类中加入时返回空数组

我有一个带有布尔字段的雄辩模型.数据库中的列是a tinyint(1),值正确存储为1.如果我在类之外的任何给定上下文中访问此值,我将获得正确的值:

$myModel = MyModel::first();
var_dump($myModel->visible); //outputs 1
Run Code Online (Sandbox Code Playgroud)

当我在模型类中的方法内访问它时虽然....

class MyModel {
    public function isVisible(){
        var_dump($this->visible); 
        // return $this->visible && $this->approved; // another true value
    }
}

//on tinker
>> $myModel->isVisible();
array(0) {
}
>>
Run Code Online (Sandbox Code Playgroud)

我知道这听起来很疯狂,但我已经在这里呆了两个小时而且无法使它发挥作用.我错过了什么?

model typing this laravel laravel-5.1

0
推荐指数
1
解决办法
268
查看次数