MartinZeltin

MartinZeltin

Member Since 8 Months Ago

Experience Points 18,680
Experience Level 4

1,320 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 132
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

15 May
4 days ago

MartinZeltin left a reply on What Does Double Colon :: Mean In View()?

@CRONIX - Oh, thanks. I was looking at the views docs but I found the answer in the Package Dev docs.

https://laravel.com/docs/5.8/packages#views

Package views are referenced using the package::view syntax convention. So, once your view path is registered in a service provider, you may load the admin view from the courier package like so:

Route::get('admin', function () {
    return view('courier::admin');
});

MartinZeltin left a reply on What Does Double Colon :: Mean In View()?

@CRONIX - I know about that. But isn't view() syntax like 'folder.view' instead of 'folder::layout'?

MartinZeltin started a new conversation What Does Double Colon :: Mean In View()?

I came across an interesting syntax when looking at the Laravel Telescope source code. I noticed a double colon in view. And I couldn't find anything about it in the Docs. What does double colon mean and how does it work?

I see that the layout view is located in telescope/resources/views/layout.blade.php

public function index()
    {
        return view('telescope::layout', [
            'cssFile' => Telescope::$useDarkTheme ? 'app-dark.css' : 'app.css',
            'telescopeScriptVariables' => Telescope::scriptVariables(),
        ]);
    }

https://github.com/laravel/telescope/blob/2.0/src/Http/Controllers/HomeController.php

14 May
5 days ago

MartinZeltin left a reply on Laravel Telescope Not Working On A Fresh Install (wrong Path)

I finally found a solution (for anyone else who might come across this issue).

  1. Open: \vendor\laravel\telescope\resources\views\layout.blade.php
  2. Change these lines
<!-- Global Telescope Object -->
<script>
   window.Telescope = @json($telescopeScriptVariables);
</script>

to this

<!-- Global Telescope Object -->
<script>
   window.Telescope = @json($telescopeScriptVariables);
   window.Telescope.path = 'your_project_folder/public/telescope';
</script>

MartinZeltin started a new conversation Laravel Telescope Not Working On A Fresh Install (wrong Path)

I installed a fresh Laravel 5.8 with composer create-project --prefer-dist laravel/laravel larascopy

I can access it by going to: http://localhost/larascopy/public/

And then installed Telescope with these commands from the Guide:

composer require laravel/telescope
php artisan telescope:install
php artisan migrate

Then I can open Telescope like this http://localhost/larascopy/public/telescope

But all the links are wrong! They are pointing to: http://localhost/telescope/...

So I get all these errors in Console..

POST http://localhost/telescope/telescope-api/commands?tag=&before=&take=50&family_hash= 404 (Not Found)
POST http://localhost/telescope/telescope-api/commands?tag=&before=&take=50&family_hash= 404 (Not Found)
POST http://localhost/telescope/telescope-api/schedule?tag=&before=&take=50&family_hash= 404 (Not Found)
app.js?id=8776cf12ba6d7c72f145:1 Uncaught (in promise) Error: Request failed with status code 404
08 May
1 week ago

MartinZeltin started a new conversation What Is Query() Method For?

I have noticed in a project a syntax like this but I could not find it on the Laravel docs anywhere. What is this syntax and why is it necessary?

class Email extends Model
{
    public function getEmails()
    {
        return self::query()
            ->where('id', 89)
            ->where('email', '[email protected]')
            ->get();
    }
}

Isn't it the same as this? What is the query() for?

class Email extends Model
{
    public function getEmails()
    {
        return self::where('id', 89)
            ->where('email', '[email protected]')
            ->get();
    }
}

MartinZeltin left a reply on How To Run A Command On A Specific Day At 9:00 Am?

@MTHOMAS - Perfect, I missed that part from the docs!

MartinZeltin started a new conversation How To Run A Command On A Specific Day At 9:00 Am?

I looked at the schedule command for Laravel and it allows to execute a command every hour, on monday etc.

But I could not find a way to execute a command only once for example On July 4th, 2019 at 9:00 am.

07 May
1 week ago

MartinZeltin left a reply on Vuejs Components In App.js And In .blades

This worked for me but without using webpack. Just native ES6.

{
    path: '/',
    component: () => import('./MyComponent.js')
}

MartinZeltin left a reply on HTML Array Not Work With Table

@SANJAYACLOUD - I'm not sure I understand what is needed. Could you give an example of the result and what you are getting?

06 May
1 week ago

