PHPackages                             crocodic/laravel-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. crocodic/laravel-model

ActiveLibrary[Database &amp; ORM](/categories/database)

crocodic/laravel-model
======================

Boost your laravel database relation with model enhancement

v4.1.3(5y ago)03021MITPHPPHP ^7.2

Since Jan 21Pushed 5y ago3 watchersCompare

[ Source](https://github.com/crocodic-studio/laravel-model)[ Packagist](https://packagist.org/packages/crocodic/laravel-model)[ RSS](/packages/crocodic-laravel-model/feed)WikiDiscussions v4.0 Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (21)Used By (0)

Crocodic Laravel Model
======================

[](#crocodic-laravel-model)

An alternative laravel eloquent. Crocodic Laravel Model using Model, Repository and Service pattern. Model is a class that define anything about table columns. Repository is a class that define your own query method. And Service is a class that should define your own business logic query method.

### Requirement

[](#requirement)

Laravel / Lumen 5.\* | 6.\* | 7.\* | 8.\*

### Install Command

[](#install-command)

`composer require crocodic/laravel-model`

### LUMEN USER:

[](#lumen-user)

after install with the composer then add this bellow to `/bootstrap/app.php` at section `Register Service Providers`

`$app->register(\Crocodic\LaravelModel\LaravelModelServiceProvider::class);`

### 1. Create a model

[](#1-create-a-model)

*Create a model from a table*
`php artisan create:model foo_bar_table`

*Create model for all tables*
`php artisan create:model`

*Create a model with other connection*
`php artisan create:model foo_bar_table --connection=con2`

I assume that you have a `books` table with the structure like bellow:

```
id (Integer) Primary Key
created_at (Timestamp)
name (Varchar) 255

```

It will create a new model class file at `/app/Models/BooksModel.php` with the following file structure :

```

```

### 3. Using Crocodic Laravel Model class that has a relation

[](#3-using-crocodic-laravel-model-class-that-has-a-relation)

I assume you have a table `categories` for book relation like bellow :

```
id (Integer) Primary Key
name (Varchar) 255

```

and your book structure to be like bellow:

```
id (Integer) Primary Key
created_at (Timestamp)
categories_id (Integer)
name (Varchar) 255

```

Now you have to create a model for `categories` table, you can following previous steps.

I assume that you have create a `categories` model, so make sure that now we have two files in the `/app/Models/`

```
/BooksModel.php
/CategoriesModel.php

```

Open the Books model , and add this bellow method

```
    /**
    * @return App\Models\Categories
    */
    public function category() {
        return $this->belongsTo("App\Models\Categories");
    }

    // or
    /**
    * @return App\Models\Categories
    */
    public function category() {
        return Categories::find($this->categories_id);
    }
```

Then open the FooController

```

```

As you can see now we can get the category name by using `->category()->name` without any SQL Query or even Database Builder syntax. Also you can recursively go down to your relation with NO LIMIT.

### 4. How to Casting DB Builder Collection output to Crocodic Laravel Model Class?

[](#4-how-to-casting-db-builder-collection-output-to-crocodic-laravel-model-class)

You can easily cast your simple database builder collection to cb model class.

```
$row = DB::table("books")->where("id",1)->first();

//Cast to Crocodic Laravel Model
$model = new Books($row);

//And then you can use cb model normally
echo $model->name;
```

### 5. How to insert the data with Crocodic Laravel Model

[](#5-how-to-insert-the-data-with-crocodic-laravel-model)

You can easily insert the data with method `->save()` like bellow:

```
$book = new Books();
$book->created_at = date("Y-m-d H:i:s"); //this created_at is a magic method you can ignore this
$book->name = "New Book";
$book->categories_id = 1;
$book->save();
```

Then if you want to get the last insert id you can do like bellow:

```
...
$book->save();
$lastInsertId = $book->id; // get the id from id property
...
```

### 5. How to update the data with Crocodic Laravel Model

[](#5-how-to-update-the-data-with-crocodic-laravel-model)

You can easily update the data, just find it for first :

```
$book = Books::findById(1);
$book->name = "New Book";
$book->categories_id = 1;
$book->save();
```

### 5. How to delete the data?

[](#5-how-to-delete-the-data)

You can easily delete the data, just find it for first :

```
$book = Books::findById(1);
$book->delete();
```

or

```
Books::deleteById(1);
```

Model Method Available
----------------------

[](#model-method-available)

```
/**
* Find all data by specific condition.
*/
$result = FooBar::findAllBy($column, $value = null, $sorting_column = "id", $sorting_dir = "desc");
// or
$result = FooBar::findAllBy(['foo'=>1,'bar'=>2]);

/**
* Find all data without sorting
*/
$result = FooBar::findAll();

/**
* Count the records of table
*/
$result = FooBar::count();

/**
* Count the records with specific condition
*/
$result = FooBar::countBy($column, $value = null);
// or
$result = FooBar::countBy(['foo'=>1,'bar'=>2]);

/**
* Find all datas and ordering the data to descending
*/
$result = FooBar::findAllDesc($column = "id");
// or simply
$result = FooBar::latest();

/**
* Find all datas and ordering the data to ascending
*/
$result = FooBar::findAllAsc($column = "id");
// or simply
$result = FooBar::oldest();

/**
* Find/Fetch a record by a primary key value
*/
$result = FooBar::findById($id);
// or simply
$result = FooBar::find($id);

/**
* Create a custom query, and result laravel Query Builder collection
*/
$result = FooBar::where("foo","=",1)->first();
// or
$result = FooBar::table()->where("foo","=",1)->first();

/**
* Join a table with a simple step
*/
$result = FooBar::table()->withTable("related_table")->first();

/**
* Auto select all from a table, and make them prefix with its table name
*/
$result = FooBar::table()
->join("related_table","related_table.id","=","related_table_id")
->addSelect("foo_bar.*")
->addSelectTable("related_table") // This will produce: related_table_id, related_table_created_at, etc
->first();

/**
* Add like condition to the query
*/
$result = FooBar::table()->like("column",$yourKeyword)->get();
// It will produce same as FooBar::table()->where("columne","like","%".$yourKeyword."%")->get()

/**
* Find a record by a specific condition
*/
$result = Foobar::findBy($column, $value = null);
// or
$result = Foobar::findBy(['foo'=>1,'bar'=>2]);

/**
* To run the insert SQL Query
*/
$fooBar = new FooBar();
$fooBar->name = "Lorem ipsum";
$fooBar->save();

/**
* To bulk insert
*/
$data = [];
$foo = new FooBar();
$foo->name = "Lorem ipsum 1";
array_push($data, $foo);
$bar = new FooBar();
$bar->name = "Lorem ipsum 2";
array_push($data, $bar);
FooBar::bulkInsert($data);

/**
* To run the update SQL Query
*/
$fooBar = FooBar::findById($value);
$fooBar->name = "Lorem ipsum";
$fooBar->save();

/**
* To delete the record by a primary key value
*/
FooBar::deleteById($value);

/**
* To delete the record by a specific condition
*/
FooBar::deleteBy($column, $value = null);
// or
Foobar::deleteBy(['foo'=>1,'bar'=>2]);

/**
* To delete after you fetch the record
*/
$fooBar = FooBar::findById($value);
$fooBar->delete();
```

A One-To-Many Relationship
--------------------------

[](#a-one-to-many-relationship)

```
class Posts extends Model {
    // etc

    /**
    * @return Illuminate\Support\Collection
    */
    public function comments() {
        return $this->hasMany(Comments::class);
    }

    // or with full option
    /**
    * @return Illuminate\Support\Collection
    */
    public function comments() {
        return $this->hasMany(Comments::class, "foreign_key", "local_key", function($condition) {
            return $condition->where("status","Active");
        });
    }
}
```

A One-To-One Relationship
-------------------------

[](#a-one-to-one-relationship)

```
class Comments extends Model {
    // etc

    /**
    * @return App\Models\Posts
    */
    public function post() {
        return $this->belongsTo(Posts::class);
    }

    // or with full option
    /**
    * @return App\Models\Posts
    */
    public function post() {
        return $this->belongsTo(Posts::class, "foreign_key", "local_key");
    }
}
```

Other Useful
------------

[](#other-useful)

1. [CRUDBooster Laravel CRUD Generator](https://github.com/crocodic-studio/crudbooster)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

20

Last Release

1886d ago

Major Versions

v1.0 → v2.02021-01-24

v2.1 → v3.02021-01-27

v3.0.2 → v4.0-alpha2021-03-04

PHP version history (2 changes)v1.0PHP ~7.2|~7.3

v2.0PHP ^7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/45650601?v=4)[ferdevelop15](/maintainers/ferdevelop15)[@ferdevelop15](https://github.com/ferdevelop15)

---

Top Contributors

[![fherryfherry](https://avatars.githubusercontent.com/u/6733315?v=4)](https://github.com/fherryfherry "fherryfherry (22 commits)")

---

Tags

eloquentlaravellaravelmodellumenmodel

### Embed Badge

![Health badge](/badges/crocodic-laravel-model/health.svg)

```
[![Health](https://phpackages.com/badges/crocodic-laravel-model/health.svg)](https://phpackages.com/packages/crocodic-laravel-model)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[ryangjchandler/orbit

A flat-file database driver for Eloquent.

922256.2k5](/packages/ryangjchandler-orbit)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3593.5k](/packages/baril-bonsai)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
