🎨 Empêche 2 posts par jour et améliore l'affichage
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Post;
|
||||
use DateTime;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
|
||||
@@ -26,11 +27,20 @@ class HomeController extends Controller
|
||||
public function index()
|
||||
{
|
||||
setlocale(LC_TIME, 'fr_FR.utf8');
|
||||
$today = new \DateTime();
|
||||
$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();
|
||||
|
||||
$posts = \App\Post::where('user_id', Auth::user()->getAuthIdentifier())->get();
|
||||
$already = false;
|
||||
if ($today_post > 0) {
|
||||
$already = true;
|
||||
}
|
||||
|
||||
return view('home', [
|
||||
'already' => $already,
|
||||
'today' => $today,
|
||||
'posts' => $posts,
|
||||
]);
|
||||
@@ -38,17 +48,19 @@ class HomeController extends Controller
|
||||
|
||||
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 ?
|
||||
'date_post' => new DateTime(), // Take back the date from the form ?
|
||||
'content' => $data['message'],
|
||||
];
|
||||
|
||||
$check = \App\Post::create($data);
|
||||
$check = Post::create($data);
|
||||
|
||||
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
||||
}
|
||||
|
15
app/Http/Controllers/WelcomeController.php
Normal file
15
app/Http/Controllers/WelcomeController.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class WelcomeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('welcome', [
|
||||
'today' => new \DateTime(),
|
||||
]);
|
||||
}
|
||||
}
|
@@ -23,4 +23,13 @@ class Post extends Model
|
||||
protected $hidden = [
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'date_post' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user