标签: method-chaining

将集合转换为 Eloquent 对象

目前我在 Laravel 5.2 中使用 Xero API。我想利用 Eloquent 的强大功能来处理这些数据。

实际上,我可以恢复发票,甚至使用链接方法过滤它们,如下所示:

    $invoices = XeroPrivate::load('Accounting\\Invoice')
        ->where('Status', 'DRAFT')
        ->execute();
Run Code Online (Sandbox Code Playgroud)

如果我执行 a var_dump,我会得到这种数据:

object(XeroPHP\Remote\Collection)[173]
  public 0 => 
    object(XeroPHP\Models\Accounting\Invoice)[171]
      protected '_data' => 
        array (size=31)
          'Type' => string 'ACCPAY' (length=6)
          'Contact' => 
Run Code Online (Sandbox Code Playgroud)

雄辩的链接方法可以让我执行这样的事情。目前失败了:

    $invoices = XeroPrivate::load('Accounting\\Invoice')
        ->where('Date','>','2016-03-20')
        ->execute();
Run Code Online (Sandbox Code Playgroud)

检查 Laravel 的文档,假设我可以使用以下命令转换为集合collect

    $collection = collect($invoices);
Run Code Online (Sandbox Code Playgroud)

$collection并不能解决问题。现在数据结构不同了但仍然无法使用Eloquent。现在数据是:

object(Illuminate\Support\Collection)[163]
  protected 'items' => 
    array (size=24)
      0 => 
        object(XeroPHP\Models\Accounting\Invoice)[171]
          protected '_data' => 
            array (size=31)
Run Code Online (Sandbox Code Playgroud)

但事实证明,数据是Illuminate\Support\Collection正确的。

谢谢!

php method-chaining eloquent laravel-5 xero-api

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

将所有类都封装在 try/catch javascript 中?

如何使用如下方法实现一个类?

class ExistingClass {
     function func1() {} // might throw error
     function func2() {} // might throw error
     get try() {
        // Some magic here
        return this; // we need to return this to chain the calls, right? 
     }
}
Run Code Online (Sandbox Code Playgroud)

可以这样称呼

obj.func1() //might throw an error
obj.try.func1() // execute func1 in a try/catch
Run Code Online (Sandbox Code Playgroud)

基本上我想要像 mochajs 那样的东西:expect(..).to.not.equal()

更新:接受的答案应该有效,以下是它的更新版本,支持async功能

get try() {
    return new Proxy(this, {

        // Intercept method getter
        get: function(target, name) {
            if (typeof target[name] === 'function') …
Run Code Online (Sandbox Code Playgroud)

javascript method-chaining

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

How to make a function like this in lua

I wanna make a plugin where someone calls a function passes some info along with the call and makes a listenerfunction, the function has to look like this:

database.execute(database.Update)
:data({username="Jhon"})
:response(function(responseString, responseTable)
    /// 
end)
Run Code Online (Sandbox Code Playgroud)

I know how to make a basic function call like this database.execute(_, _, _) but have no idea how to make it multi-line operation like this

database.execute()
:_()
:_()
Run Code Online (Sandbox Code Playgroud)

lua method-chaining love2d coronasdk

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

如何在PHP中做到这一点?

可能重复:
如何在PHP5中构建多oop函数

嘿,

我在几个论坛系统中看到过这种代码,但我找不到这样的例子:

$this->function()->anotherfunction();
Run Code Online (Sandbox Code Playgroud)

您可以在PDO中看到类似的示例:

$pdo->query($sqlQuery)->fetch();
Run Code Online (Sandbox Code Playgroud)

我不知道在PHP中如何调用这种类型的编码,因此我无法继续寻找任何教程和示例.

php method-chaining

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

调用静态方法后重用类

假设我有一个带有几个静态void方法的类,例如:

class MyClass {
    public static void doJob() {
        // ...
    }
    public static void doSmthElse() {
         // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

如何修改它来调用我的静态方法:

MyClass.doJob().doSmthElse().doJob();
Run Code Online (Sandbox Code Playgroud)

代替

MyClass.doJob();
MyClass.doSmthElse();
MyClass.doJob();
Run Code Online (Sandbox Code Playgroud)

我知道如何使用非静态方法(只返回这个),但如何使用静态字段?

java static static-methods method-chaining

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

返回类型不兼容:Mutators和Method Chaining

我们将这段代码视为我们的示例:

import java.awt.*;

class Maze extends Panel{

    String name;

    public static void main(String[] args){
        Maze m = new Maze();
        System.out.println(m.setName("Hello World").getName());
    }

    public Maze setName(String name){
        this.name = name;
        return this;
    }

    public String getName(){
        return name;
    }

    public void paint(){

    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试理解方法链,并且正如其他问题的答案,请使用return this.我尝试了它,是的它可行,但不是像setName()上面的方法那样的mutator方法.为什么编译器输出:

The return type is incompatible with Component.setName(String)
Run Code Online (Sandbox Code Playgroud)

java method-chaining

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

从VBScript调用JScript函数 - 圆括号

我试图为传统的经典ASP应用程序带来一些理智,作为其中的一部分,我正在尝试为我创建的一些JScript类编写Fluent API.

例如 myClass().doSomething().doSomethingElse()

这里概述这个概念(在VBScript中)

这是我的示例JScript类:

var myClass = function () {
    this.value = '';
}

myClass.prototype = function () {

    var doSomething = function (a) {
        this.value += a;
        return this;
    },

    doSomethingElse = function (a, b) {
        this.value += (a + b);
        return this;
    },

    print = function () {
        Response.Write('Result is: ' + this.value + "<br/>");
    }

    return {
        doSomething: doSomething,
        doSomethingElse: doSomethingElse,
        print: print
    }; …
Run Code Online (Sandbox Code Playgroud)

vbscript fluent-interface method-chaining asp-classic jscript

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

Python可链接类方法

我想做以下事情:

pattern = cl().a().b("test").c()
Run Code Online (Sandbox Code Playgroud)

哪个cl是类,a, b, c是类方法.

之后我需要调用pattern.to_string它应该输出一个形成的字符串.每个方法返回一个字符串.

现在我怎样才能实现上述目标?将方法输出附加到列表?可连接功能怎么样?如果我以正常的方式写课,上面的方法就不行了.

谢谢.

python methods class method-chaining chaining

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

JS - 三元运算符,以避免条件中的过多链接.可能吗?

condition ?
    domElement.classList.add('show') :
    domElement.classList.remove('show');
Run Code Online (Sandbox Code Playgroud)

上面的代码可以工作,但DOM变量和classList被显式输入两次.有没有办法使用三元组只将链中的差异部分放在各自的真/假条款中?

我想的是:

domElement.classList condition ? .add('show') : .remove('show');
Run Code Online (Sandbox Code Playgroud)

任何和所有输入都非常感谢.

javascript code-duplication ternary-operator method-chaining

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

为什么我不能在java中调用nextLine()方法两次?

在使用Java的nextInt()方法时,我遇到了这段代码:

Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
String string1 = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)

我知道string1将包含一个空字符串.我的问题是为什么两次调用nextLine方法会给出错误:

String string1 = scan.nextLine().nextLine();
Run Code Online (Sandbox Code Playgroud)

java method-chaining

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