MartinZeltin left a reply on HTML Array Not Work With Table

It creates the array properly for me, check out this fiddle

http://sandbox.onlinephpfunctions.com/code/018165f249c7cf37afde77ffe3f62d90bd87330e

<?php

function storeInvoice()
{
    $answersArray = array();
    $i = 1;
    
    $request['product'][] = 1;
    $request['product'][] = 2;
    
    foreach($request['product'] as $answer)
    {
        $answersArray[$i] = $answer;
        $i++;
    }
    
    return $answersArray;
}


print_r(storeInvoice());

MartinZeltin left a reply on How Can I Create Relationship Between Tables In Laravel And Page Limit Depending On User Roles

Could you post your 2 tables and their common column how they are related? And what error are you getting when creating the relationship in your Model?

MartinZeltin left a reply on Vuejs Components In App.js And In .blades

Yes, you can do a dynamic import on the fly

'comp': () => import('./comp-component')

https://vuejs.org/v2/guide/components-dynamic-async.html

03 May
2 weeks ago

MartinZeltin started a new conversation How To Use The $loop Variable In A Controller?

I wanted to get the current iteration for my foreach loop in a controller method but it said that $loop was undefined. Does it only work in a blade template?

foreach ($users as $key => $value) {
    echo "$key: $value" // username: John
    echo "current iteration: $loop->iteration"; // undefined
}
02 May
2 weeks ago

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - Sorry, my bad I had commented out the method on the Model.

It is working now! Thank you so much!

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - Ok, I called first() but still getting error

Newsfeed::where('id', $data->id)->first()->toggleSticky();

Error

"message": "Call to undefined method App\Models\Newsfeed::toggleSticky()",
"exception": "BadMethodCallException",

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - Still getting Call to undefined method toggleSticky()

Here is my Controller action

public function toggleNewsfeedSticky(Request $request)
{
      Newsfeed::where('id', $request->id)->toggleSticky();
}

And here is my Newsfeed model

    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;

    class Newsfeed extends Model
    {
        public function toggleSticky()
        {
            $this->update([
                'sticky' => DB::raw('NOT sticky')
            ]);
        }
    }

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - Yes but how can I make it so that I could call my model like Model::find(10)->toggleFlat();

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - @ROBOROBOK -

It would be nice if I could do this:

Model::find(10)->toggleFlag();

and behind the scenes it would do this

$model = Model::find(10);
if ($model->toggle == 1) $model->toggle = 0;
if ($model->toggle == 0) $model->toggle = 1;
$model->save();

MartinZeltin left a reply on Toggle Boolean

@ROBOROBOK - What's the solution if I want a toggle method on my model?

MartinZeltin left a reply on Toggle Boolean

@INANIELHOUSSAIN - I just came across this solution. But I'm getting an error. I think it's expecting a property called toggleFlag...

{
    "message": "Call to undefined method Illuminate\Database\Eloquent\Builder::toggleFlag()",
    "exception": "BadMethodCallException",
    "file": "/var/www/blog/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",
    "line": 50,
    "trace": ...
29 Apr
2 weeks ago

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

Found where the problem was. I had to call values() on the sorted collection. Now it is correct order.

$latest_activities = $latest_activities->sortByDesc(function ($item, $key) {
            return strtotime($item->activity_time);
})->values();

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

@HONDNL - Here is the original array print_r($latest_activities);

Array
(
    [0] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:48:17
        )

    [1] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:26
        )

    [2] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:41:53
        )

    [3] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:37:43
        )

    [4] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:33:33
        )

    [5] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:06:35
        )

    [6] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:00:26
        )

    [7] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:59:23
        )

    [8] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:58:21
        )

    [9] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:54:50
        )

    [10] => stdClass Object
        (
            [activity_time] => 2019-04-29 11:18:17
        )

    [11] => stdClass Object
        (
            [activity_time] => 2019-04-17 12:40:17
        )

    [12] => stdClass Object
        (
            [activity_time] => 2019-04-17 12:40:17
        )

    [13] => stdClass Object
        (
            [activity_time] => 2019-04-16 17:35:50
        )

    [14] => stdClass Object
        (
            [activity_time] => 2019-04-16 17:10:24
        )

    [15] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:45:36
        )

    [16] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:44:58
        )

    [17] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:22:13
        )

    [18] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:17:55
        )

    [19] => stdClass Object
        (
            [activity_time] => 2019-04-16 15:22:08
        )

    [20] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:32
        )

    [21] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:27
        )

    [22] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:25
        )

    [23] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:22
        )

    [24] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:19
        )

    [25] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:16
        )

    [26] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:48:12
        )

    [27] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:38
        )

    [28] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:37
        )

    [29] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:23
        )

)

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

