GithubHelp home page GithubHelp logo

tareq1988 / wp-eloquent Goto Github PK

View Code? Open in Web Editor NEW
566.0 25.0 132.0 52 KB

Eloquent ORM for WordPress

Home Page: https://tareq.co/2015/05/eloquent-orm-in-wordpress/

PHP 100.00%
wordpress orm eloquent database mysql

wp-eloquent's Introduction

Eloquent Wrapper for WordPress

This is a library package to use Laravel's Eloquent ORM with WordPress.

Package Installation

To install this package, edit your composer.json file:

{
    "require": {
        "tareq1988/wp-eloquent": "dev-master"
    }
}

Now run:

$ composer install

Usage Example

Basic Usage

$db = \WeDevs\ORM\Eloquent\Database::instance();

var_dump( $db->table('users')->find(1) );
var_dump( $db->select('SELECT * FROM wp_users WHERE id = ?', [1]) );
var_dump( $db->table('users')->where('user_login', 'john')->first() );

// OR with DB facade
use \WeDevs\ORM\Eloquent\Facades\DB;

var_dump( DB::table('users')->find(1) );
var_dump( DB::select('SELECT * FROM wp_users WHERE id = ?', [1]) );
var_dump( DB::table('users')->where('user_login', 'john')->first() );

Creating Models For Custom Tables

You can use custom tables of the WordPress databases to create models:

<?php
namespace Whatever;

use WeDevs\ORM\Eloquent\Model;

class CustomTableModel extends Model {

    /**
     * Name for table without prefix
     *
     * @var string
     */
    protected $table = 'table_name';

    /**
     * Columns that can be edited - IE not primary key or timestamps if being used
     */
    protected $fillable = [
        'city',
        'state',
        'country'
    ];

    /**
     * Disable created_at and update_at columns, unless you have those.
     */
    public $timestamps = false;

    /** Everything below this is best done in an abstract class that custom tables extend */

    /**
     * Set primary key as ID, because WordPress
     *
     * @var string
     */
    protected $primaryKey = 'ID';

    /**
     * Make ID guarded -- without this ID doesn't save.
     *
     * @var string
     */
    protected $guarded = [ 'ID' ];

    /**
     * Overide parent method to make sure prefixing is correct.
     *
     * @return string
     */
    public function getTable()
    {
        // In this example, it's set, but this is better in an abstract class
        if ( isset( $this->table ) ){
            $prefix =  $this->getConnection()->db->prefix;
            
            return $prefix . $this->table;
        }

        return parent::getTable();
    }

}

Retrieving All Rows From A Table

$users = $db->table('users')->get();

foreach ($users as $user) {
    var_dump($user->display_name);
}

Here users is the table name without prefix. The prefix will be applied automatically.

Other Examples

Writing a Model

use \WeDevs\ORM\Eloquent\Model as Model;

class Employee extends Model {

}

var_dump( Employee::all()->toArray() ); // gets all employees
var_dump( Employee::find(1) ); // find employee with ID 1

The class name Employee will be translated into PREFIX_employees table to run queries. But as usual, you can override the table name.

In-built Models for WordPress

  • Post
  • Comment
  • Post Meta
  • User
  • User Meta
use WeDevs\ORM\WP\Post as Post;

var_dump( Post::all() ); //returns only posts with WordPress post_type "post"

Filter Post by post_status and post_type

use WeDevs\ORM\WP\Post as Post;
var_dump(Post::type('page')->get()->toArray()); // get pages
var_dump(Post::status('publish')->get()->toArray()); // get posts with publish status
var_dump(Post::type('page')->status('publish')->get()->toArray()); // get pages with publish status

How it Works

  • Eloquent is mainly used here as the query builder
  • WPDB is used to run queries built by Eloquent
  • Hence, we have the benfit to use plugins like debug-bar or query-monitor to get SQL query reporting.
  • It doesn't create any extra MySQL connection

Minimum Requirement

  • PHP 5.6.4+
  • WordPress 4.0+

Author

Tareq Hasan

