MartinZeltin

MartinZeltin

Member Since 10 Months Ago

Experience Points 18,860
Experience Level 4

1,140 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-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

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

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

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

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    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 Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

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

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

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

21 May
2 months ago

MartinZeltin started a new conversation LeftJoinSub() Inside Another Left Join Subquery?

Is it possible to put a left join subquery inside another left join subquery using Eloquent?

For example, I have this query that I would like to re-write using Eloquent and without using Raw.

I know only how to do one level leftJoinSub().

select
    contracts.id,
    payments.ink_id,
from contracts
    left join (
        select
            contracts.id,
            payments.ink_id,
        from
            contracts
        left join (
            select
                ink_id,
            from
                payments
            group by
                ink_id
        ) payments on payments.ink_id = contracts.id
    ) as payments on contracts.id = payments.ink_id
20 May
2 months ago

MartinZeltin left a reply on How To Pass A Variable To A Function With Callback?

@KUNDEFINE - Yes!!! Thank you! That's what I was looking for! Like Snapey said, it's hard to find something when you don't know what to search for.

function doIt($data, $callback) {
    $callback($data);
}


doIt('my data', function($data) {
    echo 'Anonymous function: ' . $data;
});
19 May
2 months ago

MartinZeltin left a reply on How To Pass A Variable To A Function With Callback?

@SNAPEY - I got it to work. However I have seen code without the use keyword like this. How do they do it?

function doIt($callback) { 
    $callback(); 
}

$data = 'my data here...';

doIt(function($data) {
    echo 'Anonymous function!' . $data;
});

This is giving me these errors:

WARNING Missing argument 1 for {closure}(), called in /home4/phptest/public_html/code.php70(5) : eval()'d code on line 4 and defined on line number 9

NOTICE Undefined variable: data on line number 10

MartinZeltin left a reply on How To Pass A Variable To A Function With Callback?

@SNAPEY - how do I pass something to the call back? Now i'm getting undefined $data in two places.

function doIt($callback) { 
    $callback($data); 
} 

doIt(function() use ($data) {
    echo 'Anonymous function!' . $data;
});

MartinZeltin left a reply on How To Pass A Variable To A Function With Callback?

@SNAPEY - Hmm, still getting an error


NOTICE Undefined variable: data on line number 7

WARNING Missing argument 1 for {closure}(), called in /home4/phptest/public_html/code.php70(5) : eval()'d code on line 4 and defined on line number 7
Anonymous function!

Code:

function doIt($callback) { 
    $callback(); 
} 

doIt(function($data) use ($data) {
    echo 'Anonymous function!' . $data;
});

MartinZeltin started a new conversation How To Pass A Variable To A Function With Callback?

I could not find anywhere how to pass a variable to my function that uses a callback.

This is giving me an error

NOTICE Undefined variable: data on line number 2
function doIt($callback) { 
    $callback($data); 
}


doIt(function($data) {
    echo 'Anonymous function: ' . $data;
}); 
15 May
2 months 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
2 months 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
2 months 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
2 months 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
2 months 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 months 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 months 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 months 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 months 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 months 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
3 months 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
3 months 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();