👺
Cheatsheet
  • 👋My cheatsheet
  • 🐘PHP
    • Тернарные операторы
    • Замыкания в PHP
    • Таблица сравнения типов PHP
  • 🟨JS
    • JS cheat sheet
    • Тернарные операторы || ?? &&
    • Таблица сравнения типов JS
    • Область видимости в JS
    • Перебор объектов через for(key in obj)
    • Доступ к свойству через переменную []
    • this в JS
    • Конструктор, оператор "new"
    • bind привязка контекста (this)
    • Замыкания в JS ...
  • ❤️Laravel
    • Laravel websockets
    • Загрузка файлов и пути к ним
    • Vite сборка изображений
    • Vite сборка CSS и JS
    • Vite подключение jQuery
    • Vite подключение Bootstrap
    • Laravel AJAX (with jQuery)
    • Дерево категорий Laravel
    • Laravel + CK-Editor + El FInder
    • Laravel deploy
    • Laravel фасады и сервис провайдеры
    • Отношения
      • Один-к-Одному
      • Один-ко-Многим
      • Многие-ко-Многим
      • Полиморфные отношения
    • Laravel cheatsheet (MAIN)
    • Laravel cheatsheet (BIG)
      • Artisan
      • Auth
      • Blade
      • Cache
      • Composer
      • Config
      • Container
      • Cookie
      • DB
      • Environment
      • Event
      • Eloquent Model
      • File
      • Form
      • HTML
      • Helper
      • Input
      • Lang
      • Log
      • Mail
      • Pagination
      • Queue
      • Redirect
      • Request
      • Response
      • Route
      • SSH
      • Schema
      • Security
      • Session
      • String
      • URL
      • UnitTes
      • Validation
      • View
    • Laravel cheatsheet (BIG PLAIN)
  • 📕PDO
    • SELECT
    • WHERE
    • INSERT
    • UPDATE
    • DELETE
    • JOIN
    • GROUP BY
    • Дерево
    • Дерево ООП
    • Дерево в массив
  • 🐋Docker
    • Docker hub push
    • Docker offline
  • 🥤Gulp
  • 🌊jQuery
  • 🪄Composer
  • 4️⃣Composer PSR-4
  • 🍥RegEx
  • 🐙Git
  • 🧑‍💻Node js
  • 🛍️PHPStorm
  • 💻Zsh
  • 🤖Arduino
Powered by GitBook
On this page
  1. Laravel
  2. Laravel cheatsheet (BIG)

Eloquent Model

Basic Usage
// Defining An Eloquent Model
 class User extends Model {}
// generate Eloquent models
php artisan make:model User
// specify a custom table name
 class User extends Model {
  protected $table = 'my_users';
}
More
Model::create(array('key' => 'value'));
// Find first matching record by attributes or create
 Model::firstOrCreate(array('key' => 'value'));
// Find first record by attributes or instantiate
 Model::firstOrNew(array('key' => 'value'));
// Create or update a record matching attibutes, and fill with values
 Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value'));
// Fill a model with an array of attributes, beware of mass assignment!
 Model::fill($attributes);
Model::destroy(1);
Model::all();
Model::find(1);
// Find using dual primary key
 Model::find(array('first', 'last'));
// Throw an exception if the lookup fails
 Model::findOrFail(1);
// Find using dual primary key and throw exception if the lookup fails
 Model::findOrFail(array('first', 'last'));
Model::where('foo', '=', 'bar')->get();
Model::where('foo', '=', 'bar')->first();
// dynamic
 Model::whereFoo('bar')->first();
// Throw an exception if the lookup fails
 Model::where('foo', '=', 'bar')->firstOrFail();
Model::where('foo', '=', 'bar')->count();
Model::where('foo', '=', 'bar')->delete();
//Output raw query
 Model::where('foo', '=', 'bar')->toSql();
Model::whereRaw('foo = bar and cars = 2', array(20))->get();
Model::remember(5)->get();
Model::remember(5, 'cache-key-name')->get();
Model::cacheTags('my-tag')->remember(5)->get();
Model::cacheTags(array('my-first-key','my-second-key'))->remember(5)->get();
Model::on('connection-name')->find(1);
Model::with('relation')->get();
Model::all()->take(10);
Model::all()->skip(10);
// Default Eloquent sort is ascendant
 Model::all()->orderBy('column');
Model::all()->orderBy('column','desc');
Soft Delete
Model::withTrashed()->where('cars', 2)->get();
// Include the soft deleted models in the results
 Model::withTrashed()->where('cars', 2)->restore();
Model::where('cars', 2)->forceDelete();
// Force the result set to only included soft deletes
 Model::onlyTrashed()->where('cars', 2)->get();
Events
Model::creating(function($model){});
Model::created(function($model){});
Model::updating(function($model){});
Model::updated(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
Model::deleting(function($model){});
Model::deleted(function($model){});
Model::observe(new FooObserver);
Eloquent Configuration
// Disables mass assignment exceptions from being thrown from model inserts and updates
 Eloquent::unguard();
// Renables any ability to throw mass assignment exceptions
 Eloquent::reguard();

Last updated 2 years ago

❤️