wp-eloquent's People

Contributors

asaquzzaman avatar dsge avatar joecrescoll avatar mirzazeyrek avatar monkeymonk avatar polevaultweb avatar sabbir1991 avatar shelob9 avatar sohelamin avatar sultann avatar szepeviktor avatar tareq1988 avatar yevhenmikulenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-eloquent's Issues

leftJoin prefix

Hello.
It is not very convenient to work with the table prefix.
When you do DB::table ('posts') you do not have to specify a prefix when you make this request. But if you do ->leftJoin ('wp_postmeta') you have to specify a table prefix and a field prefix.
I think this will lead to many mistakes. It's easier to specify this prefix in the request.

Problem with the version 1

Hello,

This solution is not in a Tag: #46 . This causes that if we install this package via composer, this solution won't be there, being available only on "dev-master". I could offer it as a PR I think, but I would like to post here first: is there any intention of having this added to a tag version any time soon?

Thanks,

Unable to select then update model

When i select model by id and then save, exception error occurs

use WeDevs\ORM\WP\Woo_Abcd_Connection_Model as Abcd_Connection;
$hihi = Abcd_Connection::find(1);
$hihi->name = 'foo';
$hihi->save();`

image

cant load Model

Hi in trying to use your script but i cant mange to get it working as i don't know where to load the class for Eloquent Models. i jsut get the following error:

Fatal error: Class 'WeDevs\ORM\Eloquent\Model' not found in /vol/www/cefi/wp-content/plugins/owd_escuelas/includes/Models/inscripciones.php on line 9

http://54.84.190.59/cefi/inscripciones/

i'm using it only on my plugin (with plugin boilerplate: wppb.io) so it doesn't load in wp. pls help

the user meta key should be "umeta_id"?

the wp_usermeta data struchre:

CREATE TABLE `wp_usermeta` (
  `umeta_id` bigint(20) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

the user meta key is 'umeta_id'

but in the code of UserMeta model:

protected $primaryKey = 'meta_id';

the $primaryKey is meta_id.

is there some magic method to convert this? or this is just a typo error?

How to use Facades and helper functions?

I want to use some facades like Request Facade and Session Facade, but i can't get it to work.

What about helper functions that laravel provides like (redirect()...etc )?

Wrong Model definition for Comment

The post relationship inside the is wrong and will generate a critical error when you try to to the following:

$comment = Comment::find(2)->post->post_title;

Instead of a hasOne relationship it should be a belongsTo like this:

return $this->belongsTo('WeDevs\ORM\WP\Post', 'comment_post_ID');

many to many

attaching many to many rel doesnt have prefix on the query executed

executed: insert into user_post (user_id, post_id) values (7, 42)
expectation: insert into prefix_user_post (user_id, post_id) values (7, 42)

Mismatch with eloquent

The version of eloquent and models you're using return a QueryBuilder instance in the form of

 [0 => QueryBuilder]

This causes all your replace bindings scripts to fail.

I tried to fix with some code that tested for query builder and then get the bindings from that, but then questionmarks started to appear in queries and stuff just bogged down.

I'd love to use your plugin, but with the current state it doesn't works if you do a fresh composer install.

Release for the current state?

Hey @tareq1988, I know it is a little outdated, but we use your library and it works well. Only thing is we rely on the current master and I was wondering if you could make a release out if? This way we can make sure which version we depend on.

Best regards!
Wolfram

Class not loaded.

It might be a stupid thing but I don't get it.

on plugin main file:

require_once(__DIR__ . '/vendor/autoload.php');

Class file ( Models/TestUser.php ):

namespace App;
class TestUser extends \WeDevs\ORM\Eloquent\Model
[...]

Somewhere else:

use App\TestUser as TestUser;
[...]
 var_dump(TestUser::testFunc());

Error:

Uncaught Error: Class 'App\TestUser' not found in ....

I'm using phpStorm and it gets the namespace.
PD: I've ran composer dump-autoload.

any clue?¿

Issues with case-sensitive folder names

This folder names are not working on php 5.4.13 due to case-sentitivity.

\WeDevs\ORM\Eloquent\Model as Model;
\WeDevs\ORM\Eloquent\WP\User as User;

This lines are causing fatal error class not found error with those folders:

vendor/tareq1988/wp-eloquent/src/wp
vendor/tareq1988/wp-eloquent/src/eloquent

Issue fixed after renaming folder names like this:

vendor/tareq1988/wp-eloquent/src/WP
vendor/tareq1988/wp-eloquent/src/Eloquent

Problem when using "with"

Hi, I got and error when I try to load model with relationship using with eg:

Post::with('meta')->get();

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,' at line 1]
select * from wp_postmeta where wp_postmeta.post_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Declaration of WeDevs\ORM\Eloquent\Database::selectOne($query, $bindings = Array) must be compatible with Illuminate\Database\ConnectionInterface::selectOne

Seeing this after updating the latest version:

<br /><b>Fatal error</b>: Declaration of WeDevs\ORM\Eloquent\Database::selectOne($query, $bindings = Array) must be compatible with Illuminate\Database\ConnectionInterface::selectOne($query, $bindings = Array, $useReadPdo = true) in <b>/var/www/html/clone.staging.com/wp-content/mu-plugins/vendor/tareq1988/wp-eloquent/src/Eloquent/Database.php</b> on line <b>12</b><br />

How do you iterate over a collection?

Maybe I'm doing something wrong, but looking at the docs for Eloquent I'm trying:

class CustomTableModel extends \WeDevs\ORM\Eloquent\Model {
    ...
}

foreach (CustomTableModel::where('user_id', $user_id)->cursor() as $the_activity) {

and I'm getting the following error:

Fatal error: Call to undefined method WeDevs\ORM\Eloquent\Database::cursor() in vendor/illuminate/database/Query/Builder.php on line 1818

Query fields and tables are surrounded with double quotes instead of single

When I try to do a "select all" there are no results. I tried all 3 methods:

$db = \WeDevs\ORM\Eloquent\Database::instance();
$db->table('users')->find(1);
use \WeDevs\ORM\Eloquent\Facades\DB;
DB::table('users')->find(1);
class UserModel extends \WeDevs\ORM\Eloquent\Model {
    protected $table = 'users';
}
UserModel::all();

None of these returns my users. When I get the sql with UserModel::where()->toSql(); the query seems to be build with double quotes arround tables and fields instead of single quotes. Could this be the problem and how can I fix this?

Hooking into Eloquent events

Is there a way to enable support for event dispatching (illuminate/events) with the wp-eloquent library?

I have been trying to use Traits to automatically assign data to a model before its created using the "creating" event, but it never gets fired. Traced it back to the event dispatcher never being setup.

Any pointers on where to look to enable this?

PHP Version Issue

The packages states that the minimum requirement is PHP 5.3. However, its dependencies require a more up to date version of PHP. For example, the Illuminate/Database package requires >=5.5.9.

Query posts by Meta value

Thanks for this nice package!

Is there a way to query posts by meta_value? I thought something like ->whereMeta would do the trick, but this turns out to be a where scope.

Thanks for your work!

Auto-prefix doesn't work on selects, but does on insert.

As title. Example here (prefix is wp):

$supportCategory = SupportCategory::where('category_number', $row['Support Category Number'])->first();

var_dump($this->table); shows support_categories

$supportCategory = SupportCategory::create([
                "category_name" => $row["Support Category Name"],
                "category_number" => $row["Support Category Number"]
            ]);

var_dump($this->table); shows wp_support_categories

Declaration of Database::transaction() must be compatible with ConnectionInterface::transaction()

Hi,

This library looks great, however after including it using composer then attempting to use the Basic Usage example, I get the following error:

Fatal error: Declaration of WeDevs\ORM\Eloquent\Database::transaction() must be compatible with Illuminate\Database\ConnectionInterface::transaction(Closure $callback, $attempts = 1) in /var/www/app/vendor/tareq1988/wp-eloquent/src/Eloquent/Database.php on line 12

Here's my test code.

add_action('admin_init', function() {
    $db = \WeDevs\ORM\Eloquent\Database::instance();
    var_dump( $db->table('users')->find(1) );
    exit;
}, 99999);

I was able to get it working by adding $attempts = 1 to the function \WeDevs\ORM\Eloquent\Database::transaction at Line 302 of app/vendor/tareq1988/wp-eloquent/src/Eloquent/Database.php.

Example:

/**
     * Execute a Closure within a transaction.
     *
     * @param  \Closure $callback
     *
     * @return mixed
     *
     * @throws \Exception
     */
    public function transaction(\Closure $callback, $attempts = 1)
    {
        $this->beginTransaction();
        try {
            $data = $callback();
            $this->commit();
            return $data;
        } catch (\Exception $e){
            $this->rollBack();
            throw $e;
        }
    }

I'm running PHP Version 5.6.34 under Docker (Image: php:5.6-apache) and WordPress 4.9.4

problem with save new model in database

hi
i create a model in wordpress but not save in database:

<?php
class Etekaf extends \WeDevs\ORM\Eloquent\Model {

}
$et = new Etekaf();
$et->name = 'my Name';
$et->save();

and static 'create' method error:

Fatal error: Uncaught exception 'Illuminate\Database\Eloquent\MassAssignmentException' with message 'email' in C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\vendor\illuminate\database\Eloquent\Model.php:404 Stack trace: #0 C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\vendor\illuminate\database\Eloquent\Model.php(259): Illuminate\Database\Eloquent\Model->fill(Array) #1 C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\vendor\tareq1988\wp-eloquent\src\Eloquent\Model.php(19): Illuminate\Database\Eloquent\Model->__construct(Array) #2 C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\vendor\illuminate\database\Eloquent\Model.php(527): WeDevs\ORM\Eloquent\Model->__construct(Array) #3 C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\dyedd-extra-option.php(96): Illuminate\Database\Eloquent\Model::create(Array) #4 C:\xampp\htdocs\daneshjooyar\wp-settings.php(254): include_once('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\daneshjooyar\wp-co in C:\xampp\htdocs\daneshjooyar\wp-content\plugins\dyedd-extra-option\vendor\illuminate\database\Eloquent\Model.php on line 404

MySqlGrammar

Hello,
first thanks for wp-eloquent, a very useful wrapper!

I want to ask you if there is any particular reason that hasn't allowed you to use MySqlGrammar (instead of Grammar base class).

By doing a couple of quick tests I noticed that using MySqlGrammar as Grammar
use Illuminate\Database\Query\Grammars\MySqlGrammar as Grammar;
and commenting in Database:: bind_params
$ query = str_replace ('"', '', $ query);`
which generates wrong sql for json statements (due to the quotes) you could use Laravel's JSON Where clauses