Here is the original array print_r($latest_activities);

Array
(
    [0] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:48:17
        )

    [1] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:26
        )

    [2] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:41:53
        )

    [3] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:37:43
        )

    [4] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:33:33
        )

    [5] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:06:35
        )

    [6] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:00:26
        )

    [7] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:59:23
        )

    [8] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:58:21
        )

    [9] => stdClass Object
        (
            [activity_time] => 2019-04-29 12:54:50
        )

    [10] => stdClass Object
        (
            [activity_time] => 2019-04-29 11:18:17
        )

    [11] => stdClass Object
        (
            [activity_time] => 2019-04-17 12:40:17
        )

    [12] => stdClass Object
        (
            [activity_time] => 2019-04-17 12:40:17
        )

    [13] => stdClass Object
        (
            [activity_time] => 2019-04-16 17:35:50
        )

    [14] => stdClass Object
        (
            [activity_time] => 2019-04-16 17:10:24
        )

    [15] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:45:36
        )

    [16] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:44:58
        )

    [17] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:22:13
        )

    [18] => stdClass Object
        (
            [activity_time] => 2019-04-16 16:17:55
        )

    [19] => stdClass Object
        (
            [activity_time] => 2019-04-16 15:22:08
        )

    [20] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:32
        )

    [21] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:27
        )

    [22] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:25
        )

    [23] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:22
        )

    [24] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:19
        )

    [25] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:50:16
        )

    [26] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:48:12
        )

    [27] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:38
        )

    [28] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:37
        )

    [29] => stdClass Object
        (
            [activity_time] => 2019-04-29 13:47:23
        )

)

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

@HONDNL - I posted the original and sorted collections above.

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

Original collection

return collect($latest_activities);

[
    {
        "activity_time": "2019-04-29 12:32:40"
    },
    {
        "activity_time": "2019-04-29 12:31:28"
    },
    {
        "activity_time": "2019-04-29 12:30:17"
    },
    {
        "activity_time": "2019-04-29 12:28:07"
    },
    {
        "activity_time": "2019-04-29 12:25:31"
    },
    {
        "activity_time": "2019-04-29 12:24:57"
    },
    {
        "activity_time": "2019-04-29 12:23:56"
    },
    {
        "activity_time": "2019-04-29 12:23:05"
    },
    {
        "activity_time": "2019-04-29 12:21:24"
    },
    {
        "activity_time": "2019-04-29 12:17:35"
    },
    {
        "activity_time": "2019-04-29 11:18:17"
    },
    {
        "activity_time": "2019-04-17 12:40:17"
    },
    {
        "activity_time": "2019-04-17 12:40:17"
    },
    {
        "activity_time": "2019-04-16 17:35:50"
    },
    {
        "activity_time": "2019-04-16 17:10:24"
    },
    {
        "activity_time": "2019-04-16 16:45:36"
    },
    {
        "activity_time": "2019-04-16 16:44:58"
    },
    {
        "activity_time": "2019-04-16 16:22:13"
    },
    {
        "activity_time": "2019-04-16 16:17:55"
    },
    {
        "activity_time": "2019-04-16 15:22:08"
    },
    {
        "activity_time": "2019-04-29 12:43:17"
    },
    {
        "activity_time": "2019-04-29 12:27:18"
    },
    {
        "activity_time": "2019-04-29 12:27:17"
    },
    {
        "activity_time": "2019-04-29 12:27:16"
    },
    {
        "activity_time": "2019-04-29 12:27:15"
    },
    {
        "activity_time": "2019-04-29 12:27:13"
    },
    {
        "activity_time": "2019-04-29 12:27:11"
    },
    {
        "activity_time": "2019-04-29 12:27:08"
    },
    {
        "activity_time": "2019-04-29 12:26:57"
    },
    {
        "activity_time": "2019-04-29 12:24:55"
    }
]

Sorted collection (wrong)

$latest_activities = $latest_activities->sortByDesc(function ($item, $key) {
    return strtotime($item->activity_time);
});

return $latest_activities;


