//檔案:app/http/controllers/postscontroller.php
    public function store(Request $request){

        $this->validate($request,[
            'title'=>'required|max:4'
        ]);
    }
<!-- 檔案:resources/views/posts/create.blade.php -->
@extends('layouts.app')
@section('content')

<h1>發表文章</h1>
{!! Form::open(['method'=>'POST','action'=>'PostsController@store']) !!}
	{{csrf_field()}}
	<div class="form-group">
		{!! Form::label('title','標題:')!!}
		{!! Form::text('title',null,['class'=>'form-control'])!!}
	</div>
	{!! Form::submit('發文',['class'=>'btn btn-primary'])!!}
{!! Form::close()!!}

@if(count($errors)>0)
	<div class="alert alert-danger">
		<ul>
			@foreach ($errors->all() as $error)
				<li>{{$error}}</li>
			@endforeach
		</ul>
	</div>
@endif

@endsection

若打超過4個字,會跳出錯誤訊息

//下指令
php artisan make:request CreatePostRequest
<?php
//檔案:app/http/requests/CreatePostRequest.php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreatePostRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'title'=>'required'
        ];
    }
}
<?php
//檔案:app/http/controllers/postscontroller.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\CreatePostRequest;
use App\Post;

class PostsController extends Controller
{
    public function index(){
        $posts=Post::all();
        return view('posts.index',compact('posts'));
    }
    public function create(){
    	return view('posts.create');
    }
    public function store(CreatePostRequest $request){
        Post::create($request->all());
        return redirect('/posts');
    }
//檔案:routes/web.php     //auth
Route::group(['middleware'=>'web'],function(){

	Route::resource('/posts','PostsController');

});

若沒填,會出現錯誤訊息

//檔案:routes/web.php     
Route::group(['middleware'=>'web'],function(){

	Route::resource('/posts','PostsController');

	Route::get('/dates', function() {
	    $date = new DateTime('+1 week');
	    echo $date->format('m-d-Y');
	});

});

瀏覽器輸入:dates

出現下禮拜的今天的日期

註:使用前輸入use Carbon\Carbon;

//下指令
composer search carbon

呈現

nesbot/carbon A simple API extension for DateTime.
nesbot/carbon A simple API extension for DateTime.
jenssegers/date A date library to help you work with dates in different languages
htmlburger/carbon-fields WordPress developer-friendly custom fields for post types, taxonomy terms, users, comments, widgets, options and more.
laravelrus/localized-carbon A localizable version of Carbon
citco/carbon This is a wrapper for nesbot/carbon which also calculates which days are British bank holidays (England & Wales only).
lightsuner/carbon-bundle Provide Carbon datetime lib. as annotations param. converter (SensioFrameworkExtraBundle)
advmaker/carbon-period Extension of nesbot/carbon plugin, to work with date period
tyxla/carbon-breadcrumbs A basic WordPress plugin for breadcrumbs with advanced capabilities for extending.
rovangju/carbon-nbd Carbon DateTime extension to calculate the "next business day"
htmlburger/carbon-pagination A handy WordPress library for building all kinds of paginations.
2createstudio/carbon-pagination A handy WordPress library for building all kinds of paginations.
htmlburger/carbon-fragment A tiny tool that allows passing variables to theme fragment files.
htmlburger/carbon-debug Debugging functions, wrapping around Symfony's VarDumper.
htmlburger/carbon-common A collection of common utility functions, useful for building WordPress themes and plugins.
qwildz/localized-eloquent-date Multi-language support for Laravel Eloquent's dates

	//檔案:routes/web.php
	Route::get('/dates', function() {
	    echo Carbon::now()->addDays(10)->diffForHumans();
	});

瀏覽器輸入:dates

出現今天往後加十天,大概要幾周

	//檔案:routes/web.php
	Route::get('/dates', function() {
	    echo Carbon::now()->addDays(10)->diffForHumans() , '<br>';
	    echo Carbon::now()->subMonths(10)->diffForHumans();
	});

瀏覽器輸入:dates

出現今天往前加十月,大概在幾月前

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Jerry 的頭像
    Jerry

    Bug倉庫 // 程式日記

    Jerry 發表在 痞客邦 留言(0) 人氣()