
Explore migrations, seeders, and factories, then master Laravel's query builder and Eloquent ORM to define relationships from 1-to-1 to polymorphic and build complex web apps.
In this tutorial, we will cover what we will learn during this course by diving deep into topics such as database configuration, migration operations, relationships, and polymorphic relationships. You'll be guided through practical examples and real-world scenarios, allowing you to gain a comprehensive understanding of how to use Laravel's database features.
This tutorial covers how you can find the source code for each video in the course. The code is available on Github, with each video having its own branch where the corresponding code is stored. The final project code is only available to Udemy members, and is included as a zip file in the video content.
In this tutorial, I will guide you through setting up a new Laravel 10 project. I will show you how to configure your database credentials and perform a newly created Artisan command to verify that they have been set up correctly.
This video will cover the database configuration file, which specifies how environment variables from the .env file are used.
This video covers migrations in Laravel. When working with PHP, you have probably created database tables using tools like PHPMyAdmin or a database client. However, Laravel provides an easier way to manage database changes through migrations. Migrations keep track of changes to the database schema over time, allowing you to modify your database schema using PHP code. Laravel provides several helpful tools to make managing your database schema as easy as possible.
In this tutorial, we will explore the Blueprint methods that can be used to build columns in Laravel. With over 50 available methods, it is not possible to cover all of them in detail. However, you can find a link to all available methods here: https://laravel.com/docs/10.x/migrations#creating-columns
This tutorial explains how to run migrations through the CLI. Additionally, we will cover the extra commands Artisan provides for running migrations, such as refreshing them, checking their status, running fake migrations, and resetting migrations.
In this tutorial, we will explore the most important column modifiers that are available on the table object methods. Column modifiers allow you to chain methods to the table object, enabling you to change the behavior of a column.
In this tutorial, we will explore how to clean up applications with numerous migrations by squashing them into a single, cleaner migration. This process can help tidy up applications with a large number of accumulated migrations, leading to a faster and more efficient application.
In this tutorial, we will learn how to modify columns in migrations without losing data that is already present in those columns.
In this tutorial, we will learn how to easily rename database columns in Laravel.
In this tutorial, we will learn how to easily drop database columns in Laravel.
In this tutorial, we will remove two migrations that we created for previous tutorials but are no longer necessary moving forward.
In this tutorial, we will explore the difference between factories and seeders, and when to use each. Model factories are primarily used for testing purposes, as they enable you to generate large amounts of fake data. Seeder classes, on the other hand, are used to insert data that is essential for the system to function properly. Consider user roles, which can be utilized throughout the entire application.
In this tutorial, we will create and define our first factory class using Artisan.
In this tutorial, we will demonstrate how to put our first Factory Class to work by running it through a Seeder.
In this tutorial, we will demonstrate how to put our first Factory Class to work by running it through Artisan Tinker. Artisan Tinker is a command-line tool that allows you to interact with your Laravel application's code and data through a command prompt. It allows you to experiment with your application and test various features without having to go through the entire process of launching the application.
In this tutorial, we will create and define our first seeder class.
In this tutorial, we will refactor our Seeder class. When the Seeder class contains many posts, it can become messy. To avoid this, I recommend using a JSON file that we can decode inside the Seeder class and put into use.
In this tutorial, we will explore how to easily run multiple seeders using the DatabaseSeeder class.
In this tutorial, we will explore the importance of correctly using primary and foreign keys in databases. Primary keys are unique identifiers for each record in a database table, while foreign keys are used to link records in different tables together. Understanding how to use these keys effectively is crucial for designing and managing a well-functioning database system.
In this tutorial, we will finally start using the power of Laravel. The power of Laravel, in my opinion, comes from this part: working with databases. One of its most notable features is its elegant syntax for interacting with databases. Laravel's query builder is an excellent tool for most database interactions.
In this tutorial, we will explore a couple of helper methods of the Query Builder that make it easy to retrieve a single row from a database table.
In this tutorial, we will explore Laravel's pluck method. This method is used to retrieve a single column's value from the first result of a query. It is very useful when you need to retrieve a single value or list of values from the database.
In this tutorial, we will examine the insert method for inserting one or multiple rows into a database. We will also use the insertOrIgnore method, which only inserts data into a database table if the data does not already exist in the table.
In this tutorial, we will introduce a method that I honestly haven't used very often before. The insertGetId method enables you to insert a new record into a table and retrieve its ID in a single query.
This tutorial covers how to update data in a database using Laravel's update method. We will also explore the where method and orWhere method, which are useful for updating a single row or rows based on a condition. Additionally, Laravel's updateOrInsert method can be used to update an existing record or insert a new one if it does not exist.
For integer columns, Laravel provides the increment, decrement, and incrementEach methods. These methods enable you to increase an integer value in the database by a given number.
In this tutorial, we will explore how to delete data using the Query Builder.
In this tutorial, we will cover the following four aggregate methods in Laravels Query Builder: count(), max(), min(), and avg().
This tutorial covers the whereNot and orWhereNot methods in the Query Builder. These methods are mostly used to exclude specific data from query results.
In this tutorial, we will cover the exists method, which is used to check if a value exists, and the doesntExist method, which is used to check if a query result doesn't exist.
This tutorial covers the whereBetween and whereNotBetween methods in the Query Builder. These methods are useful when you need to retrieve records that fall within or outside of a specific range of values.
This tutorial covers the dump die, and dd methods in Laravel. These are useful methods for debugging because they allow you to see the exact contents of a variable at a specific point in your code.
This tutorial covers Database Transactions. Database transactions are a way of ensuring data consistency and integrity in your application, because It basically allows you to ensure that a series of database operations are performed as a single unit of work. This can be useful in situations where you need to perform multiple database operations together and want to ensure that they all succeed or fail as a single unit.
This tutorial covers pessimistic locking. Pessimistic locking is a technique used to prevent conflicts between multiple users when accessing the same resource.
This tutorial covers the chunk() method. The chunk() method retrieves a set number of rows and then processes them. Once it has finished processing those rows, it retrieves the next set and repeats the process. This continues until all the rows have been retrieved and processed.
This tutorial covers how to use the lazy() and lazyById() methods in Laravel to efficiently retrieve and process large datasets without overwhelming the server's memory. The tutorial explains the differences between the methods and provides examples of how to use them in practice, including chaining helper methods to perform operations on the retrieved data. The tutorial concludes by highlighting the benefits of using these methods to improve application performance when dealing with large datasets.
This tutorial discusses various raw methods available in Laravel's query builder. These methods allow developers to write their own custom queries from scratch, providing more flexibility in retrieving specific data from a database. The tutorial covers selectRaw, whereRaw, havingRaw, orderByRaw, and groupByRaw methods, explaining how to use them with code examples. It is aimed at intermediate to advanced Laravel developers who want to improve their database querying skills.
This tutorial discusses three different methods for sorting Laravel queries: orderBy, latest, and oldest. The orderBy method allows sorting by any column, while latest and oldest are specifically designed for sorting by the created_at timestamp. The advantages and differences of each method are also explored. The tutorial includes code examples for each method and describes how to use them effectively.
This tutorial discusses the use of full text indexes in Laravel Query Builder. Full text indexes are useful for efficiently searching for words or phrases in a text column, especially in applications that involve searching through large amounts of text. Compared to MySQL's standard search functionality, full text indexes in Laravel Query Builder provide a more efficient and advanced way to search for words or phrases in a text column. The tutorial covers how to modify a migration to add a full text index to a column and how to use the whereFullText and orWhereFullText methods to add full text "where" clauses to a query for columns that have full text indexes.
This document explains the limit() and offset() methods used to retrieve data from a database in small chunks. The limit() method is used to limit the number of records returned from a query, while the offset() method is used to skip a specified number of records from the beginning of a query. Both methods have their advantages and disadvantages, and it is important to use them appropriately based on your specific needs. When used correctly, they can help improve the performance of your application, reduce memory usage, and make it easier to manage large data sets.
This tutorial covers the advantages and disadvantages of using the "when" method for conditional clauses in queries, as well as providing an example of how to use the method in practice. The tutorial concludes with a summary of the "when" method's benefits and the flexibility it offers in building dynamic and efficient queries.
This tutorial explains how to remove any existing ordering constraints from a Laravel Query Builder query using the reorder method. By doing so, you can apply a different order to the query result set. The tutorial provides step-by-step instructions on how to reorder a list of posts based on a different column and in a different order. The tutorial includes code snippets and screenshots to help you follow along.
This tutorial explains how to use the paginate() method in Laravel to divide large sets of data into smaller chunks or pages. Pagination is a technique that can greatly improve the speed of loading pages and provide a better user experience for users. The paginate() method provided by Laravel's query builder can be used to paginate query results by specifying the number of records to be displayed per page.
This tutorial explains the simplePaginate() method on the Laravel query builder, which is used to paginate records retrieved from a database. The syntax for using simplePaginate() is similar to that of the paginate() method, but with a few key differences. The simplePaginate() method is more efficient than the paginate() method when working with a large dataset, as it uses less memory to paginate records.
This tutorial discusses the cursorPaginate() method, which is a method of pagination that is particularly useful when working with large datasets. It explains the advantages and disadvantages of using this method, as well as when to use it and when to consider other pagination methods. The document also includes code examples and explanations of how to properly implement Cursor Pagination. Overall, this document serves as a comprehensive guide to Cursor Pagination for developers who want to optimize the performance of their applications.
This tutorial focuses on Eloquent, an Object-Relational Mapping (ORM) system included in the Laravel PHP framework. The tutorial covers the advantages and disadvantages of using Eloquent, when to use Eloquent vs Query builder, and the use of a tool named Tinkerwell, which allows developers to interact with their application and database using a simple, intuitive interface. The tutorial does not require the use of Tinkerwell and focuses on teaching Eloquent and its relationships before building a project with everything learned so far.
This tutorial covers the conventions and best practices for working with Eloquent models in Laravel. It explains how models are used to interact with databases and provides guidelines for changing table names, primary keys, timestamps, and other model attributes. By following these conventions, developers can create more consistent and maintainable code when working with Eloquent models. The tutorial includes code examples and demonstrations in Tinkerwell to illustrate how to implement these conventions in practice.
This tutorial covers the $fillable and $guarded properties in Laravel's Eloquent models. These properties control which fields can be mass assigned and which fields cannot be mass assigned, and are important for protecting against mass assignment vulnerabilities. The tutorial explains how to use these properties correctly to make Laravel applications more secure and prevent malicious users from accessing sensitive data. It also includes examples and best practices for using these properties effectively.
This tutorial explores the different methods that Eloquent provides to build up queries in Laravel, including the where() method. By constructing queries with specific conditions, we can retrieve only the data we need, leading to improved performance and flexibility. The tutorial provides several examples of how to use the where() method to create complex queries with multiple conditions, and demonstrates how to paginate the results using the cursorPaginate() method. Additionally, the tutorial discusses the advantages of building queries with the where() method, including improved performance, flexibility, and readability.
This tutorial covers retrieving single models from a database using Laravel. The tutorial covers different methods, including Post::find(), Post::where(), and Post::firstWhere(), and explains the differences between returning NULL and throwing an exception. The tutorial also introduces route model binding as a technique for automatically injecting a model instance into controller methods.
This tutorial provides a detailed guide on how to insert data into a database using Laravel models. It starts by introducing the concept of a model instance and how it is used to insert data into the database. It then demonstrates three different methods to insert data: using model instance, using the fill method, and using the create method. The document also provides detailed instructions on how to use each method and when to use them.
This tutorial discusses how to retrieve data from a database using Eloquent in Laravel. It covers the all() method, which retrieves all records from a table, and its disadvantages in terms of performance when dealing with large datasets. The tutorial then introduces paginate() and cursorPaginate() as more efficient alternatives for pagination when working with large datasets. The tutorial explains the differences between the two methods and provides guidance on when to use each method based on the size of the dataset being queried.
This tutorial explains the firstOrCreate and firstOrNew methods in Laravel's Eloquent ORM. These methods are used to avoid duplicating data in the database and can potentially speed up queries. The tutorial includes examples and explanations of how to use these methods effectively.
This tutorial covers updating data in a database using Laravel Eloquent. It covers various methods for updating data, including updating using a model instance, updating using the update method on a query builder instance, and performing mass updates. The tutorial includes code examples and explanations of how each method works. By the end of this tutorial, you will have a better understanding of how to update data in your Laravel Eloquent application.
This tutorial covers the isDirty(), isClean(), and wasChanged() methods in Eloquent, which are used to track changes to attributes in a model. The tutorial explains how to use these methods to determine whether an attribute has been modified, whether there are any unsaved changes, and whether a specific attribute has been changed since the model was last saved. The tutorial provides examples of how to use these methods, and explains how they can be useful when working with models. The tutorial is aimed at developers who are working with Laravel and need to track changes to attributes in their models.
This tutorial explains the usage of updateOrCreate and upsert methods in Laravel Eloquent to update or insert records in a database table. The updateOrCreate method allows you to update an existing record or create a new one if it does not exist, while the upsert method allows you to update an existing record or insert a new one if it does not exist, using a single query. The tutorial provides examples of how to use both methods and explains their differences and use cases.
This tutorial explains how to delete models in Laravel using the delete, truncate, and destroy methods. The delete method is used to delete a single model instance, while the truncate method is used to delete all records from a table. The destroy method is used to delete multiple records from a table and accepts an array of IDs or just a single ID. Practical examples are provided to illustrate the differences between the methods and how to use them effectively in Laravel.
This tutorial covers how to use soft deleting in Laravel Eloquent. Soft deleting allows you to mark a record as deleted without actually removing it from the database, and can be a useful feature for tracking and restoring deleted records. The tutorial covers how to enable soft deleting, how to soft delete and restore records, and how to permanently delete records using the SoftDeletes trait and various methods such as delete, withTrashed, restore, and forceDelete.
This tutorial on pruning models in Laravel covers how to efficiently remove outdated data from a database using Laravel's Prunable trait. The tutorial explains how to enable the trait, define the Prunable method, and set up a schedule in Eloquent to remove outdated data. By following these steps, users can keep their databases clean and efficient.
This tutorial explains how to create a copy of an existing model in a Laravel application. It demonstrates how to use the replicate method to create a new instance of a model with all the same attributes as the original model. The tutorial also shows how to modify certain attributes to prevent duplication in the database. By the end of the tutorial, viewers will understand how to replicate models in Eloquent and have a better understanding of how to work with Laravel.
This tutorial covers using global scopes in the Laravel Eloquent ORM. The tutorial covers the basics of global scopes, including how to create and apply them to models. It includes two examples, a simple one and a more advanced one, demonstrating how to use global scopes to filter out certain types of records. The tutorial also explains how to temporarily disable global scopes for specific queries.
This tutorial explains the concept of local scopes in Laravel's Eloquent ORM. Local scopes are a powerful feature that allow developers to define a set of reusable queries on their models. These queries can be encapsulated into methods on the model, which makes the code more concise and expressive. Local scopes can be chained together to create more complex queries, and they are an essential tool for any developer working with Eloquent. This document provides simple and advanced examples of how local scopes work, including how to define them and how to use them to retrieve data from related tables.
This tutorial covers the use of dynamic scopes in Laravel. Dynamic scopes allow you to easily filter results based on multiple conditions and adjust your filtering criteria at runtime without the need to modify your code. The tutorial provides a step-by-step guide on how to define and use dynamic scopes in your Laravel application, using the Post model as an example. It also discusses the advantages and potential disadvantages of using dynamic scopes, as well as some best practices to ensure that your code remains easy to maintain in the long run.
This tutorial explains how to use traits in Laravel for code reuse and organization. The tutorial focuses on creating a trait to store scopes for a model, which allows for better organization and maintainability of code. The tutorial includes step-by-step instructions for creating the trait, moving the scopes from the model to the trait, and adding the trait to the model. The tutorial is aimed at Laravel developers who want to improve the organization and maintainability of their code.
This tutorial discusses the importance of relationships in database management, specifically in the context of Laravel. The article explains what relationships are, how they are established using keys, and the different types of relationships. The tutorial also covers the advantages and disadvantages of using relationships, emphasizing the importance of setting them up correctly in large projects to ensure data consistency and accuracy. Overall, the tutorial provides a comprehensive overview of the role of relationships in organizing and managing data effectively.
This tutorial explains the one-to-one relationship in Laravel, which is used when two tables need to be associated with a unique relationship. It is used when one record of the first table is associated with one record of the second table. One use case for a one-to-one relationship is when a table with sensitive information needs to be kept separate from the main table. The tutorial covers how to define a relationship between two tables, how to define a relationship method name, how to define the inverse relationship, how to insert data into related tables using a one-to-one relationship, how to access contact data using the contact property, and how to use the with() method to show relationships. The tutorial also provides code examples throughout to help illustrate the concepts.
This document discusses the One to Many and Belongs To relationships in Laravel. It explains how these relationships can be used to ensure data integrity and reduce redundancy in the database. The document provides examples of defining these relationships in Laravel models and demonstrates how to retrieve related data using Tinkerwell. Lastly, it briefly mentions inserting data through the One to Many relationship. This document is a useful resource for anyone looking to learn about database relationships in Laravel.
This tutorial provides a detailed explanation of many-to-many relationships in Laravel. It discusses why this relationship is more complex than other relationships, provides a real-life example, and explains the importance of pivot tables. The document also includes code snippets and examples to illustrate how to define and work with many-to-many relationships, including attaching, detaching, and updating records.
In this tutorial we will have a look at eager loading. Eager loading is a powerful tool for improving performance in Laravel. By loading related data upfront, you can avoid the need for multiple queries and improve the speed of your application. Just remember to use eager loading when you know you'll need related data for a collection of models, and use lazy loading when you don't.
Explore Laravel's has one through relationship to access a phone number via a company's intermediary model. Learn naming conventions, migrations, and retrieval via eager loading and model access.
This tutorial covers the Has One of Many Relationship in Laravel. It explains how to create a Jobs model and migration, and defines the Has One of Many Relationship between the User and Job models. The tutorial includes step-by-step instructions on how to define the latestJob() and oldestJob() methods, which retrieve the latest and oldest job of a user, respectively.
This document is a tutorial on how to implement the Has One of Many relationship in Laravel. The author, Code With Dary, provides step-by-step instructions on creating the Jobs model and migration, defining the Has One of Many relationship, and using Tinkerwell to test the functionality. The tutorial includes explanations of the latestOfMany and oldestOfMany methods and how they can be used to retrieve specific data from the database.
This tutorial covers the basics of polymorphic relationships, why they are useful, and how they differ from regular relationships. It also discusses the benefits and disadvantages of using polymorphic relationships, including their flexibility, ability to reduce data redundancy, and ease of managing relationships. However, it notes that implementing polymorphic relationships can be complex and may have a performance overhead. Overall, the chapter provides a comprehensive overview of polymorphic relationships in Laravel, highlighting their advantages and potential drawbacks.
This tutorial explains the benefits of using a polymorphic one-to-one relationship, provides a real-life example, and includes code snippets for creating and querying records. By using a polymorphic one-to-one relationship, developers can reduce code duplication, simplify queries, and create a more flexible database schema.
This tutorial covers how to create a one-to-many polymorphic relationship using Laravel between three models: Post, Video, and Comment. It provides an example of a real-life scenario where this type of relationship is useful, and discusses the advantages of using it. The tutorial also covers how to define the relationships between the models, create the necessary models and migrations, and insert data using one-to-many polymorphic relationships in Laravel. By the end of the tutorial, the reader should have a solid understanding of how to implement a one-to-many polymorphic relationship in their own Laravel projects.
This tutorial covers the "one of many" polymorphic relationship in Laravel. It explains what this relationship is, how it differs from the regular one of many relationship, and when to use it. Additionally, the document covers the latestOfMany and oldestOfMany methods available in the polymorphic relationship, and provides code snippets for implementing them in the User, Image, and Post models. Finally, the tutorial demonstrates how to retrieve data using the methods defined in Tinkerwell.
This tutorial explains what a many-to-many polymorphic relationship is and how to set it up in Laravel. The tutorial uses an example web application with three models: Post, Video, and Tag, and shows how to create a polymorphic relationship between them. The tutorial covers how to create a migration to set up the relationship, how to define the relationships in the models, and how to use Tinkerwell to play around with the relationship. The tutorial also discusses the benefits of using many-to-many polymorphic relationships in your code, such as flexibility, code reusability, and simplicity.
Set up a fresh Laravel 10 project using composer, create and configure a MySQL database, and update the .env credentials to prepare a working app.
Install Laravel Breeze to scaffold a simple authentication system with Tailwind CSS, using composer and php artisan breeze:install with blade stack, then migrate and build assets with npm.
Define migrations for tags, categories, and articles with unique name and slug fields and timestamps, then set up foreign keys and a pivot table for article_tag with cascade on delete.
Define fillable properties for article, category, and tag models to enable safe mass assignment. Establish relationships where articles belong to a user and a category, and relate to many tags.
Set up a Laravel app from command line, creating tag, category, and article models with controllers, factories, migrations, and seeders via PHP artisan flags, linking articles to categories and tags.
Seed a Laravel 10 app with factories and seeders, using categories.json and json_decode to populate categories, while generating articles with tags and pivot relations, then run db:seed.
Set up article routes with a resource controller, applying the dashboard prefix and auth middleware to restrict create, update, and delete while exposing index and show to all users.
Set up article frontend pages in Laravel by creating index and show views, plus create, edit, and layouts with app and navbar, wired to the article controller.
Learn to fetch articles efficiently with Laravel's index method by using eager loading of user and tags, moving away from the all call, and paginating results for performance.
Demonstrate the show method with route model binding, retrieving an article via slug, and rendering its show view with related user, category, and tags data.
Learn to implement Laravel 10 create and store for articles: validations via form requests, dynamic category and tag selection, slug generation, tag attachment, and redirect with a success message.
Explore editing and updating articles in Laravel, including building an edit view, wiring the update action, fetching form data for categories and tags, using patch requests, and syncing tags.
Implement the destroy method in the article controller to delete an article and redirect to dashboards with a success message, using a front-end form with hidden _method delete and csrf.
Learn to enforce article ownership with Laravel policies by creating an article policy, registering it, and using authorize resources to allow only the owner to update or delete.
Are you struggling with Laravel databases and Eloquent? Do you want to learn how to easily manage your database with migrations and seeders? Look no further than Code With Dary's "Mastering Laravel Databases & Eloquent" course on Udemy! This comprehensive course covers the ins and outs of Factories, Seeders, Migrations, Query Builder, Eloquent, and relationships in Laravel 10. With Code With Dary's expert guidance, you'll learn how to efficiently manage your database and build powerful, scalable applications. Don't miss out on this opportunity to take your Laravel skills to the next level - enroll in "Mastering Laravel 10 Query Builder, Eloquent & Relationships" today!
What is Laravel?
Laravel is the go-to PHP web application framework for developers. With its extensive set of features and tools, Laravel enables developers to create web applications in a fast and efficient manner. One of the standout features of Laravel is its powerful database management system, which runs on top of the Eloquent ORM. Using Laravel and Eloquent, developers can seamlessly perform CRUD operations on the database, as well as execute complex queries with ease. Learning Laravel and Eloquent is a must for any PHP developer who needs to build modern, scalable web applications with confidence.
Why is Laravel so popular?
Laravel is popular because it provides developers with a powerful set of tools and features that make it easy to build modern, scalable web applications. It has a robust database management system that runs on top of the Eloquent ORM, which allows developers to easily perform CRUD operations on the database and execute complex queries. Laravel also has a large and active community of developers, which means that there are many resources available for learning and troubleshooting. Additionally, Laravel is easy to learn and use, which makes it a great choice for developers of all skill levels.
Who is this course for?
Some people may find Eloquent ORM difficult because it has a lot of features and can take time to fully understand. Additionally, working with complex relationships between database tables can sometimes be challenging. However, with practice and experience, developers can become proficient in using Eloquent to manage databases in Laravel applications. This course will teach you everything in depth about Laravel databases, Query Builder, and Eloquent! I do expect you to have knowledge in Laravel before starting this course, since we’re going to focus purely on databases and eloquent.
Tools we will be using
We need to use a code editor for this task. It doesn't matter which one you use. I prefer using tools such as Ray from Spatie and Tinkerwell from BeyondCode. Both products are paid, but not required for this task. I use them in this course to make data outputting easier and to show you query speed, which can be compared to other queries.