Create Time with Carbon (ok)
PreviousJAVASCRIPT SERVICE WORKER CƠ BẢNNextHow to Add Real-Time Notifications to Laravel with Pusher
Last updated
Last updated
C:\xampp\htdocs\api\app\Http\Controllers\Api\V1\PostController.php
<?php
namespace App\Http\Controllers\Api\V1;
use App\Models\CategoryPost;
use App\Models\Post;
use Carbon\Carbon;
use Illuminate\Http\Request;
class PostController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$posts = Post::all();
return view('layouts.post.index')->with(compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
return view('layouts.post.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$getpost = new Post();
$getpost->title = $request->title;
$getpost->short_desc = $request->short_desc;
$getpost->desc = $request->desc;
$getpost->image = $request->image;
$getpost->post_category_id = $request->post_category_id;
$getpost->views = $request->views;
$getpost->post_date = Carbon::now("Asia/Ho_Chi_Minh")->format('d-m-Y');
$getpost->save();
return redirect('api/v1/post');
}
/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function show($post) {
$getpost = Post::find($post);
$category = CategoryPost::all();
return view('layouts.post.show')->with(compact('getpost', 'category'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $post) {
$getpost = Post::find($post);
$getpost->title = $request->title;
$getpost->short_desc = $request->short_desc;
$getpost->desc = $request->desc;
$getpost->image = $request->image;
$getpost->post_category_id = $request->post_category_id;
$getpost->save();
return redirect('api/v1/post');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy($post) {
$getpost = Post::find($post);
$getpost->delete();
return redirect('api/v1/post');
}
}
C:\xampp\htdocs\api\resources\views\layouts\post\create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<form autocomplete="off" method="POST" action="{{ route('post.store') }}">
@csrf
@method('POST')
<div class="form-group">
<label for="title">Tiêu đề</label>
<input type="text" name="title" class="form-control" id="title">
<label for="short_desc">Short Descript</label>
<input type="text" name="short_desc" class="form-control" id="short_desc">
<label for="desc">Descript</label>
<input type="text" name="desc" class="form-control" id="desc">
<label for="image">Image</label>
<input type="text" name="image" class="form-control" id="image">
<label for="post_category_id">Post Category Id</label>
<input type="text" name="post_category_id" class="form-control" id="post_category_id">
<label for="views">Views</label>
<input type="text" name="views" class="form-control" id="views">
<button type="submit" class="btn btn-primary mt-2" name="themdanhmuc">Add</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection