I am new to laravel. I've got the whole database designed in phpmyadmin. Now I want to integrate the existing database in Laravel. Is there any way to do that? If so then do I have to create models?
Yes, you can use your database in laravel but at first you have to provide your database credentials to allow the framework/Laravel to access your database. So, you can use a .env file or simply you can use the config/database.php to provide credentials, for example, to use your mySql database all you need to setup the database configuration as follows:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_database_name'),
'username' => env('DB_USERNAME', 'your_database_username'),
'password' => env('DB_PASSWORD', 'your_database_password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
Then regarding your second question, yes, you have to create models for each table or you can use DB facade to run queries directly from your controllers (not recommended). For example, to access your posts table you can create a Post model in app folder which may look something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
// If your table name is other than posts
// then use this property to tell Laravel
// the table name.
protected $table = 'posts';
}
Then you can use something like this:
$posts = \App\Post::all(); // Get all posts
$post = \App\Post::find(1); // Get the post with id 1
If you don't want to use Eloquent Model like one I've created above then you may use something like this:
$posts = \DB::table('posts')->get(); // Get allp posts
These are some basic examples to get you start. To learn more about the use cases of models/database you should visit Laravel website and read the manual (find Database section and check Query Builder and Eloquent).
Related
I want cake to retrive for exemple a pdf invoice from a user and send email every 10 days
So as far as I understand for the sender
I made changement Inside
app.php
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'ssl://smtp-mail.outlook.com',
'port' => 587,
'timeout' => 30,
'username' => exemple#live.fr,
'password' => exemple,
'client' => null,
'tls' => yes,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
Then I have to create the the Task Either Inside src\console or src\shell (not sure about thoses two)
Now do I have to create a Table email sql ? what changes to be applied for The UsersController
Exemple would Be appreciated
Note : documentation didn't help at all
I tried this way but its outdated
CakePHP send email
you should create inside src\shell and
format send mail
$email = new Email();
$email
->setEmailFormat('html')
->setTo($email)
->setFrom($your_email)
->setSubject($subject)
->send($content);
I used PHPMailer library
https://github.com/PHPMailer/PHPMailer
And then followed this tutoriel now
https://www.youtube.com/watch?v=RzqfpT8gv0w&list=PLmrTMUhqzS3iwbxyd61TM4qxej4zfvKeo
now I'm able to send email with Cron Shell
I've been using Laravel 4 for a while with success until I found a recent problem. I'm using an alternate DB connection to retrieve a list of products. My problem is that I don't find a way to create a connection like DB::connection('foo') and implement my query in a query builder style. I assume its some IoC behavior but my lack of understanding of the inner framework's code keeps me away from the answer
Thank you all
Add a second connection in app/config/database.php
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
Now use that second connection:
DB::connection('mysql2')->select('where...');
The proper way to accomplish this is DB::connection('mysql2')->table('foo')->join(...)->where(array(...))
My problem was outside the scope of this question.
Thank you all.
Currently I have to share my database with many CakePHP applications and the tables are prefixed to identify each application respectively.
So, can many applications share the same ACL tables?
Or, I could change the default names of the tables ACL and add the prefix of each application, eg. app_aros, app_acos, app_aros_acos?
/* beforeFilter() # AppController */
$this->Acl->Aro->useTable = 'app_aros';
$this->Acl->Aco->useTable = 'app_acos';
This code worked but I haven't found a way to change the tablename of the model Permission...
Suggestions? What could I do?
You have change this line in app/Config/core.php in all of your apps
Configure::write('Acl.database', 'default');
to:
Configure::write('Acl.database', 'your_acl_connection');
And also add connection in app/Config/database.php
example:
public $your_acl_connection = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Simply you will have one DB for managing all yours app ACLs.
In this DB you create all ACL tables.
my application's requirement is that to display data of magento database table forexample admin_user that is reside at locally connected pc.
so I need to keep as it is my cakephp database values in displaying-modifying-etc, and in only one page magento's database value needs to be printed and updated.
I have kept 2 variables in databse.php
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'myappdatabase',
'prefix' => '',
//'encoding' => 'utf8',
);
var $vsdatabase = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '192.168.1.36',
'login' => 'root',
'password' => '',
'database' => 'magento',
'prefix' => '',
);
and in controller,
App::import('Model','ConnectionManager');
$db = ConnectionManager::getDataSource('vsdatabase');
$database = $db->config['database'];
$data = $this->User->query("select * from $database.admin_user as t1");
the host I like to keep as written above means default is from my local database and other is remote PC's magento database
If I both host keep same then its working but if write different then It's not working
So I solve this problem?
Plz help me in finding out solution
What you do is plain wrong and even if you would use that code it should belong into a model not a controller.
Simply create a new model named after the magento table, but I would prefix it with magento or something. The model has to be configured to use this db connection.
class MagentoUser extends AppModel {
public $useDbConfig = 'vsdatabase';
}
You can also init models with other aliases and data sources on the fly using ClassRegistry. See http://api20.cakephp.org/class/class-registry#method-ClassRegistryinit
And stay away from using plain SQL queries, you will very likely ending up writing insecure queries and lose other features of the CakePHP ORM.
I was wondering if is possible to have CodeIgniter 2 with Doctrine 2 with multiple database connections.
I currently got CodeIgniter 2 and Doctrine 2 to work with 1 database, but is it possible to do multiple database?
If so, how can this be accomplished?
I am unsure if you can do this where the two databases interact directly. (Cross-Database Joins/etc..)
However in your load-up for doctrine you probably have something along the lines of this:
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $doctrine_config);
When You would just configure a second entity manager with the other db, perhaps with a seperate doctrine-config:
// Database connection information
$connectionOptions2 = array(
'driver' => 'pdo_mysql',
'user' => $db['other']['username'],
'password' => $db['other']['password'],
'host' => $db['other']['hostname'],
'dbname' => $db['other']['database']
);
// Create EntityManager
$this->emOther = EntityManager::create($connectionOptions2, $doctrine_config2);