所以此时的代码是我的代码(请注意 - 这段代码在PHP for循环内部被回显,所以当你看到$ i变量只是假设它是一个每次传递递增的数字):
<select name='field{$i}type' class='form-control' required>
<option disabled selected hidden>Type of Field</option>
<option value='text'>Text</option>
</select>
Run Code Online (Sandbox Code Playgroud)
当使用我的php动态创建这个选择时,我希望选择一个"占位符".我已经找到了关于如何制作占位符(Bootstrap选择下拉列表占位符)的答案,现在它已经存在且正常工作.但是,我需要这个选择.我需要强制用户在表单提交之前选择文本选项.用纯HTML做任何方法吗?我相信所需的选项变得混乱,因为我已经输入了"选中"参数.是这样的吗?我怎样才能解决这个问题.
因此,类似于带有验证的唯一规则(请参阅:https : //github.com/felixkiss/uniquewith-validator),我想知道如何生成一个条目,其中一列与另一列是唯一的。我想按如下方式播种数据库。
例:
“步骤”表中有12个步骤。每个步骤应具有与每个步骤相关联的5个类别,这些类别存储在“ step_categories”表中。这些类别中的每一个都被分配一个唯一的订单号1到5,这对于每个“ step_id”都是唯一的。
请参见此图以获取数据库外观示例:https : //imgur.com/a/XYA5yyn
对于上述图像示例,我不得不手动在数据库中进行输入。我不想每次都必须手动生成它,例如说我犯了一个错误,而不必回滚迁移。
我正在使用工厂生成此数据。所以工厂名称是StepCategoriesFactory.php,显然我正在使用文件中的create()方法调用工厂DatabaseSeeder.php。
我考虑过要在for循环中执行此操作,然后我才意识到当我调用the 'step_id' => App\Model::all()->random()->id来获取新ID时,我无法确保我没有获取我刚刚为其生成5个条目的ID 。我真的是Laravel的新手,我不确定从哪里开始。没有关于SO的真实信息,伪造者可以在另一列中使用唯一性。我将如何处理?
注意:步骤ID并不总是将为1-12。步骤ID可能会有所不同,具体取决于步骤是否被删除并重新制作。因此,仅将分配step_id为等于1-12不会起作用。
更新:这是我刚刚编写的一些代码,我认为自己的方向正确。也许。我已经step_id按number字段抓取了,因为它始终是1-12,并且我已经从条目中抓取了IID。但是现在我被困在如何生成订单1-5而不重复自身的问题上。我还没有运行它,因为它不完整,我知道如果没有正确的订单号就会抛出错误。
更新2:我认为我在这里是对的。但是我收到一个未定义的变量错误。当我从匿名函数中放入第一行时,它会将每个条目的顺序重置为“ 1”。如何使$ autoIncrement变量可用于匿名函数?Seeder在两次更新之间保持不变。
错误图片:https : //imgur.com/a/ywkd0Lb终端中带有Die / Dump错误的第二张图片:https : //imgur.com/a/rOGRv32
在此处引用此文章:https : //laracasts.com/discuss/channels/laravel/model-factory-increment-value-faker?page=1
更新3:我忘记use ($autoIncrement)了匿名函数的代码行。下面的代码已更新,但是现在我收到另一个错误,指出订单列具有空值,无法插入。显然它应该是“ 1”。即使在我调用将$autoIncrement->next();其递增为“ 1”的my之后,它仍然会根据终端返回null。但是,当我对它进行一次死注时,$autoIncrement->current()它返回1.奇怪。
更新3错误:https : //imgur.com/a/STOmIjF
StepCategoriesFactory.php
use Faker\Generator as Faker;
$autoIncrement = autoIncrement();
$factory->define(App\StepCategory::class, function …Run Code Online (Sandbox Code Playgroud) 所以我基本上要做的是将我的长代码重构为更简单的代码.我在这个网站上找到了这段代码,我并不真正理解代码中发生了什么.我不认为这段代码会起作用,因为我使用不同的策略和方法然后是标准的.
站点代码片段:
//PermissionsServiceProvider.php
public function boot()
{
Permission::get()->map(function($permission){
Gate::define($permission->slug, function($user) use ($permission){
return $user->hasPermissionTo($permission);
});
});
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释这段代码究竟发生了什么吗?
我的代码:
// Posts Policy
Gate::define('post.view', 'App\Policies\Blog\PostsPolicy@view');
Gate::define('post.create', 'App\Policies\Blog\PostsPolicy@create');
Gate::define('post.update', 'App\Policies\Blog\PostsPolicy@update');
Gate::define('post.delete', 'App\Policies\Blog\PostsPolicy@delete');
Gate::define('post.publish', 'App\Policies\Blog\PostsPolicy@publish');
Gate::define('post.edit', 'App\Policies\Blog\PostsPolicy@edit');
Gate::define('post.global', 'App\Policies\Blog\PostsPolicy@global');
// Categories Policy
Gate::define('category.view', 'App\Policies\Blog\CategoriesPolicy@view');
Gate::define('category.create', 'App\Policies\Blog\CategoriesPolicy@create');
Gate::define('category.update', 'App\Policies\Blog\CategoriesPolicy@update');
Gate::define('category.delete', 'App\Policies\Blog\CategoriesPolicy@delete');
Gate::define('category.edit', 'App\Policies\Blog\CategoriesPolicy@edit');
Gate::define('category.global', 'App\Policies\Blog\CategoriesPolicy@global');
// Tags Policy
Gate::define('tag.view', 'App\Policies\Blog\TagsPolicy@view');
Gate::define('tag.create', 'App\Policies\Blog\TagsPolicy@create');
Gate::define('tag.update', 'App\Policies\Blog\TagsPolicy@update');
Gate::define('tag.delete', 'App\Policies\Blog\TagsPolicy@delete');
Gate::define('tag.edit', 'App\Policies\Blog\TagsPolicy@edit');
Gate::define('tag.global', 'App\Policies\Blog\TagsPolicy@global');
// Parts Section Policy
Gate::define('part.section.view', 'App\Policies\Parts\PartSectionsPolicy@view');
Gate::define('part.section.create', 'App\Policies\Parts\PartSectionsPolicy@create');
Gate::define('part.section.update', 'App\Policies\Parts\PartSectionsPolicy@update');
Gate::define('part.section.delete', 'App\Policies\Parts\PartSectionsPolicy@delete'); …Run Code Online (Sandbox Code Playgroud) 所以出于某种原因,我以为我之前遇到过这个问题的答案,但对于我的生活,我无法通过Google或StackOverflow再次找到答案.这可能只是一个橡皮鸭问题而且我很抱歉,如果是的话,但我希望这个问题对某人来说会有一些用处.
让我们假设我们从一个全新安装的Laravel 5.4开始.我php artisan make:auth在终端中运行命令,它为我设置了身份验证脚手架.现在在我的/routes/web.php文件中,我看到以下行:
Auth::routes();
哪个很棒,路由列表中列出了所有的Authentication路由,包括定义的注销路由.(键入php artisan r:l以仔细检查)现在我想使用自定义Logout Controller为用户设置自定义注销路由.现在,我认为有一种方法可以链接到一组名为'except()'的资源路由,但就我而言,我在文档中找不到有关此方法的任何信息.我不知道这种方法是否存在,更不用说知道传递什么了.
我假设Auth::routes()代可以使用像资源路径中的except方法,但我不完全确定如何实现它?
所以问题很简单.如何包括除注销路由之外的所有身份验证路由,然后我将使用以下行定义注销路由.
Route::get('logout', 'LogoutController@userLogout')->name('logout');
很抱歉,如果这是一个重复的条目,我在过去一小时内使用了搜索栏,没有回答我的问题.
编辑:我对资源路由进行了更多的研究,并意识到它不是我链接到路由的方法,而是一个带有键值对的数组.请参阅下面的代码(从laravel docs中删除).
Route::resource('photo', 'PhotoController', ['except' => [
'create', 'store', 'update', 'destroy'
]]);
Run Code Online (Sandbox Code Playgroud)
但是当我将数组传递给routes()方法时(参见下面的代码),注销路径仍然在路由列表中.但是,该php artisan r:l命令不会抛出任何错误.
// User Authentication Routes
Auth::routes(['except' => 'logout']);
Run Code Online (Sandbox Code Playgroud)
编辑:经过大量挖掘,似乎不可能做这种类型的功能.我已向laravel/framework github repo提交了一个问题,要求在5.5中添加该功能.
因此,每当我运行composer self-update或composer selfupdate出现以下错误时:
[ErrorException]
rename(/Users/jrobinson/.composer/cache/composer-temp.phar,/usr/local/bin/composer): 权限被拒绝
我不太确定发生了什么。我想我可能搞砸了我的.bash_profile文件。在我的机器上安装 MySQL 并更新我的.bash_profile 中的第一行之前,我没有遇到这个问题。
我看过其他文章,并尝试使用下载页面上的 getcomposer.org 命令重新安装。这些解决方案都没有解决我的问题。所以请不要将此标记为重复问题,因为 SO 上的任何解决方案都没有帮助我解决这个问题。
这是我的 .bash_profile 文件内容。
export PATH="~/.composer/vendor/bin:$PATH:/usr/local/mysql/bin"
export EDITOR='sub -w'
Run Code Online (Sandbox Code Playgroud)
这是ls -la在/usr/local/bin文件夹中运行的命令。
目标
能够在所有这些结束时从我的终端运行composer self-update命令和mysql命令。
我搜索了又搜索,但在 SO 上找不到我的问题的答案。所以这是我的问题。我正在尝试使用 Laravel Mix 全局加载 jQuery。我已经尝试修改各种文件,但似乎没有任何效果……我仍然收到“$ 未定义”错误。
这是我的代码。
Bootstrap.js
window._ = require('lodash');
window.Popper = require('popper.js').default;
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
Run Code Online (Sandbox Code Playgroud)
webpack.mix.js
mix.js('resources/assets/js/app.js', 'public/js')
.js('resources/assets/js/select2.min.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.sass('resources/assets/sass/admin/app.scss', 'public/css/admin')
.copy('resources/assets/css/fontawesome.css', 'public/css')
.copy('resources/assets/css/select2.min.css', 'public/css')
.copy('resources/assets/webfonts', 'public/webfonts')
.copy('resources/assets/js/tinymce', 'public/js/tinymce');
mix.browserSync('http://localhost:8000');
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
未捕获的 ReferenceError: $ 未定义
@section('scripts') 中 create.blade.php 内部的代码 …
我使用 registerNewUser 方法创建了一个 PHP Mail 类。基本上正如我的标题所说,该方法返回 true,而它应该返回 false,因为我没有收到电子邮件。有人可以解释我是否做错了什么吗?(请不要评论我向用户发送未加密的密码。此未加密的密码是一个 25 个字符的字母数字随机生成的密码。安全性在这里不是问题)我不希望使用某种类型的框架。我想手动编写代码,所以请不要告诉我使用 PHPMailer 或类似的东西。
编辑:我发现我需要设置 XAMPP 以使用 SMTP 发送传出电子邮件。这个问题不是重复的,因为每个其他问题的所有其他答案都是基于 Windows 的,即使如此,也没有提供有关如何在 XAMPP 本地主机服务器上设置 SMTP 的分步说明。
这是代码:
<?php
class Mail {
private $headers;
public function __construct() {
// Setting Up Mail Headers
$this->headers = "MIME-Version: 1.0 \r\n";
$this->headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$this->headers .= "From: Admin @ NAStepsOnline <no-reply@NAStepsOnline.com>\r\n"."X-Mailer: php";
}
/**
* @desc Mails the User from registration email.
* This function should only be called through
* registerNew() within the …Run Code Online (Sandbox Code Playgroud) 所以我有桌子questions和桌子steps.该steps可以有很多questions.在question属于一个"台阶".
我有一个step_id在我的领域questions表,这是一个外键id字段在我的steps表.我的表中也有一个number字段steps与id字段没有任何关系.这只是一个数字(1-12).
问题表
---------------------------
| id | step_id | question |
---------------------------
Run Code Online (Sandbox Code Playgroud)
步骤表
-----------------------------
| id | number | description |
-----------------------------
Run Code Online (Sandbox Code Playgroud)
我之间的关系很好,因为我可以创建,更新和删除questions表中的问题.但是,我正在处理索引页面,我想抓住所有问题并按表中的number字段对它们进行排序steps.
我已经完成了一些研究,我从Laracasts中找到了一些代码,但它没有用.网站上没有提供太多信息.我是否需要依赖项才能获得此功能,或者是否有本地方法在laravel中执行此操作.
$questions = Question::join('steps', 'questions.step_id', '=', 'questions.id')
->orderBy('questions.number', 'asc')
->get(['questions.*']);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
未发现柱::SQLSTATE [42S22]在'顺序条款' 1054未知列'questions.number'(SQL:选择
questions.*从questions内连接steps上questions.step_id=questions.id …
所以我在我的项目中使用 TinyMCE 编辑器来处理我的所有文本区域。可以在此处找到有关 TinyMCE 编辑器的更多文档。
我有四个不同的按钮,但是让我们只关注一个按钮,我确信代码可以从一个按钮复制到其他按钮,因为我希望所有按钮都具有相同的功能。
这是按钮:
这些按钮会调出 Bootstrap v4 模式。可以在此处找到有关 Modal 的文档。模态示例如下。
现在让我们只关注Reminders Modal。这显然是形式的一部分。当我调出模态时,我在自动聚焦 textarea 时遇到了问题。我正在将模态加载到页面上,但在激活之前保持隐藏状态,但单击按钮。我不是动态渲染这个模态,它是一个静态模态。
所以让我们回顾一下我的代码。请记住,我们此时只关注提醒按钮和模态,因此下面只会显示与该模态相关的代码。
注意:我使用的是 Laravel 5.6,我使用 Laravel Blade 将模态包含在页面中。这些信息不应该对这个问题的答案产生影响,只是说它只是为了以防万一它可能是相关的。
我还使用 Laravel Collective 来在页面上显示文本区域。可以在此处找到有关 Laravel 集体文本区域的更多文档。Laravel Collective 不再是 Laravel 框架的原生版本,因此这些信息也可能相关,但可能性很小。
Run Code Online (Sandbox Code Playgroud)@include('components.form.part.modals.reminder')
模态(/resources/views/components/form/part/modals/reminder.blade.php)
<!-- Modal -->
<div class="modal fade" id="reminderModal" tabindex="-1" role="dialog" aria-labelledby="reminderModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="reminderModalLabel">Reminder</h5>
</div>
<div class="modal-body">
{{ Form::textarea('reminder', null, ['class' => 'form-control', …Run Code Online (Sandbox Code Playgroud) php ×6
laravel ×5
javascript ×2
jquery ×2
laravel-5 ×2
mysql ×2
bash ×1
class ×1
composer-php ×1
eloquent ×1
email ×1
factories ×1
faker ×1
foreign-keys ×1
html ×1
laravel-mix ×1
methods ×1
routes ×1
tinymce ×1