Laravel Eloquent multiple database - laravel

I have multiple database connected into my web app.
These are: write and read
I have this eloquent database query:
$users = User::whereIsAdmin(0)->get();
I want this db query to use the read database since by default my app is reading in the write database.
How would I do that?
Thanks!

I looked at the source code of Laravel Eloquent Models, and here:
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Model.php#L303
It says on should be used to change connections.
So:
User::on("read")->whereIsAdmin(0)->get();

Related

What can I use in place of DB.Where

In Gorm we can use DB.Where to check the data of the user from the database for authentication. But I have to use Go Fiber so what can I use in place of DB.Where to check if the user is present in the database.
var user models.User
database.DB.Where("id = ?", claims.Issuer).(&user)
Well, as per your question gorm is working as a database also as a framework.
you can use go fiber as a framework but if you want to use the database you cannot use the database from fiber instead you have to use a database from which you can find the collection of the data in the database.
For example, you are using MongoDB with GoFiber so it will use the method of DB.Collection.FindOne()
In form Db.Where is used to find the database collection
Well, as per your question gorm is working as a database also as a framework.
you can use go fiber as a framework but if you want to use the database you cannot use the database from fiber instead you have to use a database from which you can find the collection of the data in the database.
For example, you are using MongoDB with GoFiber so it will use the method of DB.Collection.FindOne()
In form Db.Where is used to find the database collection

How to use spatie / laravel-multitenancy with single database?

I want to convert my existing app to multi-tenant and don't want to use multiple databases. just read that the given package is allowing that.
https://github.com/spatie/laravel-multitenancy
I have read the single database documentation here: https://spatie.be/docs/laravel-multitenancy/v2/installation/using-a-single-database
But what's next ??
Let's say right now users are storing into users' tables, and by using this package I want to store users tenant-specific.
I don't found the real example anywhere. so better if anyone already implemented Spatie Multi-tenant by using a single Database, please help me here.
Thanks in advance.
Although spatie/laravel-multitenancy supporting single databases, it doesn't come with query scopes (and seems like there are no plans to add them, as you can see here: https://github.com/spatie/laravel-multitenancy/issues/124), so you would need to create them manually.
So, you need to:
add the tenant_id to your tenant-specific tables
add an observer to save the tenant_id when creating a new register in the table
add a global query scope to always filter the data using the current tenant id
You can see something similar in this video: https://www.youtube.com/watch?v=nCiNqboYFVQ
The difference is that you will use the current tenant returned by spatie/laravel-multitenancy instead of the logged user id to get the tenant data.
If you want a package that already brings the query scopes ready to use, you may try Tenancy for Laravel: https://tenancyforlaravel.com/docs/v3/single-database-tenancy
Or, if your application has a very simple multi-tenancy strategy (for example, the tenant is the logged user or his team) you can just create some query scopes without the need to use packages, exactly as the video above teaches, as it is much more simple to identify the tenant by the user_id or team_id, even in terminal commands and queue jobs (but seems that is not your case, as you need the users to belongs to a tenant).
I hope it helps you to select the correct package and strategy for your project.

Codeigniter: Database driver is not currently loaded

I got installed codeigniter 3 in a wampserver 3 with mysql, and the connection to the DB configured, and all is working well, models have connection and queries are returning data, however, when i'm using the profiler library, specially the function:
$this->output->enable_profiler(true);
It returns me the follow:
I just want to see the queries that were runned but is showing:
Database driver is not currently loaded
Any idea about how to solve this? It is like the profiler doesn't get that the DB is connected.
If you need more information ask me about it and thanks.
First of all this is completely necessary in the autoload.php:
$autoload['libraries'] = array('database');
And as annotation, Codeigniter doesn't support to show the queries runned when calling
$this->output->enable_profiler(true); // always check the difference between hyphen and underscore when calling functions
if you load the database connection on the CI_Model or any other model, it is completelly necessary loading the database connection over the controllers or main controller if want to see the queries runned.
You must be load database Library in config/autoload.php
$autoload['libraries'] = array('database');
Hope this solution will usefull to you.

How i can import data from parse.com service in my own a mongodb database?

As you know, parse.com will be closed.
I made export of all collections.
Now i have the many dumps of collections.
Collections name like:
_User
AppParentCompany
It's normal tables name, but between this files i have relations, a relation contained in the next file:
_Join:parentCompanies:_User.json
Latest file name i understand like:
_Join - is relations
parentCompany - it's column
_User - a collection containing parentCompany column
Based on this, i have next questions:
How i can import this relations into a database?
What name will be for this collection of relation?
Thanks!
Take a look at this page: https://github.com/ParsePlatform/parse-server/wiki/Migrating-an-Existing-Parse-App
Prepare mongodb database connection string first.
Go to Parse your project's App Settings > General.
Find App Management > Migrate to external database. Press Migrate button.
Enter your database connection string, it will start with mongodb://...
Then press Begin the migration.
Then Parse will do the rest for you.
P.S. If you are using mongolab, go the web browser, into your database, then you can find _Join:xxx:xxx,​​​​​​ _SCHEMA and more in Collections.

using doctrine with codeigniter

I am planning to use doctrine to write a module of my app which is built with codeigniter.
I have a very basic question :
lets say I have a table called "user", with doctrine generate-models from db, 3 classes are generated BaseUser.php, User.php and UserTable.php. Now as I saw in the examples they use User class straigtaway. Should I be doing this ? I need additional business functionality for the user objects. So should I create a codeigniter model user_model and then use User class inside it (aggregation) or somehow extend user class ( i dont know how this will be done as user_model extends model)
Am little confused on this one and cannot locate any appropriate literature for the same.
Any help would be appreciated.
thanks in advance,
For anyone who is interested - I’ve posted up a project starter on my blog - a dev ready incorporation of the following technologies:
ExtJS : client side JS library,
CodeIgniter : presentation + domain tier,
Doctrine : ORM data layer framework
Some features of this project starter are:
- CodeIgniter Models have been replaced with Doctrine Records
- Doctrine is loaded into CI as a plugin
- RoR type before and after filters….
- Doctrine transactions automatically wrapped around every action at execution time (ATOMIC db updates)
Basic Role based security (I think Redux may be in there as well?)
Simply extract, hook up the database.php config file and viola…. You can start coding your layouts, views and models. Probably a few things to iron out - but enjoy!
Hope it helps
GET IT AT: http://thecodeabode.blogspot.com
Check out this info on Doctrine_Table class.
To your 3 generated files:
BaseXXX.php:
Holds the definition of your models so that Doctrine is able to handle the operations on the database. This class tells the ORM what colums are available, their types, advaned functions (like Timestampable,...) and more. You should not put your own data into this file since it will be over-written when re-creating the models from the database.
XXX.php:
Your actual model. This won't be re-created with each new generation process and this is were you keep most of your code. You can overwrite functions of the BaseXXX.php if you have to.
XXXTable.php:
Check my link from the top, This gives you access to the table itself. Personally, I do not use it that often since I put most of the code into XXX.php.
Of course you can create new classes and use them inside your XXX.php file. In order to actually do something with the data (save, read,...) you need classes that are connected (exteneded) from Doctrine's classes.
edit: also check this on a more infos with extending from the Doctrine_Table class

Resources