Laravel package database - laravel-4

Is it possible to specify another database connection in a new Laravel workbench package in the config/database.php file? I am printing out the connections, but it doesn't seem to be there.
Thanks.

You have to make Laravel's ConnectionResolver aware of the package's connection settings.
In your Laravel package's ServiceProvider's boot() method, you can set this using the following:
\Config::set('database.connections.yourdb',
\Config::get('package::database.connections.yourdb'));
where yourdb is the name of your package's connection in your src/config/database.php file, and package is the lowercase name of your package (example: in a workbench package named Somevendor\Somepackage, the package would be "somepackage").
Now in your package's model, simply use:
protected $connection = 'yourdb';
Source: excellent blog post by Ryan Tablada here: http://ryantablada.com/post/laravel-package-database-flexibility-eloquent-models

Related

Laravel create Model and Migration dynamically with dynamic table columns

Working on a task where we need to create Model and Migration files programmatically. Also in the Model there will be some predefined functions. These functions will be same for all models. The functions are just relations with other Models. I have searched for a few options and found that Laravel has Stubs and we can create custom stubs which are great but don't think there is any option to pass params to the stub file when generating it.
In this case, we want to create a model and migration with dynamic columns. May be there is no easy way to do it but in case anyone has done it already, can you please provide me a hint of how you did it.
Trying this laravel package
https://github.com/laravel-shift/blueprint
.It can generate models, migrations, controllers from Yaml file. May be we can create a yaml file dynamically and then publish it.
Thanks
you can write your own command.
for example i wrote a command for generate repository pattern in my projects
php artisan make:repository repoName
You can publish stubs
php artisan stub:publish
Create your custom stub
<?php
// stubs/controller.custom.stub
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
/**
* Hello from the custom controller stub
*/
class {{ class }}
{
//
}
And call it
php artisan make:controller --type=custom MyController
Watch --type option

How to override any Model of vendor folder in laravel

I am using MongoDB as database so I need to change model of package tzsk\payu as using original is giving me following error
Call to a member function prepare() on null
I tried excluding original model and overriding using composer it doesn't work.
The only way would be if the package offered the option to use a custom model, like Passport is doing.
As far as I can see, there does not seem to be a way to do that. Thus you'd need to fork the package and edit the Model yourself.

How to run package in laravel 5?

How to run package in laravel 5?
I have downloaded one chat package from INTERNET. And, I have run command vendor:publish and it successfully published.
Now I don't know how to run that.
First you need to check whether the package ships with any service provider and facade. If so, add them in your config/app.php file. Package's service provider should go under providers array and facade should go under aliases array.
As soon as you specify providers under providers array, Laravel now know that you have called an external package, so it will load them. Thus, the package now will be available for your usage.
The easy way to run a package in laravel is:
add the package to composer.json file
then in terminal write composer dump-autoload

Laravel selfregistering ServiceProvider

In Laravel 4.2 is it possible to create a package which automatically registers the ServiceProvider without the user adding the path manually to the app.php file?
This way one could just run composer update after adding a package to the composer.json file and would be ready to go.
I think that there is no way to register your main service provider than putting it in the app.php file. In fact you can, but it will always require something from the developer. You can't get away with it with a simple composer update.
You can register sub service providers with App::register('MyApp\Providers\MyServiceProvider'); inside your package.
No, there is no way for a package to register itself.

Get a var from a config file?

I'm using a package (aws) and I wish to get a var from it's config file.
The config file is located in:
config>packages>aws>aws-sdk-php-laravel>config
I've tried:
Config::get('packages.aws.aws-sdk-php-laravel.config.key')
But no luck, any ideas?
Config is easily accessed and Laravel has support for packages. The structure for calling it would be.
Config::get('package::file.option');
I've just taken a look at the package you are using, and they've set up a nice handy namespace for access. Since the package only has one config file, you can also omit that, like so:
Config::get('aws::key');
For more information regarding third party package configuration, here's the Laravel documentation on the subject http://laravel.com/docs/packages#package-configuration.
Hope that helps.
Use the package notation for config files:
Config::get('aws/aws-sdk-php-laravel::key');
And you may also have to add the namespace to Laravel:
Config::addNamespace('aws/aws-sdk-php-laravel', __DIR__.'/path/to/config');

Resources