{
    "0": {
        "activity_time": "2019-04-29 12:43:22"
    },
    "1": {
        "activity_time": "2019-04-29 12:37:43"
    },
    "2": {
        "activity_time": "2019-04-29 12:32:40"
    },
    "3": {
        "activity_time": "2019-04-29 12:31:28"
    },
    "4": {
        "activity_time": "2019-04-29 12:30:17"
    },
    "5": {
        "activity_time": "2019-04-29 12:28:07"
    },
    "6": {
        "activity_time": "2019-04-29 12:25:31"
    },
    "7": {
        "activity_time": "2019-04-29 12:24:57"
    },
    "8": {
        "activity_time": "2019-04-29 12:23:56"
    },
    "9": {
        "activity_time": "2019-04-29 12:23:05"
    },
    "10": {
        "activity_time": "2019-04-29 11:18:17"
    },
    "11": {
        "activity_time": "2019-04-17 12:40:17"
    },
    "12": {
        "activity_time": "2019-04-17 12:40:17"
    },
    "13": {
        "activity_time": "2019-04-16 17:35:50"
    },
    "14": {
        "activity_time": "2019-04-16 17:10:24"
    },
    "15": {
        "activity_time": "2019-04-16 16:45:36"
    },
    "16": {
        "activity_time": "2019-04-16 16:44:58"
    },
    "17": {
        "activity_time": "2019-04-16 16:22:13"
    },
    "18": {
        "activity_time": "2019-04-16 16:17:55"
    },
    "19": {
        "activity_time": "2019-04-16 15:22:08"
    },
    "20": {
        "activity_time": "2019-04-29 12:45:46"
    },
    "21": {
        "activity_time": "2019-04-29 12:45:44"
    },
    "22": {
        "activity_time": "2019-04-29 12:43:17"
    },
    "23": {
        "activity_time": "2019-04-29 12:27:18"
    },
    "24": {
        "activity_time": "2019-04-29 12:27:17"
    },
    "25": {
        "activity_time": "2019-04-29 12:27:16"
    },
    "26": {
        "activity_time": "2019-04-29 12:27:15"
    },
    "27": {
        "activity_time": "2019-04-29 12:27:13"
    },
    "28": {
        "activity_time": "2019-04-29 12:27:11"
    },
    "29": {
        "activity_time": "2019-04-29 12:27:08"
    }
}

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

@HONDNL - Still wrong results

$latest_activities = collect($latest_activities)->sortByDesc(function ($item, $key) {
    return strtotime($item->activity_time);
});

result:

{
    "0": {
        "activity_time": "2019-04-29 12:28:07"
    },
    "1": {
        "activity_time": "2019-04-29 12:25:31"
    },
    "2": {
        "activity_time": "2019-04-16 17:10:24"
    },
    "3": {
        "activity_time": "2019-04-29 12:24:53"
    }
}

MartinZeltin left a reply on Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

I tried doing this but still getting wrong results.

$latest_activities = collect($latest_activities)->sortByDesc(function ($item, $key) {
    return Carbon::parse($item->activity_time)->timestamp;
});

result:

{
    "0": {
        "activity_time": "2019-04-29 12:28:07"
    },
    "1": {
        "activity_time": "2019-04-29 12:25:31"
    },
    "2": {
        "activity_time": "2019-04-16 17:10:24"
    },
    "3": {
        "activity_time": "2019-04-29 12:24:53"
    }
}

MartinZeltin started a new conversation Can't Get SortBy To Work With A Timestamp - Getting Wrong Order

I have an array that I converted to a Collection with collect($array) and then tried to sort it by activity_time but it's not sorting it.

$latest_activities = collect($latest_activities)->sortByDesc('activity_time');

But I'm getting this result:

{
    "0": {
        "activity_time": "2019-04-29 12:03:15",
        "activity_title": "User logged in",
    },
    "1": {
        "activity_time": "2019-04-29 11:59:07",
        "activity_title": "User opened profile page",
    },
    "2": {
        "activity_time": "2019-04-16 17:35:50",
        "activity_title": "User logged out",
    },
    "3": {
        "activity_time": "2019-04-29 11:56:40",
        "activity_title": "User commented on",
    }
}
24 Apr
3 weeks ago

MartinZeltin left a reply on Laravel 5.9 Wish List

I'd love to see a whereLike implementation. Perhaps even better integration/optimization for Vue?

23 Apr
3 weeks ago

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

@JLRDW - It's a table with 100k records and growing fast. It seems like group by and order by in my query causes it to go from 0.15 sec to 10 sec

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

@JLRDW - Well aggregate functions don't work without group by. How could I rewrite it without it?

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

@SNAPEY - Already created index for the column I am sorting by but no difference

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

