blog
user
29 042020

Laravel Eloquent provides very usefull tools to run simple and clean database queries. Sometimes it is nessesary to order requested records by related fields. When you just want to just get model using sorting options you can use

$data = Primarymodel::orderBy('column')->get();

To get records ordered by related model column you have to build more complicated query. First of all related model has to be defined in model class by

class Primarymodel extends Model
{

   public $belongsTo = ['relatedmodel','Acme\Plugin\Model\Relatedmodel'];

}

I found solution on stackoverflow.com which is very helpful many times. The first solution does't work. I don't know why it is marked as useful by over 200 users. Anyway query has to be joined to related model and look like this:

$data = Primarymodel::join('relatedmodeltable', 'primarymodeltable.relatedmodeltable_id','=','relatedmodeltable.id')
        ->orderBy('relatedmodeltable.column')->get();

 

 Thats it.

 

Nasze newsy

Śledź nasze aktualności.

blog
user
08 072020

Fałszywe e-maile z żądaniem okupu

Na konta e-mailowe naszych klientów trafiają informacje z żądaniem okupu w BTC

blog
user
04 072020

Jak utworzyć nowy plik szablonu bez ftp

Krótki poradnik jak utworzyć nowy plik szablonu bez dostępu do ftp.

blog
user
26 062020

Spam przez formularz kontaktowy prestashop

Nadeszła kolejna fala spamu przez formularz kontaktowy prestashop.

logo

Masz pytania? Skontaktuj się