background preloader

Eloquent & DB

Facebook Twitter

Eloquent by Example. Eloquent by Example. Quick Tip: The Convenient Magic of Eloquent Observers. If you’ve used Eloquent on medium to large projects before, you may have encountered a situation where you want to take action when something happens to your models.

Quick Tip: The Convenient Magic of Eloquent Observers

Eloquent provides a convenient way to do so. The Observer Pattern The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. – Wikipedia In our case, Eloquent models can notify us about changes on a given model. Model Events Eloquent provides a handful of useful events to monitor the model state: creating, created, updating, updated, deleting, deleted, saving, saved, restoring, restored. Notice the “ing/ed” difference. creating: Called before saving the new member.created: Called after saving the member. Eloquent also fires similar events that we can listen for.

Seeding Tables

Enforcing referential integrity with the Laravel Schema builder. Intermediate Laravel. MySQL Workbench Tutorial. MySQL Workbench: Visual Database Design. Visual Database Schema Design MySQL Workbench simplifies database design and maintenance, automates time-consuming and error-prone tasks, and improves communication among DBA and developer teams. It enables data architects to visualize requirements, communicate with stakeholders, and resolve design issues before a major investment of time and resources is made. It enables model-driven database design, which is the most efficient methodology for creating valid and well-performing databases, while providing the flexibility to respond to evolving business requirements.

Model and Schema Validation utilities enforce best practice standards for data modeling, also enforce MySQL-specific physical design standards so no mistakes are made when building new ER diagrams or generating physical MySQL databases. Forward and Reverse Engineering MySQL Workbench provides capabilities for forward engineering of physical database designs. Change Management. Schema Builder - Laravel - The PHP Framework For Web Artisans. Introduction The Laravel Schema class provides a database agnostic way of manipulating tables.

Schema Builder - Laravel - The PHP Framework For Web Artisans

It works well with all of the databases supported by Laravel, and has a unified API across all of these systems. Creating & Dropping Tables To create a new database table, the Schema::create method is used: Schema::create('users', function($table){ $table->increments('id');}); The first argument passed to the create method is the name of the table, and the second is a Closure which will receive a Blueprint object which may be used to define the new table. To rename an existing database table, the rename method may be used: Laravel Schema Designer. GitHub - yadakhov/laradump. Spatie/laravel-backup. This Laravel 5 package creates a backup of your application.


The backup is a zipfile that contains all files in the directories you specify along with a dump of your database. The backup can be stored on any of the filesystems you have configured in Laravel 5. Feeling paranoid about backups? No problem! You can backup your application to multiple filesystems at once. Once installed taking a backup of your files and databases is very easy. Php artisan backup:run But we didn't stop there. Spatie is a webdesign agency in Antwerp, Belgium. Postcardware You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium. The best postcards will get published on the open source page on our website. Installation and usage You'll find installation instructions and full documentation on Testing.

Raw() - make your database work - Laravel Daily. When selecting data form the database, sometimes you need to make some extra filtering with results – with some if-else statements and similar.

raw() - make your database work - Laravel Daily

With Laravel – you can achieve that with Accessor fields or just looping through results in PHP. But there is a more effective way – to move the filters to the database query itself. Imagine a real scenario – in DB table you have a field gender with values – 1 for Male, and 2 for Female. But on the page you need to show the texts, not the numbers.

So straightforward way would be IF-statement in blade: But sometimes you don’t have that luxury of filtering – you need the data to come back already processed – the best example is working with from server-side with AJAX calls, it is expecting a valid ready-made JSON as a response. So, we would have to run SQL query with IF-statement. SELECT (CASE WHEN (gender = 1) THEN 'M' ELSE 'F' END) AS gender_text FROM users; Raw queries. Google. Laravel Model Generator. Laravel Model/Migration Generator This utility to help you create new Laravel models on the fly by just defining the model's schema and table name.

Laravel Model Generator

After your definition in the corresponding fields, you will get a code snippet to copy and paste in the model and migration files. First start by defining the models name and if it uses a single or compound key. After that go ahead and defined the desired attributes the model will have. This is a work in progress. 1) Configuration 2) Fields Copy the following code in your migration script and model class for the corresponding defined Laravel model. Display all SQL executed in Eloquent.

Dedicated-Query-String-Filtering/app at master · laracasts/Dedicated-Query-String-Filtering. Dedicated Query String Filtering. Database: Query Builder - Laravel - The PHP Framework For Web Artisans. Database: Getting Started - Laravel - The PHP Framework For Web Artisans.