小编Cha*_*han的帖子

如何在Laravel的各个领域订购

我在视图表中为MYSQL中的产品做了一些计算,所以我列出了这些id作为产品参考ID.

$referenceIds = viewTable::orderBy('score', 'DESC')->lists('product_id');
Run Code Online (Sandbox Code Playgroud)

我想从这些参考ID中获取产品.

$products = Product::whereIn('id', $rederenceIds);
Run Code Online (Sandbox Code Playgroud)

产品内容是正确的,但顺序错误,如何通过引用ID加载产品订单,我知道我可以在MySQL中使用ORDER BY FIELD(id,' . $fields . '),但我想找出Laravel方式,并希望不要使用ORDER BY FIELD命令,因为它似乎浪费提高数据库效率.

mysql laravel

14
推荐指数
2
解决办法
8320
查看次数

为什么有些人在if语句中将值放在变量之前?

例如,与$ variable === true有什么不同?

<?php

if (true === $variable) {
     //
}

if (1 === intval($variable)) {
    //
}
Run Code Online (Sandbox Code Playgroud)

php comparison if-statement

12
推荐指数
2
解决办法
1381
查看次数

Dataprovider可以从setUp获取连接

通过setUp()连接到数据库失败

class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new Core\Database('unitest');
    }

    /**
     * @dataProvider testProvider
     */
    public function testData($a, $b, $c)
    {
        $this->assertEquals($a + $b, $c);
    }

    public function testProvider()
    {
        $this->db->query('SELECT `a`, `b`, `c` FROM `units`');

        return $this->db->rows();
    }
}
Run Code Online (Sandbox Code Playgroud)

连接到数据库本身工作

class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new Core\Database('unitest');
    }

    public function testData($a, $b, $c)
    {
        $this->db->query('SELECT `a`, `b`, `c` FROM `units`');

        foreach ($this->db->rows() as $item) …
Run Code Online (Sandbox Code Playgroud)

phpunit

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

需要将字符串拆分为字符并平均对齐容器

起源

起源

结果

结果

我想将一个字符串拆分成字符,并使每个字符均匀地容纳容器,这是我暂时的工作:http://jsfiddle.net/d5fu9/

第一个项目必须附加到左侧,最后一个项目必须附加到右侧.

  $.fn.textjustify = function() {
        return this.each(function() {
            var text = $(this).text(),
                containerWidth = $(this).width(),
                character = '',
                string = '',
                singleWidth = 0,
                firstItemWidth = 0,
                lastItemWidth = 0,
                alignWidth = 0;

            if ('' !== text) {
                $(this).css('position', 'relative');
                textArray = text.split('');
                singleWidth = Math.floor(containerWidth / textArray.length);

                for (i in textArray) {
                    // Wrapp with div to get character width
                    character = ('' === $.trim(textArray[i])) ? '&nbsp' : textArray[i]; …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

如何忽略 PHPMD 中特定目录的特定规则

命令

phpmd app,config,routes text phpmd.xml
Run Code Online (Sandbox Code Playgroud)

我在 Laravel 项目上使用 phpmd,在 design.xml 下有一个名为的规则CouplingBetweenObjects,我想忽略 app/Http/Controllers 目录中的所有文件到此规则,phpmd 只提供--exclude使该目录通过所有规则的选项,是否可能通过 XML 文件或命令行忽略特定目录的规则

phpmd.xml

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Netask rule set"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Netask code style rule set
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
<rule ref="rulesets/codesize.xml/ExcessivePublicCount">
    <properties>
        <property name="minimum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyFields">
    <properties>
        <property name="maxfields" value="20"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
    <properties>
        <property name="maximum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/controversial.xml"/>
<rule …
Run Code Online (Sandbox Code Playgroud)

php phpmd

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

如何使用或在哪里有Laravel的地方

我有三个表user,role,permission,user的hasMany role,role的hasMany permission,我想找出用户谁拥有我给了允许,所以我用whereHas

$user = User::whereHas('roles', function($q) {
    $q->whereHas('permissions', $function($q) {
        $q->where('name', $value);
    });
});
Run Code Online (Sandbox Code Playgroud)

结果是正确的,但如果我想通过更多条件搜索用户,我会给出其他权限值并使用orWhere,它会响应所有拥有任何权限的用户,如何修复它?

$user = User::whereHas('roles', function($q) {
    $q->whereHas('permissions', $function($q) {
        $q->where('name', $value)->orWhere('name', $value2);
    });
});
Run Code Online (Sandbox Code Playgroud)

laravel-4

5
推荐指数
2
解决办法
5507
查看次数

如何通过Google JavaScript oauth2 API获取访问令牌

http://api.chan15.info/google-stackoverflow.html

这是我用来让用户通过谷歌JavaScript API登录的示例代码,它的工作原理,下一步是使用用户ID通过PHP登录本地服务器,但是使用JavaScript的用户ID是非常危险的,我想要的真正程序是:

  1. 通过JavaScript API登录用户
  2. 从JavaScript获取access_token
  3. 将访问令牌传递给PHP
  4. 将访问令牌用于Google OAuth以通过PHP再次获取用户ID
  5. 按用户ID登录用户

但我不知道如何获得访问令牌.

php oauth youtube-javascript-api google-api-javascript-client

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

如何嘲笑mock内部方法

目标.php

<?php

class Target
{
    public function validate()
    {
        $this->getData();
        return true;
    }

    public function getData()
    {
        return array();
    }
}
Run Code Online (Sandbox Code Playgroud)

目标测试.php

<?php

class TargetTest extends PHPUnit_Framework_TestCase
{
    public function testValidate()
    {
        $mock = m::mock('Target');
        $mock->shouldReceive('getData')
            ->once();
        $expected = $this->exp->validate();

        $this->assertTrue($expected);
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

Mockery\Exception\InvalidCountException: Method getData() from Mockery_1_ExpWarning should be called exactly 1 times but called 0 times.

我使用Mockery作为模拟工具,示例总是关于如何模拟DI,我想知道我可以模拟内部方法吗?

php phpunit unit-testing mockery

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

如何在Laravel中过滤多对多结构

我有很多用户和角色结构

用户
ID
名称

角色
ID
名称

role_user
user_id
role_id

模型

user.php的

public function roles() {
    return $this->belongsToMany('Role');
}
Run Code Online (Sandbox Code Playgroud)

Role.php

public function users() {
    return $this->belongsToMany('User');
}
Run Code Online (Sandbox Code Playgroud)

有两个数据adminsmembers角色表,我想知道过滤用户哪个角色是管理员.

php laravel

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

如何在jQuery中更正Ajax函数中的循环变量

for (i = 1; i <= 6; i++) {
    $.post("ajax.php", {param: i}, function (response) {
        console.log(i);
    });
}
Run Code Online (Sandbox Code Playgroud)

如何在$.post完整函数中获得正确的i变量,我可以将变量传递给它吗?

jquery

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