journal-intime/app/Http/Controllers/HomeController.php

68 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Post;
use DateTime;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
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()
{
setlocale(LC_TIME, 'fr_FR.utf8');
$today = new DateTime();
$user_id = Auth::user()->getAuthIdentifier();
$posts = Post::where('user_id', $user_id)->orderBy('date_post', 'DESC')->get();
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
->where('user_id', $user_id)
->count();
$already = false;
if ($today_post > 0) {
$already = true;
}
return view('home', [
'already' => $already,
'today' => $today,
'posts' => $posts,
]);
}
public function store()
{
$today = new DateTime();
$data = request()->validate([
'message' => 'required'
]);
$data = [
'user_id' => Auth::user()->getAuthIdentifier(),
'date_post' => new DateTime(), // Take back the date from the form ?
'content' => $data['message'],
];
$check = Post::create($data);
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
}
}