Code Monkey home page Code Monkey logo

eloquentjs's Introduction

EloquentJs

Build Status Scrutinizer Code Quality

Brings the Eloquent ORM from the Laravel framework to the browser.

Documentation

Full documentation for the package can be found at the parsnick.github.io/eloquentjs.

Quickstart

Install

composer require parsnick/eloquentjs

and add the service provider

// config/app.php
'providers' => [
    // ...
    
    // before your RouteServiceProvider, add
    EloquentJs\EloquentJsServiceProvider::class,
    
    // ...
],

Usage

  1. Use the trait EloquentJs\Model\EloquentJsQueries in your models

    use EloquentJs\Model\EloquentJsQueries;
    
    class Post extends Model {
        use EloquentJsQueries;
        // ...
    }
  2. Add routes to respond to queries coming in via the client-side library

    (a) Use Route::eloquent($url, $modelClass) to register a set of RESTful routes similar to Route::resource($url, $controllerClass) provided by native Laravel.

    // app/Http/routes.php
    Route::eloquent('api/posts', App\Post::class);

    or (b) for complete control over which methods are permitted, use your own controller logic:

    // app/Http/routes.php
    Route::get('api/posts', function () {
        return App\Post::eloquentJs('where(id|body) orderBy(body|rating|created_at)')
            ->take(50)
            ->get();
    });
  3. Create a javascript build customised for your specific models

    php artisan eloquentjs:generate [--models=*] [--output=public/eloquent.js]
    
  4. Load eloquent.js on a page and start using Eloquent in the browser!

    var Post = Eloquent.Post;
    
    // The API is the same as the Eloquent you already know, as far as practical
    // with a few obvious limitations. Most notably, database operations are 
    // inevitably asynchronous so any method that executes a query will
    // always return a Promise.
    
    var post = new Post({ title: 'My first post!' });
    
    post.save().then(function (post) {
        // carry on...
    });
    
    Post.create({ title: 'My second post!' }).then(function (post) {
        
        console.log(post.exists); // true
        console.log(post.title); // My second post!
        console.log(post.blahblahblah); // undefined
        console.log(post.created_at.getFullYear()); // 2016
        console.log(post.getKey()); // 2    
        console.log(post.getAttributes()); // plain data object with no behaviour
        
        post.title = 'Updated';
    
        console.log(post.getDirty()); // { title: 'Updated' }
    
        post.update({ title: 'Hello!' }).then(function (post) {
            console.log('Saved at: ' + post.updated_at);
        });
    });
    
    Post.whereNotNull('published_at').get().then(function (results) {
        // results.forEach(...);
        // ...
    });
    
    Post.published().latest().get(); // assumes a scopePublished() method on the server
    
    Post.saving(function (post) {
        return !! post.title; // prevent posting without a title
    });

Changelog

1.2
  • Support relations property set in eloquentjs.php config file.
1.1
  • Added an optional eloquentjs.php config file to replace interactive prompts for unknown info during php artisan eloquentjs:generate.
  • Extracted 'update all' and 'delete all' to their own routes: now handles PUT and DELETE to /resource respectively.

Contributing

As a new project, no formal contribution guide exists but feel free to open issues and pull requests. For anything related to the companion javascript library, please use parsnick/eloquentjs-client.

License

MIT

eloquentjs's People

Contributors

scrutinizer-auto-fixer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.