Using validator for artisan command in Lumen - validation

I would like to use validator for artisan commands (i.e unique user in database table). How to get validator instance inside artisan command in Lumen?

I found the solution. Simply add use Illuminate\Support\Facades\Validator; to class and use Validator like in Laravel.

Related

Change default directory for creating migration in Laravel

I want to change the default directory for creating migration i.e. If I do php artisan make:migration create_users_table, it should create users table in /database/migrations/child_database. I do not want to use --path every time I create a migration for child_database. I want to change the default directory for php artisan make:migration.
you can create your own artisan command to achieve this:
create your a new artisan command and use it to make migration in your custom directory using this article. hope help you ^_^
try this package github repo or use laracast link
You can use loadMigrationsFrom() in your AppServiceProvider.
Inside the boot() function, run:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder1',
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder2',
]);/
This question has been answered before, even though it's a little bit old it still has it's value.
Have a look at: https://stackoverflow.com/a/35895106
If you use this answer, the default directory is changed and you will have the solution you want.

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 create modular in laravel?

I want to create a modular to create a controller and a model.
Of course, the model is pre-created.
php artisan module:make-controller Admin\ReportController Report --model=Report
I see this message
The "--model" option does not exist.
I assume you used nWidart Modules, so the answer is you can not use --model option because nWidart Modules doesn't support that option/parameter. You need run another command to create a model, an example:
php artisan module:make-model Report Admin
Report is model name, and Admin is module name
check it out for more module commands

I can't seem to access my Auth inside a vendor package

I had a Laravel app that I created, then I added another Laravel package for Oauth2 for a CRM.
This created a vendor and migration for DB. I tested it and it worked, I authenticated my CRM.
Now I would like to have that record be tied to the user_id in the Auth scaffold that I created with composer make:auth
However, when I try to get the current user from inside of those vendor files, it is not pulling in that information and is giving me errors.
I think this may be a namespace issue, but maybe it could be a guard or middleware. I am not sure. I am pretty laravel
I have tried a handful of other solutions, but I wasn't sure if those were tied to my specific issue
<?php
namespace Djaxho\LaravelInfusionsoftOauth2\Http\Controllers;
//namespace App\Http\Controllers\Auth;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
use Illuminate\Support\ServiceProvider;
//use Illuminate\Support\Facades\Auth;
class AuthorizeInfusionsoftApiController extends BaseController
'''
print Auth::user()->id;
I see that it is in its own namespace, is this what is the issue, laravel is not looking outside?
I get this error:
Class 'Djaxho\LaravelInfusionsoftOauth2\Http\Controllers\Auth' not
found
Their page says it pretty straightforward on what to do.
https://packagist.org/packages/djaxho/laravel-infusionsoft-oauth2
Installation (These instructions are for an 'alerady set-up' laravel
project with a functioning database set up) Add the service provider
for laravel by adding the following line to your 'providers' array in
the file congig/app.php
Djaxho\LaravelInfusionsoftOauth2\LaravelInfusionsoftOauth2ServiceProvider::class,
And add
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
to your model where you are calling the auth from.
That being said, I dont know if this package handles auth seperately or doesnt. In that case you should use the laravel facade,
use Illuminate\Support\Facades\Auth
You need to give you routes in your package web middleware and it will works fine
.

Laravel 5.3 DatabaseMigrations are destructive

Laravel 5.3 with mysql, PHPUnit 5.7.4
When I create a test in PHPUnit with use DatabaseMigrations;, it destroys the data that it queries.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ThingsTest extends TestCase
{
use DatabaseMigrations;
/** #test */
public function there_are_more_than_1000_things()
{
$things = App\Thing::all();
$this->assertGreaterThan(1000, count($things));
}
Before I run phpunit, there are lots of things. After running it, mysql says ERROR 1146 (42S02): Table 'database.things' doesn't exist
Any ideas how to stop this?
DatabaseMigrations is a trait and it execs:
before test 'php artisan migrate' // creates your tables, but doesn't seed them
after test 'php artisan migrate:rollback' // remove tables
So, 1st - make sure you're using another database for testing.
2nd - seed with fake data your tables before testing your Things class.
Alternative:
use DatabaseTransactions trait instead of DatabaseMigrations.
In that case each test activity will be wrapped in a database transaction. After test all your changes will be dropped by transaction's rollback automatically.
You can be using a test database with PHPUnit within your Laravel application. Right now your tests are using your main database and will modify existing information.
Please see https://stackoverflow.com/a/35228697/823549 for how to do this.

Resources