2016-05-13 15 views
1

Ich habe einen 2-Tabelle Benutzer und meldetLaravel 5.2 - Speicher-Mehrwert mit Summe

Report-Modell

public function user() { 
    return $this->belongsTo('App\User', 'author_id'); 
} 

User-Modell

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

In User-Modell gibt es eine Spalte namens " sum_sales " In Bericht Modell gibt es eine Spalte namens" Gesamt "

Ho kann ich übergeben die Summe (Summe) von meinem Berichtsmodell zu "sum_total" in meinem Benutzermodell? Mein Bericht Controller

public function store(Request $request) 
{ 
    $this->validate($request, ['title' => 'required', 
          'date' => 'required|date_format:d/m/Y|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/', 
          'image_1' => 'required|mimes:png,jpeg', 
          'products' => 'required', 
          'total' => 'required', 
          'time' => 'required|min:2', 
          'location' => 'required', 
          'sub_location' => 'required', 
          ]); 

    $user = Auth::user()->id; 

    $report = new Report($request->all()); 
    $report->author_id = $user; 

    $image = $request->file('image_1'); 
    $destinationPath = 'uploads/reports'; 
    $ext = $image->getClientOriginalExtension(); 
    $fileName = rand(11111,99999).'.'.$ext; 

    $report->image_1 = $image->move($destinationPath, $fileName); 

    $field_total = $request->input('total'); 
    $sum_total = $report->sum('total'); 
    $totalSum = $field_total + $sum_total; 

    $report->author_id->sum_sales = $totalSum;  

    $report->save(); 

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

    return redirect('dash/reports'); 
} 

dies mit dem Browser sagen:

Indirect modification of overloaded property App\Report::$author_id has no effect 

Wie kann ich herausfinden?

+0

Statt '$ Report-> author_id-> sum_sales', schreiben' $ user-> sum_sales' und Sie sollten gut zu gehen. –

+0

Ich versuche, aber wirf mich: ** Versuch, die Eigenschaft von Nicht-Objekt zuweisen ** – user0111001101

+1

Whoops, bemerkte nicht, dass '$ user' ist nur eine ID. Dann schreibe 'Auth :: user() -> update (['sum_sales' => $ totalSum])'. Stellen Sie sicher, dass sich das Attribut 'sum_sales' in einem ausfüllbaren Array befindet. –

Antwort

0

gelöst durch Giedrius Kiršys

public function store(Request $request) 
{ 
    $this->validate($request, ['title' => 'required', 
          'date' => 'required|date_format:d/m/Y|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/', 
          'image_1' => 'required|mimes:png,jpeg', 
          'products' => 'required', 
          'total' => 'required', 
          'time' => 'required|min:2', 
          'location' => 'required', 
          'sub_location' => 'required', 
          ]); 

    $user = Auth::user()->id; 

    $report = new Report($request->all()); 
    $report->author_id = $user; 

    $image = $request->file('image_1'); 
    $destinationPath = 'uploads/reports'; 
    $ext = $image->getClientOriginalExtension(); 
    $fileName = rand(11111,99999).'.'.$ext; 

    $report->image_1 = $image->move($destinationPath, $fileName); 
    $report->save(); 

    $sumUserTotal = User::find($user)->reports->sum('total'); 
    Auth::user()->update(['sum_sales' => $sumUserTotal]); 

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

    return redirect('dash/reports'); 
}