Laravel5.1框架中如何实现批量赋值操作?

2026-04-01 08:181阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计456个文字,预计阅读时间需要2分钟。

Laravel5.1框架中如何实现批量赋值操作?

本文介绍了Laravel 5.1框架下批量赋值实现方法。以下是一个简单的示例:

1. 首先,创建一个模型(例如User):

phpclass User extends Model{ protected $table='users';}

2. 在控制器中,使用批量赋值方法:

phppublic function store(Request $request){ User::insert([ ['name'=> $request->name, 'email'=> $request->email], ['name'=> $request->name2, 'email'=> $request->email2], // 更多数据... ]);}

3. 官方中文文档:[Laravel 5.1 Eloquent文档](http://laravel-china.org/docs/5.1/eloquent)

本文实例讲述了laravel5.1框架下的批量赋值实现方法。分享给大家供大家参考,具体如下:

官方中文文档在这里:

laravel-china.org/docs/5.1/eloquent#%E6%89%B9%E9%87%8F%E8%B5%8B%E5%80%BC

我先来说明一下一个场景:

你想要往数据库中存评论,在控制器的代码如下:

$comment->comment_id= $id; $comment->title = $name; $comment->url = $url; $comment->introduction = $profile; if ($comment->save()) { return redirect('admin/comment'); } else { return redirect()->back()->withInput()->withErrors('保存失败!')

设想一下如果这个评论表的字段有很多,岂不是要一个字段一个字段的存储,代码量太高。laravel框架提供了一个叫做批量赋值的功能:

控制器代码如下:

Laravel5.1框架中如何实现批量赋值操作?

public function store(Request $request) { if (Comment::create($request->all())) { return redirect()->back(); } else { return redirect()->back()->withInput()->withErrors('评论发表失败!'); } }

对应的App\models中的Comment类:

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = ['nickname', 'email', 'website', 'content','article_id']; } protected $fillable= ['nickname','email','website','content','article_id'];

这一行就表示控制器中得到的数据全部存入相应字段,是不是很简单方便?

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

本文共计456个文字,预计阅读时间需要2分钟。

Laravel5.1框架中如何实现批量赋值操作?

本文介绍了Laravel 5.1框架下批量赋值实现方法。以下是一个简单的示例:

1. 首先,创建一个模型(例如User):

phpclass User extends Model{ protected $table='users';}

2. 在控制器中,使用批量赋值方法:

phppublic function store(Request $request){ User::insert([ ['name'=> $request->name, 'email'=> $request->email], ['name'=> $request->name2, 'email'=> $request->email2], // 更多数据... ]);}

3. 官方中文文档:[Laravel 5.1 Eloquent文档](http://laravel-china.org/docs/5.1/eloquent)

本文实例讲述了laravel5.1框架下的批量赋值实现方法。分享给大家供大家参考,具体如下:

官方中文文档在这里:

laravel-china.org/docs/5.1/eloquent#%E6%89%B9%E9%87%8F%E8%B5%8B%E5%80%BC

我先来说明一下一个场景:

你想要往数据库中存评论,在控制器的代码如下:

$comment->comment_id= $id; $comment->title = $name; $comment->url = $url; $comment->introduction = $profile; if ($comment->save()) { return redirect('admin/comment'); } else { return redirect()->back()->withInput()->withErrors('保存失败!')

设想一下如果这个评论表的字段有很多,岂不是要一个字段一个字段的存储,代码量太高。laravel框架提供了一个叫做批量赋值的功能:

控制器代码如下:

Laravel5.1框架中如何实现批量赋值操作?

public function store(Request $request) { if (Comment::create($request->all())) { return redirect()->back(); } else { return redirect()->back()->withInput()->withErrors('评论发表失败!'); } }

对应的App\models中的Comment类:

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = ['nickname', 'email', 'website', 'content','article_id']; } protected $fillable= ['nickname','email','website','content','article_id'];

这一行就表示控制器中得到的数据全部存入相应字段,是不是很简单方便?

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。