![]() ![]() ![]() Laravel polymorphic relations allow us to work with a single table to store both student and teacher profiles. So, normally we need two tables for students and teachers. A student can have a profile as well as a teacher. Suppose, in a system there are two types of users called Student and Teacher. To run the development server, the PHP artisan command is- php artisan serve Eloquent relationships(Polymorphic): One To One:Ī one-to-one polymorphic relation is similar to a typical one-to-one relation. We’re going to run the development server after every relationship example. Here, the view() function returns a blade file that is stored under the User folder. Inside the controller, we’ll need a function called view to display the data. We’re going to use the name UserController. You can use the php artisan command to create a controller- php artisan make:controller UserController You can practice with different controllers if you want. In this tutorial, we’re going to use the same controller for all the examples. In your laravel project, they are stored in the app/Http/Controllers directory. The controller is a class where we handle requests, implement logic. You can create a route like this- Route::get('/index',)Īfter creating the route, now we need a controller. Because our main concern is to learn all the eloquent relationships. In this tutorial, we’re going to use the same route for all the examples. We need to create a route to view the database results. In our installed laravel project there is a file called web.php. In laravel routes makes a relationship with your laravel controller. You can name your project and database name as you want. ![]() env file on your laravel project and set up the database name. Right? Now the project is installed, we need to set up the database. To work with Eloquent ORM, obviously, we’re going to need a database. With composer installed on your computer, you can download a laravel project with the following command- composer create-project laravel/laravel laravel-eloquent-relations Setup Database The following steps are-īefore we start, we need to download and set up a laravel application. In this tutorial, you’re going to learn about polymorphic relationships step by step with examples. In this tutorial, we will use Sublime Text 3 as a text editor, xampp server, and Google Chrome as a web browser. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |