2

I have a little problème on sorting an array according to a value using Laravel, what i did is converting it into a collection with:

$array = collect($array)

Then i sorted it with:

$array = collect($array)->sortBy($sortingValue)

The result here is a collection, so the next step was to get my data as an array back again:

$array = collect($array)->sortBy($sortingValue)->values()

I then wrote this code between semicolons because it's nested in an html code, so got something like this:

{{$array = collect($array)->sortBy($sortingValue)->values()}}

This is working pretty good, i do have my array sorted according to the parameter i pasted to the function, the problem i have is that this is causing the results to show up on my page in a text format, at the place im executing this code!

Any idea on where this comes from ? im pretty sure it's because of the nested code, but i didn't figure out how to hide that text from my page, and only use the results for the rest of my project.

Thank you.

12
  • between semicolons? also why the blade echo tags for a php statement like that? Commented Jun 19, 2020 at 14:03
  • @lagbox Cause it's nested in a html file, is'nt the right way for doing that ? Commented Jun 19, 2020 at 14:12
  • ; ... ; semicolons? just trying to figure out what semicolons have to do with this at the moment :) Commented Jun 19, 2020 at 14:14
  • @lagos, if i dont put them, the code only ouputs the operation as a string.. Commented Jun 19, 2020 at 14:18
  • 1
    @TimLewis Thank's for the clarification, very helpful, didn't know about that ! Commented Jun 19, 2020 at 14:36

1 Answer 1

3

Using {{ }} to assign a variable is not the correct approach, as it is shorthand for echoing (printing) the code to your view. If you want to assign a variable in a .blade.php file, use the @php directive, or raw PHP tags:

@php $array = collect($array)->sortBy($sortingValue)->values(); @endphp
<?php $array = collect($array)->sortBy($sortingValue)->values(); ?>

Then, later in your code, you can do:

@foreach($array as $record){
  {{ $record->id }}
  ...
@endforeach

(or any other, valid property)

Sign up to request clarification or add additional context in comments.

5 Comments

Thank's a lot :)
No problem :) One final consideration, if you can define this conversion/sort inside the Controller, and pass it to the view from there, it'll clean up your code a little (although sometimes it's impossible to avoid, just a thought)
@TimLwis, clearly a good idea, in this project, i'm actually taking back other's developer code, so a bit complicated to refactor all of it at this point, but clearly the way to go in my future projects :)
Ah, quite understandable; refactoring and writing new code require separate approaches sometimes, I get that. As long as you're aware of the options moving forward, and what is and isn't good practice, etc. then you're fine :)
Exactly, thank's again @TimLewis, also for teaching me english haha !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.