Laravel Collection Chains vs Arrays: Simple Demo

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024

Комментарии • 34

  • @pilotcoding7129
    @pilotcoding7129 2 года назад +7

    Excellent approach would love to use in Laravel, but as often I'm working on different languages and frameworks i prefer classic way for welcoming juniors as well to take over for the changes etc

  • @szymonpszczoka3284
    @szymonpszczoka3284 2 года назад

    I came from JS world so it's really worth learning Collection :D

  • @yu5uy
    @yu5uy 2 года назад +1

    Keep going amazing videos

  • @giacomogaravaglia6742
    @giacomogaravaglia6742 2 года назад

    in therms of performances which one is the best? With collection it's more elegant and readable, but sounds too many loops on it, isnt it?

    • @LaravelDaily
      @LaravelDaily  2 года назад +1

      I will shoot a demo about it. The difference in performance is almost non existent.

    • @giacomogaravaglia6742
      @giacomogaravaglia6742 2 года назад

      @@LaravelDaily i think this should be the focus. Thanks

  • @yashpatel7291
    @yashpatel7291 2 года назад

    I prefer to use the collection in Laravel.

  • @TheSnower123
    @TheSnower123 2 года назад

    Como hago para que en un $data = collect()->put('name', 'jesus' ); pueda hacer $data->name;

  • @Voltra_
    @Voltra_ 2 года назад +5

    When chaining more than one method, I recommend transforming it into a LazyCollection first to reduce time complexity (*Model::query()->cursor()* for DB, *collect()->lazy()* for the rest)

    • @parlor3115
      @parlor3115 2 года назад

      Thanks, I didn't know we had things like this in PHP / Laravel.

  • @phil86_
    @phil86_ 2 года назад +1

    Great example, not so different from Stream API and related methods (map, filter, collect) we see in Java

  • @Buscalia
    @Buscalia 2 года назад +1

    In my humble opinion prefer the pure php method for simple tasks 'cause any call to Laravel's commands implies more code to be executed for the server. I'm most for a compromise between code easy to read and performance. But, another time, this is simply my humble opinion.

    • @LaravelDaily
      @LaravelDaily  2 года назад

      "more code to be executed" - do you have any time measurements for that? Cause I think it's a myth, there's almost no difference in performance from what I've tested.

    • @Buscalia
      @Buscalia 2 года назад +1

      @@LaravelDaily Nope, I haven't any measurement, only a little math assertion support my preference, that's: 1 > 0 . More and more, not only performance could be affected IMO, RAM resources could be involved also. But I insist this is my humble opinion about that. Thanks for your time and useful teaching work, sincerely.

  • @burakozcan9769
    @burakozcan9769 2 года назад

    hey;
    thanks for video ıt's a helpful video but I have a question how to define two dimensional array? ( $arr[][] ) thanks for answer

  • @joshedwards6545
    @joshedwards6545 2 года назад +1

    In this instance this looks less readable than the original for each. I love using collections and chaining with maps etc but I think in this example the regular php method would be preferable.

  • @GergelyCsermely
    @GergelyCsermely 2 года назад

    Thanks

  • @juniordev4190
    @juniordev4190 2 года назад

    Whenever i see the collection methods in laravel docs i always think why php array methods are not as simple as this.

  • @soniablanche5672
    @soniablanche5672 2 года назад

    as someone who also works with javascript, I am used to array method chaining.

  • @jeffbee6090
    @jeffbee6090 2 года назад

    love it!

  • @unitaxi1644
    @unitaxi1644 2 года назад

    PLS make full course about Laravel collection

  • @sirnt7
    @sirnt7 2 года назад

    I believe sometime Laravel collection is quite slow than array. E.g: Collection filters..

    • @LaravelDaily
      @LaravelDaily  2 года назад +1

      Do you have any real tests with measurements?

    • @sirnt7
      @sirnt7 2 года назад

      @@LaravelDaily yup, recently i implemented the reporting feature which is contain a lot of data. I figured out the colection filter is very slow instead of get all data at the begining and make them as array (index by foreign keys)

    • @sirnt7
      @sirnt7 2 года назад

      hopefully I can give you an example later.

    • @LaravelDaily
      @LaravelDaily  2 года назад +1

      Yes without certain example I can't comment. In my tests, I didn't see much difference.

  • @darkestbrush604
    @darkestbrush604 2 года назад

    How to make a comment reply system in livewire with recursion? This concept is seriously so hard to understand!

    • @wahyu_pratamabwi
      @wahyu_pratamabwi 2 года назад

      You can use nested hasMany() in model. Example:
      /**
      * Get all of the comments for the Comment
      *
      * @return \Illuminate\Database\Eloquent\Relations\HasMany
      */
      public function comments(): HasMany
      {
      return $this->hasMany(Comment::class);
      }
      /**
      * Get all of the children for Comments
      *
      * @return mixed
      */
      public function childrenComments()
      {
      return $this->comments()->with('childrenComments');
      }

    • @darkestbrush604
      @darkestbrush604 2 года назад

      @@wahyu_pratamabwi yeah I have already done all those things but Idk how to edit comments or replies in real time

    • @wahyu_pratamabwi
      @wahyu_pratamabwi 2 года назад

      @@darkestbrush604 just use the comment id to edit

    • @darkestbrush604
      @darkestbrush604 2 года назад +1

      @@wahyu_pratamabwi bro I know all these things but look there will be 3 forms one for the main comment one for the reply and one for the edit comment I have already applied the 1st 2 steps but idk how to edit the comment if you want i can show the source code

    • @wahyu_pratamabwi
      @wahyu_pratamabwi 2 года назад

      @@darkestbrush604 sure, can I make a pull request to your github repository?