我尝试在Laravel中验证数组POST:
$validator = Validator::make($request->all(), [
"name.*" => 'required|distinct|min:3',
"amount.*" => 'required|integer|min:1',
"description.*" => "required|string"
]);
Run Code Online (Sandbox Code Playgroud)
我发送空POST并将其if ($validator->fails()) {}作为False.这意味着验证是正确的,但事实并非如此.
如何在Laravel中验证数组?当我提交表格时input name="name[]"
有实体User是在表stotedUsers
默认情况下,此表中的某些字段为null.
我需要更新这些字段并设置非空数据.
为此,我尝试PATCH在Laravel中使用方法:
路由:
Route::patch('users/update', 'UsersController@update');
Run Code Online (Sandbox Code Playgroud)
控制器:
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
"name" => 'required|string|min:3|max:50',
"email_work" => 'email|max:255|unique:users',
"surname" => 'required|string|min:3|max:50',
"tel" => 'required|numeric|size:11',
"country" => 'required|integer',
"region" => 'required|integer',
"city" => 'required|integer'
]);
if ($validator->fails()) {
return response()->json(["message" => $validator->errors()->all()], 400);
}
$user = User::where("user_id", $id)->update([
"name" => $request->name,
"surname" => $request->surname,
"tel" => $request->tel,
"country" => $request->country,
"city" => $request->city,
"region" => $request->region,
"email_work" => $request->email
]); …Run Code Online (Sandbox Code Playgroud) 当我有权访问存储库时,您能否解释一下如何使用git:
我尝试了这些步骤
git init
git clone git.repository
git pull develop (where develop is branch)
git add .
git commit -m "m"
git push origin develop
Run Code Online (Sandbox Code Playgroud)
结果是:
* branch develop -> FETCH_HEAD
fatal: refusing to merge unrelated histories
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我有与 SQL 服务器一起使用的项目。在Models目录中,我有像这样的迁移文件:
public partial class UserData : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.confirmation_code",
c => new
{
sys_id = c.Long(nullable: false, identity: true),
resource_id = c.Guid(nullable: false),
code = c.String(maxLength: 64),
user_id = c.Long(nullable: false),
id = c.Guid(nullable: false),
edit_date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.sys_id)
.ForeignKey("dbo.user", t => t.user_id, cascadeDelete: true)
.Index(t => t.user_id);
....
Run Code Online (Sandbox Code Playgroud)
对于开发,我使用 VisualStudio,如何运行所有迁移以进行部署?
我有两个字段:
landing.blade.php
Run Code Online (Sandbox Code Playgroud)
并阻止comments.blade.php。
我怎样才能在里面包含comments.blade.php landing.blade.php?
我$id在Laravel中得到一排,如:
$user = User::where("id", $id)->get();
Run Code Online (Sandbox Code Playgroud)
我需要做之后 return $user->first();
我可以更简短地使用此请求吗?
现在,为了获取数据,我从控制器调用方法,将数据作为JSON返回:
return response()->json([$data]);
Run Code Online (Sandbox Code Playgroud)
我可以在此响应中添加全局数据吗?合并这个$data?
例如$user,我想在每个HTTP响应中放弃全局对象,以避免每个方法中的以下条目:
return response()->json(["data" => $data, "user" => $user]);
Run Code Online (Sandbox Code Playgroud) 我有端点可用的 RESTful 服务。
例如,我api/main从服务器请求并获取 JSON 数据。
对于响应,我使用:
return response()->json(["categories" => $categories]);
Run Code Online (Sandbox Code Playgroud)
如何控制 URL 中响应传递参数的格式?
作为示例,我需要这个:api/main?format=json|html它适用response于控制器中的每个。
在我对DB的请求中,我使用Laravel的模型和功能with如下:
$user = User::with("city")->get();
Run Code Online (Sandbox Code Playgroud)
问题是如果要在模板中执行以下操作:
{{$user->city()->name}}
Run Code Online (Sandbox Code Playgroud)
只有当用户指定了city时才会起作用,因此数据库表中存在value.否则返回错误:
如何摆脱多余的检查,如:
@if(isset($user->city()->name))
{{$user->city()->name}}
@endif
Run Code Online (Sandbox Code Playgroud)
这真是太棒了!