小编Rob*_*man的帖子

'String'类型的值没有成员'stringByTrimmingCharactersInSet'

将我的项目转换为swift 3后,我Value of type 'String' has no member 'stringByTrimmingCharactersInSet'在这个块的第一行得到以下错误:

extension UIColor {
    convenience init (hex:String) {
        var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercased() // error appears on this line

        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substring(from: 1)
        }



        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)

        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: …
Run Code Online (Sandbox Code Playgroud)

xcode ios swift swift3

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

Laravel 负载与模型条件的关系

我正在尝试使用依赖于通知模型值的关系急切加载用户的通知:

$notifications = $user->notifications()
    ->with([
        'notifiable' => function ($query) {
            // Only load notifiable relation if notification 'notifiable_type' equals...
        },
        'notifiable.group:id,title' => function ($query) {
            // Only load notifiable.group:id,title relation if notification 'notifiable_type' equals... 
        }
    ])
    ->get();
Run Code Online (Sandbox Code Playgroud)

问题是$query闭包内正在查询notifiable关系而不是通知模型本身......我显然错过了一些非常微不足道的东西。有什么建议?

php sql laravel eloquent

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

Laravel Eloquent 按关系计数排序

我有一个小型博客应用程序,允许用户上传照片和音频。用户还可以搜索其他博客。在搜索时,我想按以下优先级向查询添加排名:

1) Users with most photos
2) Users with most audio
Run Code Online (Sandbox Code Playgroud)

User模型构建如下:

public function blog() {
    return $this->hasOne(Blog::class);
}

public function photos() {
    return $this->hasMany(Photo::class);
}

public function audio() {
    return $this->hasMany(Audio::class);
}
Run Code Online (Sandbox Code Playgroud)

博客模型的构造如下:

public function user()
{
    return $this->belongsTo(User::class);
}
Run Code Online (Sandbox Code Playgroud)

我当前的搜索查询:

    $blogs = Blog::where('description', 'ilike', '%'.$search.'%')
    ->orWhere('title', 'ilike', '%'.$search.'%')
    ->orWhereHas('user', function($query) use ($search) {
        $query->where('name', 'ilike', '%'.$search.'%')
            ->orWhere('username', 'ilike', '%'.$search.'%');
    })
    ->paginate(10);
Run Code Online (Sandbox Code Playgroud)

根据给定的详细信息,我如何调整查询以返回对我的用户照片和音频计数进行排名的博客?

**更新**

我可以通过使用withCount方法和预先加载来获取每个嵌套关系的计数:

    $blogs = Blog::where('description', 'ilike', '%'.$search.'%')
    ->orWhere('title', 'ilike', '%'.$search.'%')
    ->orWhereHas('user', …
Run Code Online (Sandbox Code Playgroud)

sql relational-database laravel eloquent

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

Vue JS中的递归方法

我试图在Vue自身中调用方法,但是出现以下错误

this.loop不是函数。(在“ this.loop()”中,“ this.stagger”未定义)

这是方法:

    loop: function () {
            var n = $(".item").length;
            var i = 1;
            var m = n + 5;
          setTimeout( function () {
                $('.item:nth-child('+i+')').addClass('show');
                var x = i - 2;
                var y = x - 2;
                i = i + 3;

                // for 2 columns:
                // x = i - 1;
                // i = i + 2;

                $('.item:nth-child('+x+')').addClass('show');
                $('.item:nth-child('+y+')').addClass('show'); // remove for 2 columns


            if (i < m) {
                this.loop() // error occurs here
            }
          }, …
Run Code Online (Sandbox Code Playgroud)

javascript methods recursion vue.js vue-component

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