For example:
Social::where ('meta->facebook->account->email', '[email protected]')->get ();

Improvements on `bind_params` function

Right now, I have a hacky way to build the SQL query by binding params and I think its not efficient. Today I came to know that it fails when there is any ? (question mark sign) in the attributes.

It is here

How does it connect to db?

How does this connect it to the right database ? There is no config.

I´ve got en error when i try this
`$db = \WeDevs\ORM\Eloquent\Database::instance();

var_dump( $db->table('users')->find(1) );`

The error says:
`
Fatal error: Uncaught Error: Call to a member function get_results() on null in /var/www/html/test/vendor/tareq1988/wp-eloquent/src/Eloquent/Database.php:136 Stack trace: #0 /var/www/html/test/vendor/illuminate/database/Query/Builder.php(2082):

How Do I Create (Drop) Tables For Custom Models In The Database?

When I create new tables, I usually would create them with a register_activation_hook hook and callback and drop them with register_deactivation_hook … How can I make sure the tables for custom models are going to be created at activation and dropped with the deactivation of my plugin?

Thanks for this repository by the way! Really made things more flexible, then the usual WP_Query search!

Multiple database connections in a loop

Hello Tareq, thank you for this wonderful project, it's been a great help!

I have an issue however that I haven't been able to solve in almost a day which is very unusual for me. Basically what's going on is that I have to pull some statistical data from multiple wordpress sites. I've created a csv config file listing all databases I need to access and so on.
I'm looping over all these dbs creating new $wpdb object at the top of my loop like so:

$wpdb = new wpdb( $host['dbuser'], $host['dbpass'], $host['dbname'], $host['host'] );
$GLOBALS['wpdb'] = $wpdb;

I then use my eloquent models to access some data along these lines:

$options = WPOptions::where('option_name','=','blah')->first();
$vehicles = Vehicles::all();

and so on. At the end of my loop I unset all database objects I created by using unset() and setting them to null to top it off.

Problem is the data I get back for each individual host and it's database is from the very first connection, so it looks like $wpdb stays static since first being created even though I unset() it and create a new one. I'm positive I'm using new db creds on each iteration so It's completely flabbergasting why this is happening.

All the databases are on separate hosts, and I watched tcpdump traffice to these hosts when I run my script, it's totally fine. I also setup query logging on these boxen, and I see my source host connecting but it's not running any queries these model requests should generate, so $wpdb object is created but it's not being used by /tareq1988/wp-eloquent/src/Eloquent/Database.php, only the first one is, not any of the new ones created at each loop iteration.

Please let me know if you have any ideas on how I can get this fixed.

Thank you so much!!!

how to use select raw with model ?

When I try to do this:

Model::select(DB::raw('query'))
->whereNull('deleted_at')
->orderBy('id')
->get();

I get this error:

Deprecated: Non-static method WeDevs\ORM\Eloquent\Database::raw() should not be called statically

Problem current time

field create_at and update_at not work with current_time wordpress ?
i think use carbon package

Can't install with Laravel 5.2

I have added this to my composer.json file:

"tareq1988/wp-eloquent": "dev-master"

this is the result after composer update:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.2.30
    - Conclusion: don't install laravel/framework v5.2.30
    - Conclusion: don't install laravel/framework v5.2.29
    - Conclusion: don't install laravel/framework v5.2.28
    - Conclusion: don't install laravel/framework v5.2.27
    - Conclusion: don't install laravel/framework v5.2.26
    - Conclusion: don't install laravel/framework v5.2.25
    - Conclusion: don't install laravel/framework v5.2.24
    - Conclusion: don't install laravel/framework v5.2.23
    - Conclusion: don't install laravel/framework v5.2.22
    - Conclusion: don't install laravel/framework v5.2.21
    - Conclusion: don't install laravel/framework v5.2.20
    - Conclusion: don't install laravel/framework v5.2.19
    - Conclusion: don't install laravel/framework v5.2.18
    - Conclusion: don't install laravel/framework v5.2.17
    - Conclusion: don't install laravel/framework v5.2.16
    - Conclusion: don't install laravel/framework v5.2.15
    - Conclusion: don't install laravel/framework v5.2.14
    - Conclusion: don't install laravel/framework v5.2.13
    - Conclusion: don't install laravel/framework v5.2.12
    - Conclusion: don't install laravel/framework v5.2.11
    - Conclusion: don't install laravel/framework v5.2.10
    - Conclusion: don't install laravel/framework v5.2.9
    - Conclusion: don't install laravel/framework v5.2.8
    - Conclusion: don't install laravel/framework v5.2.7
    - Conclusion: don't install laravel/framework v5.2.6
    - Conclusion: don't install laravel/framework v5.2.5
    - Conclusion: don't install laravel/framework v5.2.4
    - Conclusion: don't install laravel/framework v5.2.3
    - Installation request for tareq1988/wp-eloquent dev-master -> satisfiable by tareq1988/wp-eloquent[dev-master].
    - Conclusion: don't install laravel/framework v5.2.2
    - Conclusion: don't install laravel/framework v5.2.1
    - tareq1988/wp-eloquent dev-master requires illuminate/database 5.0.* -> satisfiable by illuminate/database[v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.27, v5.0.28, v5.0.33, v5.0.4].
    - don't install illuminate/database v5.0.0|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.22|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.25|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.26|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.27|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.28|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.33|don't install laravel/framework v5.2.0
    - don't install illuminate/database v5.0.4|don't install laravel/framework v5.2.0
    - Installation request for laravel/framework 5.2.* -> satisfiable by laravel/framework[v5.2.0, v5.2.1, v5.2.10, v5.2.11, v5.2.12, v5.2.13, v5.2.14, v5.2.15, v5.2.16, v5.2.17, v5.2.18, v5.2.19, v5.2.2, v5.2.20, v5.2.21, v5.2.22, v5.2.23, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.29, v5.2.3, v5.2.30, v5.2.4, v5.2.5, v5.2.6, v5.2.7, v5.2.8, v5.2.9].

A facade root has not been set

When I try to get data like this
$result = DB::table('wp_users')->get();
I get an error
Fatal error: Uncaught RuntimeException: A facade root has not been set
But at the same time, if I make a request as follows
$result = User::get();
I get all records from db without error (Moreover, the User class is redefined and extended from the Model class, that is, this is not the class described in your library)
How to fix this? Because I need to use custom queries via DB::statement ()

DB::beginTransaction() Not Working

I'm using "beginTransaction" but i don't know why it's not committed in my DB please give me any solution .

use \WeDevs\ORM\Eloquent\Facades\DB;
use WeDevs\ORM\WP\Employee as Employee;

DB::beginTransaction();

Employee::create(['name' => 'taylor3']);
 
DB::commit();

Below DB details and table details please check you will getting better idea

-- phpMyAdmin SQL Dump
-- version 4.6.6
-- https://www.phpmyadmin.net/
--
-- Host: 192.168.21.84
-- Generation Time: Sep 28, 2017 at 12:21 PM
-- Server version: 5.7.19-0ubuntu0.16.04.1
-- PHP Version: 7.0.22-0ubuntu0.16.04.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `project_wpmultisite`
--

-- --------------------------------------------------------

--
-- Table structure for table `wp_employees`
--

CREATE TABLE `wp_employees` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `wp_employees`
--

INSERT INTO `wp_employees` (`id`, `name`, `created_at`, `updated_at`) VALUES
(21, 'taylor', '2017-09-28 05:52:23', '2017-09-28 05:52:23'),
(22, 'taylor', '2017-09-28 05:53:35', '2017-09-28 05:53:35'),
(36, 'taylor3', '2017-09-28 06:46:15', '2017-09-28 06:46:15');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `wp_employees`
--
ALTER TABLE `wp_employees`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `wp_employees`
--
ALTER TABLE `wp_employees`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=37;

Installation instructions?

I've installed this in a plugin but when I try to use {{ $users = $db->table('users')->get() }} I am just getting Undefined variable: db

DB::commit() not saving changes to database

I'm making modifications to two tables in a given time and trying to avoid concurrency problems, so I'm using DB::beginTransaction();

Everything executes just fine, but when I do DB::commit() nothing changes in database. If I execute the exact same code without the transaction, everything goes just fine.

imagen

This is my code, I need to add the like to the liked_videos table and increase or decrease the likes/dislikes values on the videos table. If I run this exact code without the DB::commit() it works.

Working code:
imagen

I also tried to use DB::transaction($callback) to achieve this, but didn't work either. My database is like this:

Initial State:
liked_videos table
imagen

videos table
imagen

After not working code (with transaction and commit) it remains the same.

If executed without the beginTransaction and commit it updates the tables to this:

After working code State:
liked_videos table
imagen

videos table
imagen

I've also tried to run the transaction directly on the database to check if the problem was on my MySql server, but it worked just fine.

I need some advice on see what could be wrong with this or which file I should check in order to submit a pull request with a possible solution.

Thanks in advance.
Greetings

N00best.

update dependency

"illuminate/database": "5.0.*"

some people already have this as a dependency (lumen 5.1) and require ^5.1

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.