50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Http\Request;
|
|
use App\Mood;
|
|
use App\Diary;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
/**
|
|
* Show the application dashboard.
|
|
*
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$votedToday = Diary::votedToday();
|
|
$moods = Mood::all();
|
|
$mood = $request->input('mood');
|
|
if (!$votedToday && empty($mood)) {
|
|
return view('new_record', [
|
|
'moods' => $moods
|
|
]);
|
|
}
|
|
if (!empty($mood)) {
|
|
Diary::addRecord($mood);
|
|
$request->session()->flash('status', __('Logged your mood.'));
|
|
// Clear GET parameters.
|
|
return redirect('/home');
|
|
}
|
|
$history = Diary::with('mood')->where('user_id', Auth::id())->where('created_at', '>', strtotime('-1 year'))->get();
|
|
return view('home', [
|
|
'moods' => $moods,
|
|
'history' => $history
|
|
]);
|
|
}
|
|
}
|