@SNAPEY - Already created index for the column I am sorting by but no difference

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

@SNAPEY - Already created index for the column I am sorting by but no difference

MartinZeltin left a reply on Query Takes 10 Seconds If I Add OrderBy And Paginate

Also if I remove group by then the execution time drops to 15 sec

MartinZeltin started a new conversation Query Takes 10 Seconds If I Add OrderBy And Paginate

I have this query but it is taking 10 seconds to execute. If I remove orderByRaw nad paginate then it only takes 15 ms. But I need to order it and paginate it. Is there another way to do it? (excuse the strange table names)

$inkasso = \App\Models\Inkasso::selectRaw(implode(',', $columns))
                              ->leftJoin('inkasso_items', 'inkasso.id', '=', 'inkasso_items.ink_id')
                              ->leftJoin('end_statuses', 'inkasso.end_status', '=', 'end_statuses.id')
                              ->leftJoin('inkasso_statuss', function($join) {
                                  $join->on(function($query) {
                                      $query->on('inkasso.status', '=', 'inkasso_statuss.id');
                                      $query->orOn('inkasso.parent_status', '=', 'inkasso_statuss.id');
                                  });
                              })
                              ->leftJoin('companies', function($join) {
                                  $join->on(function($query) {
                                      $query->on('inkasso.parent_id', '=', 'companies.id');
                                      $query->orOn('inkasso.grand_parent_id', '=', 'companies.id');
                                  });
                              })
                              ->with([
                                  'inkasso_status:id,description',
                                  'inkasso_parent_status:id,description',
                                  'inkasso_parent_company:id,description',
                                  'inkasso_grand_parent_company:id,description',
                                  'inkasso_end_status:id,description',
                               ])
                              ->orderByRaw(
                                  $this->getOrderBy($options->orderBy, $options->orderDirection)
                                )
                              ->take($options->perPage)
                              ->groupBy('inkasso.id')
                              ->paginate(
                                    $options->perPage, // per page
                                    ['*'],             // columns
                                    'page',            // page query variable name
                                    $options->page     // current page
                                );
19 Apr
1 month ago

MartinZeltin left a reply on How To Sort By A Related Table's Column?

@MKBAT - Thanks, that did the trick! It turned out easier than I thought.

$users = User::with('status')
        ->join('status', 'status.id', '=', 'users.status_id')
        ->orderBy('status.description')
        ->get();
+----+-------------+-----------+------------+----+-----------------+
| id |  username   | status_id | profile_id | id | description  1  |
+----+-------------+-----------+------------+----+-----------------+
|  6 | hangman123  |         4 |          6 |  4 | Administrator   |
| 10 | prince23    |         4 |         10 |  4 | Administrator   |
|  4 | z-baller    |         3 |          4 |  3 | Junior user     |
|  3 | MartinZ     |         5 |          3 |  5 | Moderator       |
|  7 | bookerdude  |         1 |          7 |  1 | New user        |
|  1 | john_ivy    |         1 |          1 |  1 | New user        |
|  2 | billy298    |         1 |          2 |  1 | New user        |
|  5 | willybilly  |         1 |          5 |  1 | New user        |
|  8 | anotheruser |         2 |          8 |  2 | Senior user     |
|  9 | mominator2  |         2 |          9 |  2 | Senior user     |
| 11 | Jacky_Blue  |         6 |         11 |  6 | Winner of Month |
+----+-------------+-----------+------------+----+-----------------+
18 Apr
1 month ago

MartinZeltin left a reply on How To Sort By A Related Table's Column?

@CRONIX - That won't work because I mainly need users and I'm eager loading several related tables not just statuses.

MartinZeltin left a reply on How To Sort By A Related Table's Column?

@CRONIX - That won't work because I mainly need users and I'm eager loading several related tables not just statuses.

MartinZeltin left a reply on How To Sort By A Related Table's Column?

This doesn't seem to be working

$users = User::with('status')->orderBy('status.descriptoin', 'asc')->get();

MartinZeltin started a new conversation How To Sort By A Related Table's Column?

I have two tables (users and statuses) that are related and I would like to sort them alphabetically by the user's status. I can't just sort them by users.status column because then it won't be in alphabetical order.

How could I achieve this if I'm able to get users with statuses like this?

    $users::with('status')->get();

Users table

