在 Laravel 验证中,
$this->validate($request, [
'email' => 'exists:users,email',
]);
Run Code Online (Sandbox Code Playgroud)
意味着用户输入的电子邮件应该存在于表中的email字段中users。我想确保表中不存在用户输入的电子邮件以避免重复条目。对此有什么巧妙的方法吗?
我想尝试类似的东西:!exists:users,name但那是无效的。
我使用带有CORS中间件的Laravel构建了一个API.
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers','Content-Type, Authorization, X-XSRF-TOKEN');
}
}
Run Code Online (Sandbox Code Playgroud)
当尝试通过API访问数据时localhost:8000/api/items,我在Laravel终端上获得以下URL
调用未定义的方法Symfony\Component\HttpFoundation\Response :: header()
我错过了什么吗?
HomeComponent
ngOnInit()
{
console.log('loaded');
this.retrieveData();
}
retrieveData()
{
// this.dataService.getData().subscribe(...);
}
Run Code Online (Sandbox Code Playgroud)
组件加载时,我正在检索数据。routerLink例如,当用户单击另一个,SettingsComponent然后返回时HomeComponent,当然会再次调用该函数,因为该组件已再次加载。但是,每当我返回该组件时,它都会再次调用该函数,这会创建太多不需要的HTTP请求。我需要防止这种情况,并确保仅在第一次调用该函数。我该怎么做呢?我应该使用其他组件生命周期挂钩吗?
我正在尝试setState(),以便在调整窗口大小时在设置为组件状态的不同屏幕尺寸上呈现不同数量的项目。我在组件安装时添加了一个事件侦听器,在卸载时将其删除了。
componentDidMount()
{
this.updateValue();
window.addEventListener("resize", this.updateValue.bind(this));
}
updateValue()
{
var windowWidth = window.screen.width;
if(windowWidth <= 991 && windowWidth >= 768){
this.setState({ items:6 })
} else if(windowWidth <= 767 && windowWidth >= 479){
this.setState({ items:4 })
} else if( windowWidth < 480 && windowWidth >= 359){
this.setState({ items:2 })
} else if( windowWidth < 360){
this.setState({ items: 2})
} else {
this.setState({items:12})
}
}
componentWillUnmount()
{
window.removeEventListener("resize", this.updateValue.bind(this));
}
Run Code Online (Sandbox Code Playgroud)
在安装了组件的路线上工作正常;直到我通过打开另一条路线离开组件并调整窗口的大小(这是我收到错误的时间):
警告:setState(...):只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是无人值守。请检查FeaturedRestaurants组件的代码。
调整屏幕大小时,每秒我会收到一百个错误。
显然,removeEventListener无法正常工作。我哪里做错了?
我正在使用Laravel 5.4设置一个表单,用户可以在其中输入食物名称及其图像。尝试上传PNG或JPG图片时,出现以下内容Validation Error:
该图像必须是图像。
如果删除图像验证,则会得到FatalErrorException:
在null上调用成员函数getClientOriginalExtension()
这是Intervention /图片库提供的帮助程序功能。这可能意味着该图像根本没有上传。
FORM
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
Run Code Online (Sandbox Code Playgroud)
CONTROLLER
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename; …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个first word在字符串中打印的管道。以下是我的错误代码,该代码不希望地显示first letter字符串。
例如。
PIPE
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'firstWord'
})
export class GetFirstWord implements PipeTransform
{
transform(value: string, args: any[]): string | boolean
{
if (value === null) {return false;}
const firstWords = [];
for (let i = 0; i < value.length; i++)
{
const words = value[i].split(' ');
firstWords.push(words[0]);
}
return firstWords[0];
}
}
Run Code Online (Sandbox Code Playgroud)
COMPONENT
userName: string = 'Chuck Norris';
Run Code Online (Sandbox Code Playgroud)
TEMPLATE
{{ userName | firstWord }}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
C
DESIRED …
为了获取当天的所有记录,我做了
$dt = Carbon::now();
$dataToday = Data::whereDay('created_at', $dt->day)->get();
Run Code Online (Sandbox Code Playgroud)
为了获得本周的记录,我尝试了以下操作,但没有成功。
$dataThisWeek = Data::where('created_at', $dt->weekOfYear);
$dataThisWeek = Data::where('created_at', $dt->weekOfMonth);
$dataThisWeek = Data::whereWeek('created_at', $dt->week);
Run Code Online (Sandbox Code Playgroud)
我们如何通过Eloquent和Carbon(最好)或本机MYSQL函数来实现这一目标?
laravel ×3
laravel-5 ×3
angular ×2
laravel-5.4 ×2
angular-pipe ×1
api ×1
cors ×1
intervention ×1
javascript ×1
ngoninit ×1
php ×1
reactjs ×1
validation ×1