2016-05-03 10 views
2

私はモデルのユーザーとレポートに対してhasMany関係を持っています。 レポートの著者名を設定したいと思います。 (ブログ投稿者のように)作成者の名前のデータを保存しています - laravel 5.2

私のモデルユーザー:

public function reports() { 
    return $this->hasMany('App\Report', 'author_id'); 
} 

モデルレポート

public function user() { 
    return $this->belongsTo('App\User', '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'); 
} 

私はphpmyadminで設定することができますが、どうすれば私のコントローラで設定できますか?

編集:私の見解:

{!! 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() !!} 
+0

私は助けることができますが、私は何かに少しは不明です。ユーザー、作成者、およびレポート(自動インクリメントされた主キー)に固有のIDを持っていますか?もしそうなら、より簡単です。 – DrewT

+0

たとえば、 'DB :: table(' reports ') - >ここで(' id '、1) - > update([' author_id '=> $ user_id]);' – DrewT

答えて

5

非常に簡単。 Report::create...をこれに置き換えてください。

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

先頭にuse Auth;があることを確認してください。

これは
が保存せずに$requestデータを使用して新しいReportを構築し、現在のユーザーを取得するためにAuthオブジェクトを使用して、
我々はモデルのauthorとして$userを関連付けている報告書に指示し、
保存します著者情報を含むレポート。

+0

のようなものは、私のコメントごとに生の更新より:) – DrewT

+0

私は試してみるが、ボタンを押した後にブラウザが空白のページを返す 編集するために私の投稿を編集する – user0111001101

+1

何でもよい。 – Josh

0

ソリューション:

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'); 
} 
関連する問題