I have installed laravel 5 and looking for scaffolding but when i run this command
php artisan make:scaffold Tweet --schema="title:string:default('Tweet #1'), body:text"
it is giving exception "The "--no-migration" option does not exist".
I have checked it with php artisan migrate --help command and the option does not exists.
Can anybody help me please?
Thanks.
The laralib/l5scaffold extension does not have a --no-migration option. So you cannot prevent creating the migration files via command.
Currently I don't see any suitable way to achieve the desired behaviour. Just delete the migration files afterwards.
Or implement the feature yourself and create a pull request to the repository. You will probably only need to change src/Commands/ScaffoldMakeCommand.php. Here are some hints:
public function fire()
{
// ...
// Generate files
if (!$this->option('no-migration')) {
$this->makeMigration();
}
$this->makeSeed();
// ...
}
protected function getOptions()
{
return [
['schema', 's', InputOption::VALUE_REQUIRED, 'Schema ...', null],
['form', 'f', InputOption::VALUE_OPTIONAL, 'Use ...'],
['no-migration', 'm', InputOption::VALUE_OPTIONAL, 'Don\'t create migration files.']
];
}
Related
After connecting to database, I want to programmatically run migration and seeder once the project detects that the database doesn't have any tables.
I think what I should do is inject the code below somewhere, but I don't know what file I should edit.
if (!Schema::hasTable('users')) {
$init_met = ini_get('max_execution_time');
set_time_limit(300);
Artisan::call('migrate:fresh');
Artisan::call('db:seed');
set_time_limit($init_met);
}
Or, is there an alternative way to do this instead of injecting the code?
Thanks in advance.
i'd suggest you to look at composer scripts section - there are a lot of events that could be used as trigger for your code. for example post-autoload-dump event fired after composer dumpautoload which is fired in most common calls like install, update or by itself. the benefit of using composer events is that you don't need to check for existing tables on each request.
the most easy way to achieve this is to create custom artisan command
php artisan make:command PrepareEmptyDatabase
then in app\Console\Commands\PrepareEmptyDatabase.php
<?php
namespace App\Console\Commands;
use Exception;
use App\Http\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
class PrepareEmptyDatabase extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'db:prepare-empty';
/**
* The console command description.
*
* #var string
*/
protected $description = 'check for users table and run migrations and seed if has not';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
// don't forget to remove this if database user
// don't have access to dba tables
if (Schema::hasTable('users')) {
return Command::SUCCESS;
}
/*
// if your user doesn't have permission to access to dba tables
// you can simply try to do any request to users table
$needActions = false;
try {
User::first();
} catch (Exception $ex) {
$needActions = true;
}
if (!$needActions) {
return Command::SUCCESS;
}
*/
$init_met = ini_get('max_execution_time');
set_time_limit(300);
Artisan::call('migrate:fresh');
Artisan::call('db:seed');
set_time_limit($init_met);
return Command::SUCCESS;
}
}
and the last step is tie this command with composer event, so in composer.json
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover",
"#php artisan db:prepare-empty"
]
},
now any time you install/update composer dependencies or just run composer dumpatoload application will run your custom command. or you can stick with any of provided in composer docs event on your taste
about running the same code after npm run dev
i'm not quite sure about place to search, guess its about webpack events, but your question tagged with laravel so i assume you're using laravel-mix and there are event hooks
quick googling says that nodejs can run bash scripts using nodejs child process
// webpack.mix.js
const exec = require('child_process')
// or import exec from 'child_process'
mix
.js('resources/assets/js/app.js', 'public/js')
// other configs
.after(() => {
exec.execSync('php artisan db:prepare-empty')
})
pay attention that this code will run on any mix even including npm run prod for example and i don't actually understand why do you need it in frontend build event (if only for testing purpose and even then its questionable)
I'm using docker for running a Laravel project and i have this command in Laravel that writes into a file in storage folder
Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
)
when i call it through web like
Route::get('/', function () {
\Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
);
});
an exception from /src/vendor/symfony/console/Input/ArrayInput.php file is generated with this message: "Trying to access array offset on value of type int"
but in command line this command is working completely OK.
the issue was coming from my using version of PHP, the PHP version was 7.4.1 and the package that Laravel was using for this version of PHP was changed and that caused the error to happen, I changed my using PHP version to 7.2 and got worked.
I have been given the code but normally, programmers should not use this kind of packages this way, for example in this one, WEB should not call Artisan commands directly, because if a situation like this happens you will need to override your code like everywhere. Instead, use your own code and put what you want behind it, as an example:
imaging you have this artisan command that writes some information in a file and you want to use it in your WEB section then you must put your code into a class or function outside of your artisan command and use your class/function in your artisan and web for work. then code like bellow
Route::get('/', function () {
\Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
);
});
will become to code like
Route::get('/', function () {
$cache = new \App\Custom\Cache(
$message = 'this is a command',
$seconds = 0
);
});
In this way, your functioning code is separated from your framework and if you even use some packages in your class/function and a package needs to change like it was to call Artisan command then there is just one place to update, not multiple places.
Try calling it like this:
Artisan::call('app:cache --message="this is a command" --seconds=0');
and if you want to put dynamic variables in it:
Artisan::call('app:cache --message=' . $yourMessage . '--seconds=' . $seconds);
You will not have to handle any arrays by passing your variables in a single string line like this. It still makes it simple to read as a single string.
I'm trying to make a thumbnail in a Laravel project and I tried a lot other ways but with no success - Libraries, APIs... The problem is that the project is developed in Windows and the professor needs it in Windows as well.
So far, I experimented to integrate different libraries (wkhtmltoimage, spatie/browsershot, mpdf and s.o.) but in the most of the cases, there are problems with the path.
The required function, which I need, works very good in command prompt and I thought that I must find a way to call it in the controller.
I've tried with:
shell_execute($call);
system($call);
exec($call);
// with $call = "{func_name} {path_to_input_file} {name_of_output_file}";
// Example: $call = "wkhtmltoimage C:\xampp\htdocs\app\public\test.html img.jpg"
But no result. The function generates an image, which I want to store in the database.
Is there an other way to make a call to the command prompt?
Maybe SSH call?
You can execute Artisan commands directly via your controller.
Look at this example from the Laravel documentation:
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
I have upgraded my app from Laravel 4.2 to 5.5 and I am getting issues with the queue.
public function saved(Model $review)
{
if (App::runningInConsole()) {
return;
}
$data = [
'review' => serialize($review),
'action' => self::ACTION_SAVE
];
Queue::push(new UpdateReviewSummaryQueue, $data);
}
When I run this on model save, I am getting an error that the UpdateReviewSummaryQueue class does not exist. I've ran composer dump-autoload and namespacing seems to be fine. Are there any other issues I might look into?
I've also added
use SerializesModels;
as Laravel upgrade guide suggested
In laravel 5.5 you now dispatch jobs rather than push. See https://laravel.com/docs/5.5/queues#dispatching-jobs for full documentation.
Try :
dispatch((new UpdateReviewSummaryQueue($data));
The issue was actually in this line:
Queue::push(new UpdateReviewSummaryQueue, $data);
Changing it to this made it work:
Queue::push(UpdateReviewSummaryQueue::class, $data);
My ModelFactory:
<?php
$factory->define(App\Models\Customer::class, function (Faker\Generator $faker) {
return [
'name' => $faker->company,
'email' => $faker->unique()->safeEmail,
'status'=> $faker->numberBetween($min = 0, $max = 2),
'slug'=> $faker->slug,
];
});
Database seeder
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$customers= factory(App\Models\Customer::class, 100)->create();
}
}
When I run
php artisan db:seed
I get the error
[Symfony\Component\Debug\Exception\FatalThrowableError]
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting '
]'
I've tried everything I can think of, but can't find the problem...
Edit:
I forgot to mention that this was working fine a day earlier and then "broke" as I started to add more ModelFactories (in separate files). I then discarded all my changes (from source control) to be 100% sure I hadn't changed anything. The only other aspect could be that I have something in the .gitignore that may have updated and wasn't rolled back:
/node_modules
/public/storage
/public/hot
/storage/*.key
/.idea
Homestead.json
Homestead.yaml
It seems the problem is this line:
'status'=> $faker->numberBetween($min = 0, $max = 2),
It should be:
'status'=> $faker->numberBetween(0, 2),
Ok, so I found the reason and feel really idiotic about it, but posting it here anyway in case someone follows in my footsteps.
The issue was that there were other ModelFactories in the database/factories folder and it seems that running php artisan db:seed parses those files as well, even though they are not referenced in the DatabaseSeeder class. One of those files had incorrect one-many syntax and that was causing the error.
The only way I realized it was that I ran the factory->create method within php artisan tinker and the error message it threw up referenced this other factory definition.
FWIW, I then used the approach outlined here for my relationships - for the reasons mentioned in the question there...