Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用作者名称存储数据-laravel 5.2_Laravel_Eloquent_Has Many_Relation - Fatal编程技术网

使用作者名称存储数据-laravel 5.2

使用作者名称存储数据-laravel 5.2,laravel,eloquent,has-many,relation,Laravel,Eloquent,Has Many,Relation,我与我的模型用户和报告有很多关系。 我想为报告设置作者姓名。(就像一个博客作者) 我的型号用户: public function reports() { return $this->hasMany('App\Report', 'author_id'); } public function create() { $category = Category::lists('title','id'); return view('dash.reports.create')-

我与我的模型用户和报告有很多关系。 我想为报告设置作者姓名。(就像一个博客作者)

我的型号用户:

public function reports() {
    return $this->hasMany('App\Report', 'author_id');
}
public function create()
{
    $category = Category::lists('title','id');
    return view('dash.reports.create')->with('category', $category);
}

/**
 * Store a newly created resource in storage.
 *
 * @return void
 */
public function store(Request $request)
{
    $this->validate($request, ['title' => 'required', ]);

    Report::create($request->all());

    Session::flash('flash_message', 'Report added!');

    return redirect('dash/reports');
}
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
                {!! $errors->first('title', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}

            <div class="col-sm-2">
                {!! Form::selectRange('day', 1, 31, null,  ['class' => 'form-control']) !!}
                {!! $errors->first('day', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::selectMonth('month', null,  ['class' => 'form-control']) !!}
                {!! $errors->first('month', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null,  ['class' => 'form-control']) !!}
                {!! $errors->first('year', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

        <div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
            {!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
                {!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
            </div>
        </div>


<div class="form-group">
    <div class="col-sm-offset-3 col-sm-3">
        {!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
    </div>
</div>
{!! Form::close() !!}
模型报告

public function user() {
    return $this->belongsTo('App\User', 'author_id');
}
和我的控制器:

public function reports() {
    return $this->hasMany('App\Report', 'author_id');
}
public function create()
{
    $category = Category::lists('title','id');
    return view('dash.reports.create')->with('category', $category);
}

/**
 * Store a newly created resource in storage.
 *
 * @return void
 */
public function store(Request $request)
{
    $this->validate($request, ['title' => 'required', ]);

    Report::create($request->all());

    Session::flash('flash_message', 'Report added!');

    return redirect('dash/reports');
}
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
                {!! $errors->first('title', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}

            <div class="col-sm-2">
                {!! Form::selectRange('day', 1, 31, null,  ['class' => 'form-control']) !!}
                {!! $errors->first('day', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::selectMonth('month', null,  ['class' => 'form-control']) !!}
                {!! $errors->first('month', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null,  ['class' => 'form-control']) !!}
                {!! $errors->first('year', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

        <div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
            {!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
                {!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
            </div>
        </div>


<div class="form-group">
    <div class="col-sm-offset-3 col-sm-3">
        {!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
    </div>
</div>
{!! Form::close() !!}
我可以在phpmyadmin中设置,但如何使用控制器设置

编辑:我的视图:

public function reports() {
    return $this->hasMany('App\Report', 'author_id');
}
public function create()
{
    $category = Category::lists('title','id');
    return view('dash.reports.create')->with('category', $category);
}

/**
 * Store a newly created resource in storage.
 *
 * @return void
 */
public function store(Request $request)
{
    $this->validate($request, ['title' => 'required', ]);

    Report::create($request->all());

    Session::flash('flash_message', 'Report added!');

    return redirect('dash/reports');
}
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
                {!! $errors->first('title', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

       <div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
            {!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}

            <div class="col-sm-2">
                {!! Form::selectRange('day', 1, 31, null,  ['class' => 'form-control']) !!}
                {!! $errors->first('day', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::selectMonth('month', null,  ['class' => 'form-control']) !!}
                {!! $errors->first('month', '<p class="help-block">:message</p>') !!}
            </div>

            <div class="col-sm-2">
                {!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null,  ['class' => 'form-control']) !!}
                {!! $errors->first('year', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

        <div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
            {!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
                {!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
            </div>
        </div>


<div class="form-group">
    <div class="col-sm-offset-3 col-sm-3">
        {!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
    </div>
</div>
{!! Form::close() !!}
{!!Form::open(['url'=>'/dash/reports','class'=>'Form horizontal'])
{!!Form::label('title','Servizio',['class'=>'col-sm-3 control label'])
{!!Form::text('title',null,['class'=>'表单控件','必需'=>'必需'])
{!!$errors->first('title','help block'>:message

) {!!Form::label('date','Data lavoratia',['class'=>'col-sm-3 control label']) {!!Form::selectRange('day',1,31,null,['class'=>'Form control']) {!!$errors->first('day','help block'>:message

') {!!Form::selectMonth('month',null,['class'=>'Form control']) {!!$errors->first('month','

:message

) {!!Form::select('year',array('2016'=>'2016','2015'=>'2015'),null,['class'=>'Form control']) {!!$errors->first('year','

:message

) {!!Form::label('category_id','Cliente',['class'=>'col-sm-3 control label']) {!!Form::select('category_id',$category,null,['class'=>'Form control']) {!!$errors->first('category_id','help block'>:message

') {!!表单::提交('Create',['class'=>'btn btn primary Form control']) {!!Form::close()!!}
非常简单。用此替换
Report::create…

$user   = Auth::user();
$report = new Report($request->all());
$report->author()->associate($user);
$report->save();
确保
使用Auth在顶部向上

这将使用
Auth
对象获取当前用户
使用
$request
数据生成新的
报告,而不保存,
告诉我们将模型的
$user
关联为
作者的报告,
使用作者信息保存报告。

解决方案:

public function store(Request $request)
{
    $this->validate($request, ['title' => 'required', ]);

    $user = Auth::user()->id;
    $report = new Report($request->all());
    $report->author_id = $user;
    $report->save();

    Session::flash('flash_message', 'Report added!');

    return redirect('dash/reports');
}

我可以帮忙,但我有点不清楚。您是否有用户、作者和报告的唯一id(自动递增主键)?如果是这样,就更容易了。例如,类似于
DB::table('reports')->where('id',1)->update(['author\u id'=>$user\u id])这是一种比根据我的评论进行原始更新更好的OOP方式:)我尝试了,但按下按钮后浏览器返回空白页编辑我的帖子以插入视图可能有很多事情。