Laravel Collections: 5 Methods with Real Examples

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024

ความคิดเห็น • 48

  • @daleryanaldover6545
    @daleryanaldover6545 3 ปีที่แล้ว +3

    One thing I realized while learning laravel is that eloquent queries return a collection which means we can use collection methods AFTER getting the result from eloquent query. Been using old php way. Also there is the laravel helpers documentation which is a god send.

  • @dadditsdonovskiy3145
    @dadditsdonovskiy3145 3 ปีที่แล้ว

    Thank you very much for such a short and nevertheless clear and comprehensive explanation.

  • @sakhaprasna
    @sakhaprasna 2 ปีที่แล้ว

    i like it sir
    so useful and clear on explaination
    i was confused in my way solving how to use where like with collection, then i found this video
    many thanks!!! ❤❤❤

  • @andresluisvelasquez1747
    @andresluisvelasquez1747 ปีที่แล้ว

    Gracias por la info

  • @SowrenSen
    @SowrenSen 3 ปีที่แล้ว +1

    Want Tinkerwell features in PhpStorm? Try Laravel Tinker plugin. Super useful one.

  • @qudratxojamusayev6785
    @qudratxojamusayev6785 3 ปีที่แล้ว +1

    Great video. I just wanted to ask if it is a good practice to filter things with php instead of using database queries

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      Good catch. Usually, it's a bad practice, in that example of the video it kinda makes sense because filtering is quite complex with lowercase transformation, so for the same to happen in the database queries, you would probably need to use raw MySQL methods.

    • @ward7576
      @ward7576 3 ปีที่แล้ว

      Always better to go with DB filtering. For that you run one query and hydrate models once (ideally). When you return date from DB and filter it on php, you are hydrating a lot of data where a chunk of it will be just thrown away..since filtering. Not as efficient. It would either way be a good practice to learn some more mysql as you go and optimize things ;)

  • @bsen2267
    @bsen2267 3 ปีที่แล้ว

    Hey @Laravel Daily, do you think it's worth learning VIM ?

  • @pavelmgn
    @pavelmgn 3 ปีที่แล้ว +2

    First example: return array_map(fn($name) => Str::slug($name), self::PROJECT_TYPES);

    • @mallesbixie
      @mallesbixie 3 ปีที่แล้ว +1

      I love collections, but in this case I'd go for the native solution you posted. untested, but I think even `return array_map([Str::class, 'slug'], self::PROJECT_TYPES);
      ` will work

  • @overLordOrigin
    @overLordOrigin 3 ปีที่แล้ว

    thanks

  • @R0b3
    @R0b3 ปีที่แล้ว

    What kind of editor are you using, PHPstorm or vscode?

  • @1234matthewjohnson
    @1234matthewjohnson 3 ปีที่แล้ว

    what are your throughts on when to use map() vs each()? they seem to do a very similar thing.

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      Well, not really, map() returns something and each() is just for performing some operations, it may not return anything.

  • @TahirBhai
    @TahirBhai 3 ปีที่แล้ว

    Thank you for your regular helpful videos, can we expect a video from your channel about mysql database shards in laravel project. I am looking into this now a days but there is no good practical example over there.

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      Not in plans, sharding is a niche topic, too niche. I'm focused on broader topics.

    • @TahirBhai
      @TahirBhai 3 ปีที่แล้ว

      @@LaravelDaily Thank you :) if you can recommend me some readings or video material about this. Eagerly looking for it.

  • @GergelyCsermely
    @GergelyCsermely 3 ปีที่แล้ว

    Thanks!

  • @bagusk_awan
    @bagusk_awan 3 ปีที่แล้ว

    i have a question, what's the difference between $collection->mapWithKeys(...) and $collection->transform(...) ?

  • @shethchaitali8012
    @shethchaitali8012 3 ปีที่แล้ว

    Hello can you tell me which editor did u use and how can i do when i change code at a same tome output showing on right side?

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      It's not editor, it's Tinkerwell app, it's not free

  • @afestacincome2224
    @afestacincome2224 3 ปีที่แล้ว +1

    Do you have a tutorial on how to send post to Twitter

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      Here are a few examples from my Laravel Examples: laravelexamples.com/tag/twitter

  • @thanakornboy1129
    @thanakornboy1129 3 ปีที่แล้ว

    6:20 I think all Str::contain() run although $query contains in title, right?

    • @Remls
      @Remls 3 ปีที่แล้ว

      .

  • @developerdiwakar
    @developerdiwakar 3 ปีที่แล้ว

    Hi Povilas, I have a question. How can we sort collection after concat with pagination? #laravel8

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      It needs a specific example, I can't answer with just this short description.

    • @developerdiwakar
      @developerdiwakar 3 ปีที่แล้ว

      @@LaravelDaily Okay. Suppose we have two collections $biddings = Bidding::with('user')->paginate(10); and $evaluations = Evaluation::with('user')->paginate(10); then I do this $offers = $evaluations->setCollection( $evaluations->concat($biddings)->sortByDesc('created_at')->values() ); It sets the pagination as well as concatenation but failed to sort the whole collection by created_at in desc order. I want the sorting here.

  • @Voltra_
    @Voltra_ 3 ปีที่แล้ว

    I'd recommend to use cursor instead of get if you plan to use multiple methods afterwards

    • @renwar
      @renwar 3 ปีที่แล้ว

      why?

    • @Voltra_
      @Voltra_ 3 ปีที่แล้ว

      @@renwar It returns a lazy collection instead which allows for lazy evaluation instead of eager evaluation (one for loop instead of n for loops)

  • @cardboarddignity
    @cardboarddignity 2 ปีที่แล้ว

    This collect()->...->toArray() is pretty performance unfriendly and is kinda slow. If you have and array and want the same array at the end, it's better to use php native methods, like array_map and array_filter. These have the same exact purpose, just avoiding the heavy collect() method

  • @josuebarros-desenvolvedorw2490
    @josuebarros-desenvolvedorw2490 3 ปีที่แล้ว

    How can we paginate a HTTP response data?
    Example: Requesting to a Real Estate listing and paginate the 500 results in groups of 50

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      It depends on the structure of how those listings are presented to you. So it's very individual, I can't answer in a comment.

    • @josuebarros-desenvolvedorw2490
      @josuebarros-desenvolvedorw2490 3 ปีที่แล้ว

      @@LaravelDaily it is returning as a collection

  • @yuliarahma1300
    @yuliarahma1300 3 ปีที่แล้ว

    can you gave us tutorial or concept about otp using telegram, and fresh laravel jetstream, thanks.

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว

      I don't work with OTP and Telegram.

    • @yuliarahma1300
      @yuliarahma1300 3 ปีที่แล้ว

      @@LaravelDaily can you at least give us an example or which file to modify to change two factor auth from email to telegram? is it possible right?

  • @rishikantsrivastava2444
    @rishikantsrivastava2444 3 ปีที่แล้ว

    Hi Povilas,
    Thanks for your wonderful support to Laravel community and people like me.
    but kindly clear this as well,
    two cases are there,
    Case 1:
    if(statement1||statement2||statement3)
    {
    return;
    }
    Case 2:
    if(statement1)
    {
    return;
    }
    if(statement2)
    {
    return;
    }
    if(statement3)
    {
    return;
    }
    Case 1 : everytime, all three statement will be executed,
    Case 2: Not All conditions will be executed.it might be 1, 1-2, or 1-2-3
    So which is better?

    • @LaravelDaily
      @LaravelDaily  3 ปีที่แล้ว +1

      In theory, separate if statements are better for performance, you're right, only one would be executed. But if it's just a simple comparison without any DB query or other expensive operation, then performance difference would be 0.0001s and code will be longer for no big reason.

  • @devKazuto
    @devKazuto 3 ปีที่แล้ว +4

    I don't like chained conditions. Yes, it's shorter but it's less readable. I'd much rather have code that is easier to read than having that requires to think what could happen in that chain, especially if you have chains like "|| ($a && $b) || (!$c || $d)" and so on.

    • @travholt
      @travholt 3 ปีที่แล้ว

      I agree. The multiple if statements read almost like English and it makes the logic very easy to understand at a quick glance.