简体   繁体   English

如何在laravel中将具有用户ID的表格数据插入表中?

[英]How do I insert into table form data with user id in laravel?

So my create news form is very simple: 因此,我的创建新闻表单非常简单:

  <div class="row padding-10">
{!! Form::open(array('class' => 'form-horizontal margin-top-10')) !!}
<div class="form-group">
  {!! Form::label('title', 'Title', ['class' => 'col-md-1 control-label padding-right-10']) !!}
  <div class="col-md-offset-0 col-md-11">
  {!! Form::text('title', null, ['class' => 'form-control']) !!}
  </div>
</div>
<div class="form-group">
  {!! Form::label('body', 'Body', ['class' => 'col-md-1 control-label padding-right-10']) !!}
  <div class="col-md-offset-0 col-md-11">
  {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
  </div>
</div>
<div class="col-md-offset-5 col-md-3">
  {!! Form::submit('Submit News', ['class' => 'btn btn-primary form-control', 'onclick' => 'this.disabled=true;this.value="Sending, please wait...";this.form.submit();']) !!}
</div>
{!! Form::close() !!}

This is processed by NewsProvider: 这是由NewsProvider处理的:

    public function store()
{
$validator = Validator::make($data = Input::all(), array(
  'title' => 'required|min:8',
  'body' => 'required|min:8',
));

if ($validator->fails())
{
  return Redirect::back()->withErrors($validator)->withInput();
}

News::create($data);

return Redirect::to('/news');
}

But i have another field, not only title and text body in database, which is author_id and I have no idea how to add info, like the user id from currently authenticated user which wasnt supplied by form. 但是我还有另一个字段,不仅是数据库中的标题和文本正文,它是author_id,而且我不知道如何添加信息,例如当前未通过表单提供的经过身份验证的用户的用户ID。 I know how to add hidden input to form with user id, but then someone could change hidden field value. 我知道如何将隐藏的输入添加到具有用户ID的表单中,但是有人可以更改隐藏的字段值。 How do I do that correct way? 我该如何正确做?

Maybe I have to edit my news eloquent model in some way, which is: 也许我必须以某种方式编辑新闻雄辩的模型,即:

use Illuminate\Database\Eloquent\Model as Eloquent;

class News extends Eloquent { 新闻类扩展口才{

  // Add your validation rules here
  public static $rules = [
    'title' => 'required|min:8',
    'body' => 'required|min:8',
  ];

  // Don't forget to fill this array
  protected $fillable = array('title', 'body');

}

You can always get the current authenticated user by Auth::user() . 您总是可以通过Auth::user()获得当前经过身份验证的用户。 And you can also modify the $data array before passing it to create . 您还可以在将$data数组传递给create之前对其进行修改。 Here's how you do it: 这是您的操作方式:

$data['author_id'] = Auth::user()->id;
News::create($data);

Also don't forget to add author_id to the fillable attributes 同样不要忘记将author_id添加到fillable属性

protected $fillable = array('title', 'body', 'author_id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据user_id插入数据 - How do I insert data based on user_id MySQL / PHP:如何将登录的用户ID插入另一个表,该表从用户输入的表单收集数据 - MySQL/PHP: How to insert logged in user id into another table that is gathering data from a form that the user is inputting 如何将表格中的数据插入到表格中 - How do I insert data from a table to a form 我如何获取用户的数据并将其插入到另一个表中 - how do i get user seesion data and insert it to another table 在 Laravel 中,如何从用户表中检索随机 user_id 以生成模型工厂种子数据? - In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation? Laravel 6:如何将 user_id 和 role_id(s) 发送到控制器,以同步 user_role 数据透视表? - Laravel 6: how do I send user_id with role_id(s) to controller, in order to sync user_role pivot table? 如何将数据插入表的列(2个表中的一个)中,以使用户ID在两个表中均匹配? - How can i insert data into a column of a table (out of 2 tables) such that user id matches in both of them? 如何通过 id 从 laravel 特定表中传递数据 - how to pass data by id form a laravel specific table 如何使用 Laravel Ajax 在表中保存带有用户身份验证 ID 的数据 - How to Save Data With User Auth id In Table Using Laravel Ajax 如何使用Laravel 5.4根据用户ID提取数据 - How do I fetch data based on user id using Laravel 5.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM