我已经进入了 Laravel 的奇妙世界,目前正在研究在数据库中植入虚假数据进行测试。
我有几个想要使用的表;项目和故事。
故事表有列;id、name和project_id(这是项目表的外键)。
我的项目表已填充 10 个项目的列表。现在我需要用相关的随机项目填充 100 个故事。我有下面的方法。
public function run()
{
DB::table('stories')->delete();
DB::statement('ALTER TABLE stories AUTO_INCREMENT = 1');
$faker = Faker::create();
foreach(range(1, 100) as $index)
{
Story::create([
'reference' => $faker->numberBetween(1, 9999),
'name' => $faker->sentence(6),
'project_id' => Project::orderBy(\DB::raw('RAND()'))->get()->first()->pluck('id')
]);
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是完成我需要的事情的最佳方式。然而,执行此代码时,每个故事的project_id 都设置为1;第一个项目的 ID。
当我在tinker中执行以下命令时...它总是返回1作为id。
Project::orderBy(\DB::raw('RAND()'))->get()->first()->pluck('id')
Run Code Online (Sandbox Code Playgroud)
但是当我在修补程序中执行下一个命令时......
Project::orderBy(\DB::raw('RAND()'))->get()->first()
Run Code Online (Sandbox Code Playgroud)
它每次都会返回一个随机项目。这很奇怪。因为如果 ->pluck() 之前的所有内容都正常工作,那么 pluck() 应该获取收集到的项目 id...对吧?这就是上面命令返回的内容。
<App\Project #000000000c385908000000000de30942> {
id: 6,
name: "New Bernadetteton",
cover_photo_url: "/uploads/covers/horizon-grass.png",
created_at: "2015-07-08 16:32:15",
updated_at: …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows 服务器上使用 Laravel 任务调度程序,但经过各种研究,我不知道如何做到这一点。
显然我可以在 Linux 上做到这一点,但据我所知,从 Windows 任务调度程序触发 artisan 是不够的。
你能帮我解决这个问题吗?
我删除了一些迁移文件。但是当我执行 php artisan migrate 命令时,旧迁移文件中显示错误。
[Illuminate\Database\QueryException]
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL :在删除级联上更改表cushbu_notifications添加约束cushbu_notifications_art_id_foreign外键 (art_id) 引用cushbu_arts( ))id
现在我没有 cushbu_notifications 的迁移文件,而是为 cushbu_user_notifications 创建了一个新的迁移文件。我从数据库中删除了 cushbu_notifications 表,也从迁移表中删除了相应的条目。
我已经在多个项目中使用了 laravel,但是由于某种原因,我的新 laravel 项目没有 server.php 文件。
重新安装 composer 和/或重新创建项目不起作用。我也不能以任何方式将名为 server.php 的文件放入项目中。
[Fri May 11 02:26:55 2018] PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
[Fri May 11 02:26:55 2018] PHP 致命错误:未知:打开失败第 0 行未知中需要 'C:\Users\Luke Derkzen\Documents\GitHub\Website\server.php' (include_path='C:\xampp\php\PEAR')
当我尝试启动服务器时出现此错误,据我所知,它希望我在 XAMPP 上运行一些东西,但是在我的其他项目中,有一个 server.php 文件,它没有给出上述错误。
该项目是使用以下命令创建的:
composer create-project --prefer-dist laravel/laravel
如果我之后使用 php artisan serve 命令启动服务器并重新加载页面。上述错误出现在工匠控制台中。
我怎样才能解决这个问题?
奇怪的事情正在发生。一直在尝试运行迁移,php artisan migrate但出现以下关于缺少表(应该由迁移创建和填充)的错误。
PHP Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.portals' doesn't exist in /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:80
Stack trace:
#0 /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(80): PDO->prepare('select * from `...', Array)
#1 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Doctrine\DBAL\Driver\PDOConnection->prepare('select * from `...')
#2 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(657): Illuminate\Database\Connection->Illuminate\Database\{closure}('select * from `...', Array)
#3 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#4 /home/daniel/Programming/app/vendor/laravel in /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
PHP Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.portals' doesn't exist in /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:80 …Run Code Online (Sandbox Code Playgroud) 我正在尝试迁移我新创建的迁移。问题是等待迁移的一批迁移是按照我使用创建迁移的顺序
php artisan make:migration
Run Code Online (Sandbox Code Playgroud)
因此,我的product_list迁移试图在尚未迁移的表上设置外键。
$table->foreign('product_category')->references('id')->on('product_categories');
$table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)
当我运行时,php artisan migrate我收到这些错误:
C:\xampp\htdocs\iezonsolutions>php artisan migrate
Migrating: 2019_01_15_001617_product_list
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `iezonsolutions`.`#sql-1844_16e` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `product_list` add constraint `product_list_product_category_foreign` foreign key (`product_category`) references `product_categories` (`id`))
at C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make …Run Code Online (Sandbox Code Playgroud) 我可以使用内存来迁移我的数据库并启动服务器吗?
我的/配置/数据库.php
'sqlite' => array(
'driver' => 'sqlite',
'database' => ':memory:', // fail
// 'database' => 'database', // success
'prefix' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
),
Run Code Online (Sandbox Code Playgroud)
并迁移表并启动laravel服务
$ php artisan migrate:refresh
$ php artisan serve
Run Code Online (Sandbox Code Playgroud)
我使用 driver=sqlite + database=database 一切正常,
我使用driver=sqlite + database=:memory,错误信息来自127:0.0.1 说数据库表尚未创建。
我是 Laravel 5 的新手,浏览过https://laravel.com/docs/5.8/controllers但找不到任何方法可以使用带有特定方法名称的 php artisan 创建控制器。我尝试了以下选项,但没有一个起作用。
php artisan make:controller HomeControler#index logout
Run Code Online (Sandbox Code Playgroud)
和
php artisan make:controller HomeControler/index logout
Run Code Online (Sandbox Code Playgroud)
和
php artisan make:controller HomeController index logout
Run Code Online (Sandbox Code Playgroud)
Laravel 5 有可能实现这一目标吗?如果是,在使用 artisan make 命令创建控制器时是否也可以声明多个函数?
我正在尝试创建一个外键,artisan 没有显示任何错误,只是没有创建我的外键,这可能是我的代码:
1°表:
Schema::create('cooperados', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('nameCoop', 255);
$table->integer('numCoop');
$table->string('cpfCoop', 11);
$table->date('dtCad');
$table->date('dtDem')->nullable();
$table->text('description')->nullable();
$table->decimal('subscritas', 6, 2);
$table->decimal('integralizadas', 6,2)->nullable();
$table->decimal('aintegralizar', 6,2)->nullable();
$table->enum('status', ['ativo','inativo'])->default('ativo');
});
Run Code Online (Sandbox Code Playgroud)
外键表
public function up()
{
Schema::create('mov', function (Blueprint $table) {
$table->bigIncrements('idMov');
$table->timestamps();
$table->integer('id_coop')->unsigned;
$table->foreign('id_coop')->references('id')->on('cooperados');
$table->decimal('valor', 6, 2);
});
}
Run Code Online (Sandbox Code Playgroud) 我为我正在处理的 Trait 设置了一些配置变量,其中之一是一个data_formatjson 或 xml 变量。
它是这样设置的:
$this->data_format = config('rightmove.FORMAT');
Run Code Online (Sandbox Code Playgroud)
在控制器中,我可以运行 dd($this->data_format) 并返回:“ json ”
我已将此作为控制台命令,但是当我尝试使用$this->data_format它的 dd 运行控制台命令时,它会返回
空值
随后我的其余代码错误。
我已经运行了一个php artisan config:cache命令来清除缓存,但控制台似乎没有选择该配置变量?
我的App\Console\Commands\UpdateRightMove样子如下:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Traits\RightMoveTrait;
use App\Property;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use QCod\AppSettings\Setting\AppSettings;
class UpdateRightmove extends Command
{
use RightMoveTrait;
private $is_set;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rightmove:update';
/** …Run Code Online (Sandbox Code Playgroud) laravel-artisan ×10
laravel ×9
php ×6
laravel-5 ×3
composer-php ×1
console ×1
controller ×1
cron-task ×1
faker ×1
laravel-5.4 ×1
migration ×1
mysql ×1
sql ×1
sqlite ×1
task ×1