Osijek,
Croatia
+385 95 578 4732
MON - FRI, 4PM - 10PM
ivan@javorovic.com
REPLY IN 24 HOURS
Ivan Javorović profile image

Ivan Javorović

Full Stack Developer

  • BORN : October 3, 1995
  • NICKNAME : Jawe
  • FAVORITE BAND : The Beatles

10 tips for beginners in Laravel

3,608 views

10, Laravel, php, OOP, Tips, Visual Studio code, Beginner, Relationships

1. Install a good code editor


I wish I knew the importance of a good code editor before I started programming in general.
They can significantly increase how fast and clean you can code.

Not to mention there are a number of very useful plugins which you can use for imports, color picking,
code snippets, minifying files etc.


A couple of great code editors I'd like to recommend are Visual Studio Code and Sublime!


2. Learn basics of PHP and OOP (object oriented programming)


I've personally had about 1.5 years of experience in working with PHP before i started anything with Laravel.
You don't need that much, take a week or two or a month, watch some tutorials (maybe buy a course on Udemy or buy a book).


A great book on Laravel especially for beginners is Laravel: The Ultimate Beginner's Guide to Learn Laravel Step by Step - 2nd edition (2020)!


And then start with Laravel. The most helpful way in the beginning (that i found) was to watch youtube videos and code as the
person is also coding.


Getting used to writing PHP code helps a lot more than just watching someone else type.


3. Set a good foundation for your project


Before starting any project you need a plan, an idea of how everything is going to work.


Personally i like to write the idea down into a text editor (just snippets) and then get working on the database.
The database structure is really one of the most important things in a project.


A good way to visualize how your database is going to look is with vertabelo.

It's a simple FREE online tool for building a DB structure.


4. Build something you're passionate about


Building a real world project (and not just copying a video) is the best way you can learn.

With this you'll get use to not relying on someone else (except google).

Build something you are passionate about, no matter if it's a blog, social media site etc.


The reason why this is so important is because it will keep you motivated,
it's something you want to build and finish (also it's a lot of fun). Some sites I've built I sometimes went
on 38 hour coding sessions!



5. Laravel helper functions


For the longest time I've been searching and recreating functions in Laravel that were already handed to me
but i didn't know about. LARAVEL HELPERS are awesome.


They are global PHP functions, which means you can use them anywhere within your application!


For example assets() gets the url of your public directory which means quick access to your css, js etc., str_slug() makes a slug out of a passed string, last() gets the last value in an array, etc.


There are over 100 helper functions within Laravel, USE THEM!


6. Don't feel bad for googling everything


At the beginning i felt bad for always googling for answers because i felt like i wasn't learning fast enough,
but, guess what? Everyone is doing it. Google is a programmers best friend.


I've been in the Laravel "world" for over 2 years now and I'm still googling everything about it.


Of course with practice you'll learn most of the classes and functions, but it takes time!


7. Use commands to generate files


Laravel comes with various commands which you can use to generate files within your existing project.
You can generate models, migrations, seeds, controllers, middleware etc.


And they are really simple to learn!
(example: php artisan make:model Post) - makes a model
(example: php artisan make:model PostController) - makes a controller



8. Learn about Model naming


Laravel enforces naming conventions on it's models. For example if you name a model Post (as we did in the example
above) Laravel will expect a posts table in the database.


Always name you models in singular e.g. "Post" and you tables will be in plural e.g. "posts"!



9. Use relationships in your Models


At the beggining i use to use join() on everything, but it was really annoying and caused a lot of issues, and more or less
required extra queries to the DB which are not needed anymore.


Relationships help you connect tables within your DB.
There are a few relationship types e.g. one to one, one to many, many to many etc.


Why are they useful? Well glad you asked. Let's say you have a Post model and a PostComment model.

And you want to gather all of the comments for a certain post.


The only thing you have to do is make sure your post_comments table has a post_id column and you're basically set. You just go to your Post model and define a hasMany() relation with the PostComment model. e.g.

public function comments(){
    return $this->hasMany('App\PostComment');
}



10. Debugging


As with any apps you're making you're going to have a few bugs (or many) along the way.


Laravel debugging is generally easy because it usually tells you where the bug is occurring (specific line), but if you still can't solve it
the best way is to just copy the error and google it.


Chances are, someone else has run into the same problem as you!
I found that stackoverflow and the laravel forum are the best sources to resolve your problem!


The community is huge and really helpful! Also when debugging i like to use the dd() (dump and die function) to which
you can pass a variable and it will cleanly show you the contents of the variable!

Very useful way of looking at post request data.



If you want to become an awesome Laravel developer, check out this article fom Toptal. Make sure to check it out!


By: Ivan Javorović