+----+------------+--------+
| id |  username  | status |
+----+------------+--------+
|  1 | john123    |      5 |
|  2 | billy_joe  |      1 |
|  3 | martin_lee |      5 |
|  4 | two_cents  |      2 |
|  5 | Lucy       |      3 |
|  6 | Bill2      |      4 |
+----+------------+--------+

Statuses table

+----+-------------+
| id | description |
+----+-------------+
|  1 | VIP         |
|  2 | Junior      |
|  3 | Senior      |
|  4 | Banned      |
|  5 | New member  |
+----+-------------+

Result

+---------+------------+------------+
| user_id |  username  |   status   |
+---------+------------+------------+
|       6 | Bill2      | Banned     |
|       4 | two_cents  | Junior     |
|       1 | john123    | New member |
|       3 | martin_lee | New member |
|       5 | Lucy       | Senior     |
|       2 | billy_joe  | VIP        |
+---------+------------+------------+
17 Apr
1 month ago

MartinZeltin left a reply on 100k Rows With() Eager Loading Result In - Too Many SQL Parameters?

@STAUDENMEIR - Oh I see, so I shouldn't use whereIn() with more than 65k bindings? But instead use eager loading when possible?

MartinZeltin left a reply on 100k Rows With() Eager Loading Result In - Too Many SQL Parameters?

@STAUDENMEIR - I just ran a test and I got the error.

Route::get('test', function () {
        
        ini_set('memory_limit', '2G');

        $posts = \App\Models\Post::select('id')->take(70000)->get();
        $comments = \App\Models\Comments::whereIn('post_id', $posts->pluck('id'))->get();

        return $comments->first();
    });

It gave me this error

Illuminate \ Database \ QueryException (HY000)

"SQLSTATE[HY000]: General error: 7 number of parameters must be between 0 and 65535 (SQL: select * from "comments" where "post_id" in (149519, 142920, 147987, 147045, 148070, 142797, 149327, 142862, 142681, 167789, 142910, 143819, 142077, 145681, 140397, 142351, 141390, 143577, 149932, 141714, 140597, 142972, 149902, 142817, 142542, 149336, 140365, 142613, 142209, 140590, 147231, 142969, 142950, 147842, 141567, 140501, 149357, 141789, 145979, 147125, 143841, 145495, 149812, 141522, 148000, 141616, 140843, 149852, 142980, 143532, 149581, 141434, 149312, 149811, 149214, 149161, 142793, 140529, 147594, 142061, 149179, 143644, 142547, 143881, 140968, 145448, 142661, 142370, 145784, 143733, 142106, 149305, 141896, 143218, 146113, 140342, 149275, 142605, 147943, 147041, 142809, 140268, 140162, 140265, 140300, 140288, 140303, 141860, 140304, 140325, 140354, 140356, 140372, 149320, 140402, 140410, 140412, 140416, 149255, 140422, 140431, 140481, 140492, 140508, 140539, 140556, 140573, 174651, 140577, 140639, 140640, 140668, 145462, 140687, 140697, 140700, 140717, 141722, 140725, 140727, 140742, 140759, 140814, 141637, 140822, 140858, 140862, 149332, 149331, 140873, 149314, 140876, 149291, 149290, 140877, 140887, 140888, 140913, 140914, 174520, 140920, 140923, 140931, 140939, 160881, 140949, 140944...................................

MartinZeltin left a reply on 100k Rows With() Eager Loading Result In - Too Many SQL Parameters?

Can you explain what you mean that it doesn't use bindings? How does it eager load it?

I have debugbar installed so I looked at the actual queries executed and this is what it shows

select * from `posts` order by `i_date` desc limit 10 offset 0

select * from `comments` where `comments`.`post_id` in (1, 2, 3, 4, 5, 9, 10, 11, 12, 13) order by `id` desc

Post model

public function comments()
        {
            return $this->hasMany('App\Models\Comments', 'post_id', 'id')->orderBy('id', 'desc');
        }

Comments model is a just plain Model.

MartinZeltin left a reply on 100k Rows With() Eager Loading Result In - Too Many SQL Parameters?

5.7.20

It doesn't look like Laravel is the problem but rather it seems to be a limitation of PHP's PDO.

MartinZeltin left a reply on 100k Rows With() Eager Loading Result In - Too Many SQL Parameters?

@STAUDENMEIR - I'm using Laravel 5.7 with php 7.2 and postgresql 9.3

It looks like PDO stores the number of request parameters in an immutable int, so it cannot be more than 65,535 for some reason.

I don't know if anyone has run into this problem. It looks like the solution could be to split the query in smaller chunks.