DYK That You Can Use Scopes with Relations?

With Laravel, I’m sure you use the shit out of relations if you are working with Eloquent. There’s an eager loading style of Eloquent which you can define custom operations like ->where(...), ->join(...) or anything else that is in Query Builder. But do you really think that it’s okay to do this in a controller?:

Category::with([ 'post' => function($query)
{
    $query
        ->select([ 'id', 'title' ])
        ->where('is_active', '1');
})->get();

Ugh, most likely I’ll have to copy and paste that to a few more controller methods. Instead, let’s create a scope for our beloved Post model.

class Post extends Eloquent {

    public function scopeTitleOnly($query, $includeInactive = false)
    {
        if ( ! $includeInactive)
        {
            $query->where('is_active', '1');
        }

        return $query->select([ 'id', 'title' ]);
    }

}

Eloquent, isn’t it? Do you know what’s best about it? You can use your scopes in relation closure like it’s in Query Builder. Watch this:

Category::with([ 'posts' => function($posts)
{
    return $posts->titleOnly();
})->get();

Noticed that I changed $query to $posts? Feels lovely this way.

End of post. Feel free to comment or email me if something wrong about this post.

Komut Satırı: Eşleşen Dosyaları Arşive Eklemek

SSH üzerinden sunucunuzdaki bazı dosyaları arşivleyip (zipleyip) saklamanız veya başka bir yere aktarmanız gerekebilir. Eğer bu bazı dosyaların isimlerinde ortak kelime veya bir şablon varsa wildcardları kullanarak dosyaları aşağıdaki şekilde seçebilirsiniz.

find . -name *access.log*

Bu komutu ben, sunucumda bulunduğum dizindeki tüm access log dosyalarını seçmek için kullandım.

Bilgi: Komutların ayrıntılı açıklamalarını explainshell.com adresinden öğrenebilirsiniz.

Komutu çalıştırdığınızda eşleşen tüm dosyaların isimlerini dökecektir. Bu dosyaları arşive eklemek içinse find komutunun -exec parametresinden yararlanmalısınız.

find . -name *access.log* -exec tar czvf arşivismi.tar.gz {} \;

Bu komutla, bulunduğum dizinde, isminin herhangi bir yerinde “access.log” geçen bütün dosyalar arşivismi.tar.gz dosyasına eklenir.

“find: paths must precede expression” hatası alırsanız dosya ismini tırnak içine alın.
… -name “*access.log*” …

Nemo Rules

I recently started using Linux Mint 16 with Cinnamon desktop. Besides the fact that it reminds me of the old friend Windows because it also used a bottom taskbar (or what they like to call it, the panel) instead of the shitty launcher of Unity or Gnome 3′s completely different UI, it’s pretty much the same with Ubuntu. That’s because, just like Ubuntu, Mint is also based on Debian. I also should probably mention that Cinnamon is a Gnome Shell fork.

Mint’s default file manager, Nemo, is a fork of Ubuntu’s Nautilus. The difference is Nemo has removed some features of Nautilus. A few minutes earlier I found out that you can add your own options to the contextual (right-click) menu of Nemo by creating a file for each option. While you can accomplish the same for Nautilus with the package called nautilus-actions, this is not really a satisfying solution for people who don’t like doing things via UIs like me. In order to add your own options to the contextual menu of Nemo, here’s a brief tutorial.

How to add extra options to contextual menu of Nemo?

There’s a sample option file for you to examine which is located at /usr/share/nemo/actions/sample.nemo_action. If not, or you are too lazy to go through the codes, keep reading. Continue reading

Hello world!

Hey there! This is yet another fresh start to my blogging experiments after various incidents to the previous ones. Gonna use it more effectively this time. Started as an English blog but there’ll be Turkish posts from time to time. They are basically gonna be about my random thoughts mostly. Hope that you all will enjoy it.