Navigate back to the homepage

Laravel Model

Infinitbility
Laravel
January 20th, 2021 · 1 min read
Laravel Model

Hello Friends,

Welcome To Infinitbility!

In this article, we can create a model in laravel and use it in the controller with an example.

This article only explains the required things to learn and use laravel model.

Model

Eloquent models allow you to insert, update, and delete records from the table as well.

Eloquent

Laravel use Eloquent, When using Eloquent, you have to create a separate model for each table.

Create model

create model using laravel make model cammand

open cmd Or Terminal to your project dir and use below cammand.

1php artisan make:model ModelName

after run Artisan make model cammand, check your project/App/Models folder.

You get ModelName.php file something like below code.

1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class ModelName extends Model
8{
9 //
10}

Add table name in model

add your table name in your model file using the below example.

1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class ModelName extends Model
8{
9 /**
10 * The table associated with the model.
11 *
12 * @var string
13 */
14 protected $table = 'model_name';
15}

Add Primary Key in model

Laravel model uses default id as your model primary key but if you use another name for your primary id column then you need to declare.

1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class ModelName extends Model
8{
9 /**
10 * The table associated with the model.
11 *
12 * @var string
13 */
14 protected $table = 'model_name';
15
16 /**
17 * The primary key associated with the table.
18 *
19 * @var string
20 */
21 protected $primaryKey = 'model_id';
22}

Insert Default Value ( Optional )

when you want to insert default for your any column like is_active => true. laravel model provide options to set.

1/**
2 * The model's default values for attributes.
3 *
4 * @var array
5 */
6 protected $attributes = [
7 'is_active' => true,
8 ];

Use Model to your controller

if you want example to use model in your controller, check below example.

1<?php
2
3namespace App\Http\Controllers;
4
5use App\Http\Controllers\Controller;
6use App\Models\ModelName;
7use Illuminate\Http\Request;
8
9class ModelController extends Controller
10{
11 /**
12 * Store a new modelName in the database.
13 *
14 * @param \Illuminate\Http\Request $request
15 * @return \Illuminate\Http\Response
16 */
17 public function store(Request $request)
18 {
19 // insert new records
20 $modelName = new ModelName;
21
22 $modelName->name = $request->name;
23
24 $modelName->save();
25 }
26}

if you get help, please share a post on your social network

More tutorial form Laravel

  1. Yajra issue after install in laravel
  2. Laravel call function from another class
  3. laravel pagination with customization
  4. How to solve page expired error in laravel for webhooks, ajax, and form
  5. Laravel Model
  6. Laravel Clear cache, config, view and Routes

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

CSS Combinators

CSS Combinators

A combinator is something that explains the relationship between the selectors.

January 15th, 2021 · 1 min read
What is Brute force Attack & How to prevent Brute force Attack?

What is Brute force Attack & How to prevent Brute force Attack?

A brute force attack is a trial and error method to get a login to the system or account. In this method, hackers try to guess the password. They try to guess passwords and try every combination hoping to guess correctly.

January 14th, 2021 · 5 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]