我在 eloquent 方面遇到了一个奇怪的问题。
我有两个雄辩的模型,一个 User 模型和一个 Profile 模型,它们都有单独的表并具有一对一的关系。
我有一个表单,其中传递了这两个模型的数据。因此属于用户和配置文件的数据存在于表单中。
我试图从控制器尽可能干净地执行插入:
$user = $this->users->createUser($request->all());
Run Code Online (Sandbox Code Playgroud)
该createUser函数被定义像这样的
public function createUser(array $data)
{
$user = new User($data);
// set some non relevant user options here...
$user->save();
$profile = new Profile($data);
$user->profile()->save($profile);
return $user;
}
Run Code Online (Sandbox Code Playgroud)
但这会产生 sql 错误,因为 eloquent 将表单中的所有值分配给两个模型,因此 User 和 Profile 都获得了所有相同的属性,因此 eloquent 尝试插入到给定模型不存在的列中。
我认为该$fillable字段用于防止此类事情发生,但我确实$fillable在两个模型中都定义了字段:
// User
protected $fillable = ['email', 'password', ...];
// Profile
protected $fillable = ['first_name', 'last_name', ...];
Run Code Online (Sandbox Code Playgroud)
我也试过这个:(new User)->fill($data)但这不起作用,也不应该,因为该fill方法也是从 …
遗嘱,我在应用程序(又名项目)目录中。工匠文件在我所在的目录中。
接下来...如果我运行以下命令,我会得到输出(可用命令的列表):
php工匠列表
但是如果我运行这个命令(故意省略一个必需的参数),虽然我希望有一个错误,但我不会得到任何错误:
php工匠制作:命令
artisan 鉴于这个故意不完整的命令导致:没有输出,它的配置似乎不满意。
我尝试过的事情
find -L ./ -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l | grep "Errors parsing"没有发现有语法错误的文件composer updatecomposer installphp artisan optimize 请提供会引起php artisan bad:command投诉的解决方案。
我知道我可以使用命令创建一个带有控制器的模型,php artisan make:model Task -c我也可以使用php artisan make:controller TasksController -r. 有没有办法用资源丰富的控制器创建模型?
目前使用 Laravel 5.5 和与 Laravel 安装程序一起提供的 Guzzle。
我正在尝试发出 GET 请求(其他 HTTP 请求也会发生错误),但似乎不起作用。
此代码不起作用:
public function callback(Request $request)
{
$code = $request->code;
$client = new Client(['exceptions' => false]);
try {
$response = $client->request('GET', 'http://localhost/api/tests');
// $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
// 'form_params' => [
// 'grant_type' => 'authorization_code',
// 'client_id' => Config::get('oauth_client.client_id'),
// 'client_secret' => Config::get('oauth_client.client_secret'),
// 'redirect_uri' => Config::get('oauth_client.redirect_uri'),
// 'code' => $code,
// ],
// ]);
// return json_decode((string) $response->getBody(), true);
} catch (\Exception $e) {
dd($e); …Run Code Online (Sandbox Code Playgroud) 执行时出现以下错误:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
错误:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] 服务器请求客户端未知的身份验证方法(SQL:select * from information_schema.tables where table _schema = d1 and table_name = migrations)
这是一个截图;
附注。以防万一:我在php.ini文件中启用了“extension=pdo_mysql” 。
任何想法如何解决这一问题?
我经营工匠:
php artisan down --message "Going down for maintenance" --retry=60
Run Code Online (Sandbox Code Playgroud)
[更新] 或者像@Remul 建议的那样运行它:
php artisan down --message="Going down for maintenance" --retry=60
Run Code Online (Sandbox Code Playgroud)
然后两者都给了我错误:
[Symfony\Component\Console\Exception\RuntimeException]
Too many arguments, expected arguments "command".
Run Code Online (Sandbox Code Playgroud)
如果像这样运行没有空格的命令:
php artisan down --message "Going_down_for_maintenance" --retry=60
Run Code Online (Sandbox Code Playgroud)
没有错误发生
我试图在 vps 上通过 laravel 中的维护模式允许我的公共互联网 IP。
该--allow命令是通过 Internet 工作还是仅在本地主机上工作?因为我无法使用以下命令通过维护模式页面:
$ php artisan down --allow=xx.xx.xx.xx (my public internet ip)
Application is now in maintenance mode.
$ php artisan up
Application is now live.
Run Code Online (Sandbox Code Playgroud) maintenance maintenance-mode laravel laravel-5.7 laravel-artisan
我有一个使用 docker 和 laravel 运行的应用程序,但有一些指令显然已执行但无法运行。
这些说明是php artisan clear:cache和php artisan optimize。有时我需要进入容器并运行这些指令,以便在浏览器中反映我的更改。除了 kubernetes 由于未反映的更改而检测到的故障
FROM php:7.2.0-fpm as php
RUN apt-get update && apt-get install -y openssl openssh-client \
rsync sshpass zip unzip git vim libmcrypt-dev mysql-client \
libssh2-1-dev libmagickwand-dev \n
gnupg2 --no-install-recommends zlib1g-dev sass \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install gd \
&& docker-php-ext-install zip \
&& pecl install …Run Code Online (Sandbox Code Playgroud) 我正在 Laravel 5.8 上开发一个电子商务项目,但由于我不小心在项目文件夹上运行了“laravel new”命令,因此当我尝试在本地服务器上启动项目时出现此错误:
致命错误:未捕获的反射异常:C:\wamp64\www\e-commerce\vendor\laravel\framework\src\Illuminate\Container\Container.php:788 中不存在类配置:0 C:\wamp64\www \e-commerce\vendor\laravel\framework\src\Illuminate\Container\Container.php(788): ReflectionClass->__construct('config') 1 C:\wamp64\www\e-commerce\vendor\laravel\framework \src\Illuminate\Container\Container.php(667): Illuminate\Container\Container->build('config') 2 C:\wamp64\www\e-commerce\vendor\laravel\framework\src\Illuminate\Container \Container.php(615): Illuminate\Container\Container->resolve('config', Array) 3 C:\wamp64\www\e-commerce\vendor\laravel\framework\src\Illuminate\Foundation\Application.php (767): Illuminate\Container\Container->make('config', Array) 4 C:\wamp64\www\e-commerce\vendor\laravel\framework\src\Illuminate\Container\Container.php(1227): Illuminate\Foundation\Application->make('config') 5 C:\wamp64\www\e -commerce\vendor\laravel\framework\src\Illuminate\Log\LogManager.php(417): Illuminate\Container\C in C:\wamp64\www\e-commerce\vendor\laravel\framework\src\Illuminate\Container \Container.php 在线 788"
第 788 行的错误是指那部分代码:
$reflector = new ReflectionClass($concrete)
Run Code Online (Sandbox Code Playgroud)
这是在我从 Github 克隆我的 repo 之后发生的。现在,对于我尝试在本地服务器上运行的每个项目,我都会收到此错误。
我试过“作曲家安装”,“作曲家更新”,“作曲家转储自动加载”,但他们在最后这一行都给出了同样的错误:“脚本@php artisan package:discover --ansi processing the post-autoload-dump事件返回错误代码 255"。
我检查了 .env 文件以查看是否也存在命名空间问题,但没有。
预先感谢您的帮助。
我想知道是否可以扩展或替换该php artisan tinker命令,因此它首先要求进行身份验证,以作为看门人可以使用它的方式。
我尝试了以下方法:
<?php
namespace App\Console\Commands;
use Laravel\Tinker\Console\TinkerCommand;
use Illuminate\Support\Facades\Auth;
class Tinker extends TinkerCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tinker';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return …Run Code Online (Sandbox Code Playgroud) laravel-artisan ×10
laravel ×8
php ×5
laravel-5.7 ×2
docker ×1
dockerfile ×1
eloquent ×1
guzzle ×1
http ×1
laravel-5 ×1
maintenance ×1
migrate ×1
orm ×1
phpunit ×1
rest ×1
serve ×1
symfony ×1
tinker ×1