Part 16 - Read our CRUD [How to Build a Blog with Laravel 5 Series]

Поделиться
HTML-код
  • Опубликовано: 17 ноя 2024
  • We already have the Read portion of our Laravel CRUD complete to show a single blog post, but now its time to display our posts.index which can display a lot of blog posts for us to manage.
    This is designed to be our backend of our application and will soon be protected by an authentication system that we build ourselves. But for now its open to the public and allows us to see all our blog posts and then go to either the view or edit screen from this table.
    We will learn to truncate with the substr() function of PHP to only show part of the body of our posts. We will once again practice with our Date() function as well.
    -- DOWNLOAD SOURCE CODE --
    github.com/jac...
    -- MORE FROM THIS SERIES --
    Previous Video [Part 15]: • Part 15 - Working with...
    Next video [Part 17]: Coming Soon
    Full Playlist for the "How to Build a Blog with Laravel" Series: • How to Build a Blog wi...
    -- FOLLOW ME --
    Subscribe for New Releases!
    Twitter - / _jacurtis
    (ask me questions!)
    -- QUESTIONS? --
    Leave a comment below and I or someone else can help you.
    For quick questions you may also want to ask me on Twitter, I respond almost immedietely.
    Thanks for all your support!
    ---
    Tags: Laravel 5.2 Framework CRUD Read Index Tutorial Easy to learn. Best tut about Laravel. Build a blog with Laravel Tutorial. Beginner tutorial. How to video. PHP framework easy best tutorial. Laravel 5 coding in PHP with Alex.

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

  • @maarten1012TTT
    @maarten1012TTT 7 лет назад +10

    For anyone using Bootstrap 4, at 13:00 put "mt-2" in the class next to "col-md-2" to get the spacing correct.

  • @jonathanteeter321
    @jonathanteeter321 8 лет назад

    I seriously look forward to watching your videos all day. You're the best. Thank you for making these. I'm only here on part 16 but I'm definitely going all the way.

  • @jamesfrancisflores7712
    @jamesfrancisflores7712 8 лет назад +5

    Love how you explain things!!! Please keep it up, waiting to complete this series!!!

  • @robspeakman6750
    @robspeakman6750 8 лет назад +8

    Awesome videos! You can also use: {{str_limit($post->body, $limit = 50, $end = '...') }}
    Additionally, you can use the diffForHumans() method to make the created_at value more...readable. {{ $post->created_at->diffForHumans() }}

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад +5

      Thanks, these were two commands I didn't know until a few other people have shared them with me. I fall back on PHP a lot, even though there are these cool Laravel functions to make my life easier :) I will be using those in the future. Thanks for sharing them with me.

    • @marcgonzales9597
      @marcgonzales9597 8 лет назад +4

      str_limit will add the ... so no need for the extra code

  • @abhishekjanjalkar9074
    @abhishekjanjalkar9074 8 лет назад

    Thank you so much for referencing to these offline documentation browsing apps. This will definitely help to some of my friends villages who are eager to learn programming.

  • @phraronraweechainirattisai960
    @phraronraweechainirattisai960 8 лет назад +1

    Thanks so much. You are a great teacher. Your instructions are very clear.

  • @PhoenixRebirthed
    @PhoenixRebirthed 8 лет назад +17

    You can also do it like "{{$post->created_at->format('F d, Y')}}"
    It comes out as September 04, 2016

    • @Kliamframe
      @Kliamframe 7 лет назад

      But it's September 09, 2017??? ;)

    • @tiktokminister5177
      @tiktokminister5177 7 лет назад

      Kliamframe yeah but it was created on september 4 ; 2016

  • @edo76136
    @edo76136 8 лет назад +1

    Hello sir, just started following your tutorial(2 days now...) found it so interesting and helpful i barely get any sleep(I do that secretly at work).. I included updated_at on the table and it came out well..

  • @Exigopro
    @Exigopro 8 лет назад +1

    I think this is a better way to deal with the body truncating:
    {{ str_limit($post->body, 100) }}
    With 100 being the amount of characters you limit the body to.

  • @ThePurpleShrine
    @ThePurpleShrine 8 лет назад

    {{str_limit($string, $limit = 10, $end = '...')}} can be easily use for truncating the string.
    This automatically tests if the body is greater than given limit or not and accordingly places dots at the end.

  • @theDarkewave
    @theDarkewave 7 лет назад

    Am I wrong if I say it would be better to use grids instead of table for responsive design? Especially on small screen devices the table will cause horizontal scrolling. I love your tutorials!

  • @b31tom
    @b31tom 7 лет назад

    These videos are awesome. Thank you!!

  •  6 лет назад

    Great job, Alex!

  • @MakingStuffWithEvan
    @MakingStuffWithEvan 7 лет назад

    I love this series. Thanks so much for making these. :D

  • @JacquesRousseauuBD
    @JacquesRousseauuBD 8 лет назад +2

    Can you pls make authentication with different user role like Admin, Blogger and so on, also with single and multiple image upload along with post

  • @kamaboko1
    @kamaboko1 7 лет назад

    Question regarding buttons. On this tutorial you are using route() for the buttons. On a previous tutorial for the show page you used Html::linkRoute(). How would you characterize the differences between the two and when the most appropriate time to use either one? Thanks.

  • @scwyldspirit
    @scwyldspirit 6 лет назад

    Alex when was Carbon offered in Laravel? Takes the guesswork out of formatting dates

  • @amadeuscam1
    @amadeuscam1 8 лет назад +1

    We can replace with() with compact() function is more flexible

  • @MrWilde
    @MrWilde 8 лет назад

    Hey J, love the training, so easy to follow. That being said I think I missed a bit. Creating the foreach loop, how is $posts made available, I don't see it declared anywhere, is it a mad global from the controller routing to the page?
    FYI: ... or dot dot dot is referred to as an ellipsis. I only mention it as I was saying the same thing only a week or two ago and someone corrected me or rather advised me with a little to condescendingly. Like the first time some told me / is commonly referred to as whack. Must not be that common, I never heard it until this year and I have used computers since 1995. Anyhow, love your work. Keep it coming.

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад +2

      The $post variable is made available because in the controller on line 25 we return the view and we pass in withPosts($posts) which makes a variable called $posts which is equal to $posts in our controller. So that is how it is made available. That is part of the Laravel magic.
      With the ellipsis, I think it is now an official unicode character. So there is a character for it (so the three dots only take up one character space and act like a single letter) in the unicode system. www.fileformat.info/info/unicode/char/2026/index.htm
      It also has its own HTML entity too (…) You might remember using these things back in the day. THe new guys don't appreciate the   and & to make keyboard symbols other than normal letters in html. Luckily the modern web is fairly forgiving now, not as harsh as before. I made my first website back in 1999. So you have some years on me :)
      1995 was the wild west of web. Kind of miss those days. Of course I was young (in my teens) but I remember it vividly. The first websites I saw were like magic. I have been hooked ever since.

    • @MrWilde
      @MrWilde 8 лет назад +1

      its Funny and a really good sign. Reading the question I wrote I am baffled as to why I asked it. The difference between what I knew several weeks ago to what I know now is mind blowing. Your training is a very big part of that, thanks for all the hard work.

  • @madmax1717
    @madmax1717 6 лет назад

    lol I was screaming YOU MISSED A BRACKET!

  • @legendtony9098
    @legendtony9098 6 лет назад

    pls can I still watch this videos, am learning laravel and I notice that this video was made in 2016 but the current version of laravel now is 5.7

  • @AbhimanyuNaikareWebDeveloper
    @AbhimanyuNaikareWebDeveloper 6 лет назад

    At 4:23, you mentioned storing all the data in a variable. through can it be better to store 10 records at a time through navigation of data table?

  • @JohnnyMcCaffery
    @JohnnyMcCaffery 8 лет назад

    With the hr, rather than creating a new div, you can put it outside the row class div

  • @virtumind
    @virtumind 8 лет назад

    great videos !!

  • @RealToOy
    @RealToOy 8 лет назад

    Man your videos are great but i would love it if sometimes tou didnt spend time on exaplaining why you like postS instead of post :P It would be a lot better this way. Thanks for teaching me Laravel :)

  • @kravetzpm
    @kravetzpm 6 лет назад

    I noticed sometimes Laravel developers forget the correct spelling of methods or properties, or the sequence of arguments a function expects. Is there some IDE to typehint those methods the way it works in Android Studio, for example?

  • @Faou47
    @Faou47 7 лет назад +1

    I just cant get the data for the $posts object i passed from the controller to the index page.
    When I use the foreach to loop over the $posts Object i get this error : Invalid argument supplied for foreach().
    And Yes I have everything just like you,EVERYTHING,I've been following this tutorials from the very beginning and everything seems to work smoothly,until now.
    If anyone faces this problem,please provide me with a solution !

    • @tiagoaguiar3246
      @tiagoaguiar3246 7 лет назад

      Hey budy, Have you solved this problem? This error is commonly given when you try to loop a non array object. Try to put the whole @foreach statement in a @if statement just to check it out. Like this:
      if( is_array($posts) ) {
      foreach ( $posts as $post ) {
      {{ $post->id }}
      {{ $post->title }}
      {{ $post->body }}
      {{ $post->created_at }}
      Ver PostEditar
      // endforeach
      @endif
      An error was found
      If the h1 comes out with this error statement, you're not passing an array inside the foreach statement. In this case you should look in your Postcontroler if is there everything in order. A common error is when someone passes
      return view('posts.index')->withPosts('$posts');
      In this case, you need to delete ' ' from $posts, like this:
      return view('posts.index')->withPosts($posts);
      I hope this solve your problem, comment bellow and I'll try to help you.

    • @kobadg
      @kobadg 6 лет назад +1

      I assume you probably already figured out the problem... however I had the same thing...
      My issue was that in the PostController.php file, I did the following..
      return view('posts.index')->withPosts('$posts');
      While below is the correct...
      return view('posts.index')->withPosts($posts);
      If you have the single quotes in the with()/withPosts() this will also cause that error.

  • @dameryends
    @dameryends 7 лет назад

    Im getting an error saying that "Property[id] does not exist on this collection instance."

  • @radwanshaheen2323
    @radwanshaheen2323 5 лет назад

    Dear :I am practicing this video but I got an error
    resources\views\index.blade.php)
    Previous exceptions
    Undefined variable: posts (0
    can you help please

  • @noorfadzlianaromram1625
    @noorfadzlianaromram1625 6 лет назад

    Hey there , i wanna ask how to make admin can view all form that have been submitted by user. For example the form for vehicle registration in order to apply for valid sticker. I had used crud in VehicleController and index function return only form that submit by that particular user only. My problem is i want to display all form that been submitted to admin.

  • @aleeawann
    @aleeawann 8 лет назад

    hey Jacurtis, when will we work on the user login and authentication system and session page and profile page of users? and also the admin panel... I think that is something that should come in the begining and we haven't yet worked on it. I was watching laracasts and jeffery said that authentication is somthing you should do in the begining of a project.

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад +3

      +Alee Awan Authentication starts in part 26.
      Yes I agree that if I am writing an application today for myself, you would want to do authentication first thing. That is how I usually do it as well. However, because we are learning along the way, we couldn't do it first thing because we hadn't learned about controllers, migrations, middleware, crud, MVC structure, models, query builder, etc. These are all things I wanted to teach first so it wasn't overwhelming to those of you learning for the first time. But now that I have taught these things, then yes the authentication would be one of the first things you set up when building a real world application.

  • @devaiaizarry2235
    @devaiaizarry2235 8 лет назад

    If some of you guys wanted the time still have AM and PM at created at you can use the date format :)

    • @devaiaizarry2235
      @devaiaizarry2235 8 лет назад

      +devai aizarry oh HAHA! +Jacurtis already did it at the end hehe

  • @HussamadinAbushelha
    @HussamadinAbushelha 8 лет назад

    Why did you used Route ('posts.create')and not Html::linkRoute as in show page?

  • @farhan_aftab
    @farhan_aftab 5 лет назад

    Perfect

  • @subhasreemazumder3687
    @subhasreemazumder3687 7 лет назад

    Hi, Thanks for your amazing tutorials. They make learning super easy. Why have you not used Html::linkRoute in here instead of

  • @shqawe
    @shqawe 8 лет назад

    Hi Alex,
    i don't know why you don't use
    $post->created_at->format(d-m-y)
    with date i think is better than date function don't you think that?

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад +2

      I didn't know that you could do that! Thats an eloquent thing I guess and I haven't seen that before. I like it, nice and easy. I will be using that in the future. Thanks for sharing.
      I come from the old PHP days when you had to do all this stuff the long way. These little quick easy helpers are mostly new to me. I keep discovering new ones. Somoene the other day told me about str-limit() which I didn't know about either. I always used long PHP functions before that. There are lots of these little helpers like that and many aren't documented so I simply didn't know about it.
      From now on i will be using it though.

    • @leokhan2077
      @leokhan2077 8 лет назад

      Use of undefined constant d - assumed 'd' (View: W:\domains\myblog.com
      esources\views\posts\show.blade.php)

    • @Valentin1448162
      @Valentin1448162 8 лет назад +1

      You should put this as a string. For example if you want the same result as the previous video just do:
      {{ $post->created_at->format('M j, Y H:i') }}

    • @leokhan2077
      @leokhan2077 8 лет назад

      ooh, thx y so much, i was not attentive .

  • @mattwagg123
    @mattwagg123 8 лет назад

    How do I add a stylesheet without the html helper.... mine currently looks like this to get out of the partials folder/view/resources and then into public...........href=".../public/css/styles.css"> any help?

    • @harkaransa
      @harkaransa 8 лет назад

      add this in your _head.blade.php page

  • @aleeawann
    @aleeawann 8 лет назад

    Dear Jacurtis, I thank you for your continuous support, There is an issue I am facing, anything I put in my public directry, it does not work, first I put a logo for my blog in the public directory but it did not show up, then in this lesson as we put a custom style sheet, it does not work as well, the style is not applied although i can see the path when i view page source.. why is that happening? here is the error when I click on the style sheet link in the view page source..
    The requested resource /css/mystyle.css was not found on this server.

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад

      +Alee Awan and you have a folder called "css" in your public folder right?
      Basically the public folder is your base URL, then if you want to add stuff in there then you just add folders inside public and you can navigate it with the URL. So if you have a folder called "awesome" and a stylesheet called "styles.css" in there then you would use localhost:8000/awesome/styles.css if you don't have a css folder then you just access it as styles.css without the css folder in the url structure.

    • @aleeawann
      @aleeawann 8 лет назад

      yes, I have a folder called "css" and the stylesheet resides in there. still not working.

    • @devaiaizarry2235
      @devaiaizarry2235 8 лет назад

      +Alee Awan can you try to access your CSS at the URL for example( localhost:8000/css/mystyle.css) like +Jacurtis said,?
      if you can access it then your good, check the link names of your code maybe you mispelled it or something

    • @Skyler.Finance
      @Skyler.Finance 8 лет назад +1

      I am stucked with the same thing, my css/styles.css is in public yet it is not found

  • @aliraza-uk7dw
    @aliraza-uk7dw 7 лет назад

    Trying to get property of non-object (View: E:\xampp\htdocs\blog
    esources\views\posts\show.blade.php)sir when i access index,blade.php i get this error.

  • @BorislavRazvanski
    @BorislavRazvanski 8 лет назад

    I am using cyrillic alphabet in my post body so I have to use {{ substr($post->body, 0, 20) }}{{ mb_strlen($post->body) > 20 ? "..." : "" }} but when I use it I have an empty result in table body! Where I have to declare like procedural php coding
    mb_internal_encoding('utf-8') to make the things worked?

    • @BorislavRazvanski
      @BorislavRazvanski 8 лет назад

      I find my mistake. I changed the code to {{ mb_substr($post->body, 0, 20) }}{{ mb_strlen($post->body) > 20 ? "..." : "" }} and everything is Ok

  • @GURURAJug
    @GURURAJug 8 лет назад

    Hi Sir, why can't you show pagination here itself...

  • @javadheidari6603
    @javadheidari6603 8 лет назад

    is there a way to make this table mobile friendly with its width?

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад

      The default tables are not mobile friendly. I usually use a plugin called TableSAW to make it mobile friendly (but we wont use it in this series).
      www.filamentgroup.com/lab/tablesaw.html

  • @ДаниелГригоров-г2д

    I have "ErrorException in a15211d4f6a3e7f216bd9719bc2a122b26952bf5.php line 31:
    Undefined variable: posts (View: C:\laragon\www\Bloggie
    esources\views\posts\index.blade.php)" when I try to load index of all posts.In PostController.php I have also
    public function index()
    {
    $posts = Post::all();
    return view('posts.index')->withPost($posts);
    }

  • @Tapoedrape
    @Tapoedrape 8 лет назад

    when doing the strlen function if its under 50 char i get a 0 why is this happening? very strange :( i want this to work so i can use this in the future great tut btw

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад

      That is odd, i have no idea. Check the php.net docs on it for more info php.net/manual/en/function.strlen.php

    • @Tapoedrape
      @Tapoedrape 8 лет назад +1

      I did the tut over again and the problem is solved so I think I made a coding mistake

  • @IngPedroAraujo
    @IngPedroAraujo 7 лет назад

    What is the difference between these two lines of code?
    {!! Html::linkRoute('posts.show', 'View', [$post->id], ['class'=>'btn btn-default btn-sm']) !!}
    View
    Of not being different when we should use one or the other?

  • @NathanBudd
    @NathanBudd 6 лет назад

    Bad semantics using a TH tag in the table body. Why didn't you just use CSS? :first would work fine.

  • @ashkanabbasi7948
    @ashkanabbasi7948 7 лет назад +6

    HEY man, i dont know who u are or where u live, but im living in iran and you cause i changed my life and got 2000$ salary per month , i dont know how i compensation for u thanks a looot

  • @xiaopo
    @xiaopo 7 лет назад

    mb_substr replace substr

  • @ziaullah6548
    @ziaullah6548 8 лет назад

    wow I did not get error.... at 34:28

  • @nicoandriano2210
    @nicoandriano2210 7 лет назад

    Per interrompere a parola conclusa e non trocare
    {{strlen($post -> body) >35 ? substr( $post -> body, 0, strrpos( substr( $post -> body, 0, 35), ' ' )).'[...]': $post -> body}}

  • @Skyler.Finance
    @Skyler.Finance 8 лет назад +1

    localhost:8000/css/styles.css is accessible
    but none of the following imports stylesheet in the head
    {{ Html::style('css/styles.css') }}
    {{ Html::style('/css/styles.css') }}
    {{ HTML::style('css/styles.css') }}
    {!! HTML::style('css/styles.css') !!}
    Although this way of importing worked perfectly while importing parsley.css
    At first localhost:8000/css/styles.css was not accessible so I gave it permissions through chmod, not the file is accessible but still now placed in head

    • @SabinSipai
      @SabinSipai 7 лет назад +1

      instead of above code you can use the cod below:

    • @bre3eze983
      @bre3eze983 6 лет назад

      this is what worked for me in laravel 5.6 :