0

I have a Laravel project set up which uses vueJs as the frontend and there I need to sort related models naturally. So:

I have a Project model which has a hasMany relationship to a Plannings model. I am displaying all plannings on the show page of the project. And I need to sort the plannings on that page naturally by title.

At the moment my code looks like this (I found the sorting solution here):

ProjectController:

public function show(Project $project)
    {
        if (auth()->user()->id == $project->user_id || auth()->user()->hasPermissionTo('edit_users')) {
            return New ProjectResource($project->load(['plannings' => function ($query) {
                                                $query->orderByRaw('LENGTH(title)', 'ASC')
                                                    ->orderBy('title', 'ASC');
                                                }]));
        } else {
            return response()->json(['message' => 'Kein Zugriff'], 403);
        }

    }

ProjectResource:

public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'user_id' => $this->user_id,
            'plannings' => PlanningResource::collection($this->whenLoaded('plannings')),
        ];
    }

This works for example titles like this: (the array is already in the order of the result of the sorting)

$plannings = [
  'Project #1',
  'Project #2',
  'Project #10', 
  'Project #11', 
  'Project Room 1 #1', 
  'Project Room 1 #2', 
  'Project Room 1 #11'
];

However it does not work for example titles like those: (the array is already in the order of the result of the sorting)

$plannings = [
  'Gang 7 - 8m x 2m LPH 2,5m 300LUX',
  'Gang 8 - 5m x 2m LPH 2,5m 300LUX',
  'Gang 1 - 13m x 2m LPH 2,5m 300LUX',
  'Gang 3 - 13m x 2m LPH 2,5m 300LUX',
  'Gang 4 - 19m x 2m LPH 2,5m 300LUX',
  'Gang 5 - 17m x 2m LPH 2,5m 300LUX',
  'Gang 6 - 19m x 2m LPH 2,5m 300LUX',
  'Gang 2 - 13m x 2m LPH 2,5m 300LUX #2'
];

So I was thinking, maybe I can use the collection method sortBy('key', SORT_NATURAL, true)?

However, I do not know how I would implement that in my ProjectController or ProjectResource. Does someone know a solution to this?

shaedrich
  • 5,457
  • 3
  • 26
  • 42
  • How does it not work? for performance i would always sort in the database – mrhn Jun 17 '21 at 10:03
  • The second array should have the order: Gang 1 - ... Gang 2 - ... Gang 3 - ... and so on. – bFlow Webdesign Jun 17 '21 at 10:08
  • But you are sorting on length? then title? added my answer guess the problem is the order – mrhn Jun 17 '21 at 10:09
  • I guess my problem is not that it does not work as it is supposed to work based on my code, but more that I do not know how I can achieve the right sorting that is not dependent on the length of the string. It simply should sort alphbetically/naturally – bFlow Webdesign Jun 17 '21 at 10:16
  • https://stackoverflow.com/a/36716875/2943403 – mickmackusa Aug 22 '22 at 08:06

0 Answers0