*建立controller(含CRUD方法)
*建立model(含migration)
*設定migration
*執行migrate
*設定route
*查route設定
*設定model
*設計controller: index()
*設計view: 列出資料
*用seeder建立假資料
*新增資料:form
*新增資料
*編輯資料
*刪除資料
*首頁設定
----------------
其他:
*查 make:controller 的使用指南
*VS code 的 Emmet 用法
===============================
===============================
*建立controller(含CRUD方法)
php artisan make:controller ArticleController -r
*建立model(含migration)
php artisan make:model Article -m
===============================
*設定migration:
database\migrations\2019_01_05_033215_create_articles_table.php
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title',30);
$table->text('content');
$table->timestamps();
});
}
*執行migrate:
php artisan migrate
===============================
*設定route:
Route::resource('article','ArticleController');
*查route設定:
php artisan route:list
+--------+-----------+------------------------+------------------+------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------+--------------+
| | POST | article | article.store | App\Http\Controllers\ArticleController@store | web |
| | GET|HEAD | article | article.index | App\Http\Controllers\ArticleController@index | web |
| | GET|HEAD | article/create | article.create | App\Http\Controllers\ArticleController@create | web |
| | PUT|PATCH | article/{article} | article.update | App\Http\Controllers\ArticleController@update | web |
| | GET|HEAD | article/{article} | article.show | App\Http\Controllers\ArticleController@show | web |
| | DELETE | article/{article} | article.destroy | App\Http\Controllers\ArticleController@destroy | web |
| | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController@edit | web |
+--------+-----------+------------------------+------------------+------------------------------------------------+--------------+
===============================
*設定model:
app\Article.php
class Article extends Model
{
protected $table='articles';
protected $primaryKey='id';
protected $fillable=['title','content'];
}
===============================
*設計controller: index()
app\Http\Controllers\ArticleController.php
use App\Article;
public function index()
{
$query=Article::all();
return view('article.index',compact('query'));
}
===============================
*設計view: 列出資料
resources\views\article\index.blade.php
-----------------------
*Emmet:
section.container
<section class="container"></section>
-----------------------
@extends('layouts.app')
@section('content')
<section class="container">
<table class="table table-hover">
@foreach($query as $var)
<tr>
<td>{{ $var->id }}</td>
<td>{{ $var->title }}</td>
<td><a href="{{ url('article/'.$var->id.'/edit') }}" role="btn" class="btn btn-warning">編輯</a></td>
<td><a href="{{ url('article/'.$var->id.'/delete') }}" role="btn" class="btn btn-danger">刪除</a></td>
</tr>
@endforeach
</table>
</section>
@endsection
===============================
*用seeder建立假資料:
database\seeds\ArticleTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Article;
class ArticleTableSeeder extends Seeder
{
public function run()
{
DB::table('articles')->truncate();
for ($i=0; $i < 10; $i++) {
Article::create([
'title'=>str_random(10),
'content'=>str_random(255)
]);
}
}
}
-----------------------
database\seeds\DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call('ArticleTableSeeder');
}
}
-----------------------
*執行seeder:
手動建立seeder檔案要先執行composer dump-autoload:
composer dump-autoload
php artisan db:seed
------------
*自動建立seeder檔案:
php artisan make:seeder 名稱
===============================
*新增資料:form:
*controller:
app\Http\Controllers\ArticleController.php
/**新增資料:form
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('article.create');
}
*view:
1.
resources\views\article\create.blade.php
@extends('layouts.app')
@section('content')
<section class="container">
<h5>新增文章</h5>
<form action="{{ url('article') }}" method="post">
@csrf
<div class="form-group">
<label for="text1">主題:</label> <b class="text-danger">{{ $errors->first('title') }}</b>
<input type="text" name="title" class="form-control" value="{{ old('title') }}" id="text1">
</div>
<div class="form-group">
<label for="text2">內容:</label> <b class="text-danger">{{ $errors->first('content') }}</b>
<textarea name="content" cols="30" rows="10" class="form-control" id="text2">{{ old('content') }}</textarea>
</div>
<input type="submit" value="送出" class="btn btn-primary">
</form>
</section>
@endsection
2.
resources\views\article\index.blade.php
加入新增按鈕:
<a href="{{ url('article/create') }}" role="btn" class="btn btn-primary float-right">新增</a>
位置如下:
@extends('layouts.app')
@section('content')
<section class="container">
<a href="{{ url('article/create') }}" role="btn" class="btn btn-primary float-right">新增</a>
<table class="table table-hover">
@foreach($query as $var)
<tr>
<td>{{ $var->id }}</td>
<td>{{ $var->title }}</td>
<td><a href="{{ url('article/'.$var->id.'/edit') }}" role="btn" class="btn btn-warning">編輯</a></td>
<td><a href="{{ url('article/'.$var->id.'/delete') }}" role="btn" class="btn btn-danger">刪除</a></td>
</tr>
@endforeach
</table>
</section>
@endsection
===============================
*新增資料:
/**新增資料(資料未驗證)
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Article::create($request->all());
return redirect('article');
}
------------------
*表單資料驗證:
建立一個「表單請求( form request )」。
表單請求是一個自定的請求類別,裡面包含驗證的邏輯。
php artisan make:request ArticleRequest
產生檔案:
app\Http\Requests\ArticleRequest.php
設定驗證規則:
app\Http\Requests\ArticleRequest.php
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required',
'content' => 'required|min:3'
];
}
public function messages()
{
return [
'required' => '不可為空白',
'content.min' => '至少填寫3個字'
];
}
------------------
*新增資料(含資料驗證):
app\Http\Controllers\ArticleController.php
use App\Http\Requests\ArticleRequest;
/**新增資料(含資料驗證)
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ArticleRequest $request)
{
Article::create($request->all());
return redirect('article');
}
===============================
*編輯資料:
*view:
resources\views\article\edit.blade.php
@extends('layouts.app')
@section('content')
<section class="container">
<h5>編輯文章</h5>
<form action="{{ url('article/'.$query->id) }}" method="post">
@csrf
@method('PUT')
<div class="form-group">
<label for="text1">主題:</label> <b class="text-danger">{{ $errors->first('title') }}</b>
<input type="text" name="title" class="form-control" value="{{(!$errors->has('title') and old('title')==null)?$query->title:old('title')}}" id="text1">
</div>
<div class="form-group">
<label for="text2">內容:</label> <b class="text-danger">{{ $errors->first('content') }}</b>
<textarea name="content" cols="30" rows="10" class="form-control" id="text2">{{(!$errors->has('content') and old('content')==null)?$query->content:old('content')}}</textarea>
</div>
<input type="submit" value="送出" class="btn btn-primary">
</form>
</section>
@endsection
---------------
*controller:
app\Http\Controllers\ArticleController.php
use App\Article;
use App\Http\Requests\ArticleRequest;
/**編輯資料:form
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$query=Article::find($id);
return view('article.edit',compact('query'));
}
/**編輯資料
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ArticleRequest $request, $id)
{
//方法1
// Article::where('id',$id)->update([
// 'title' => $request->title,
// 'content' => $request->content
// ]);
//方法2
$article = Article::findOrFail($id);
$article->update($request->all());
return redirect('article');
}
===============================
*刪除資料:
*view:
resources\views\article\index.blade.php
@extends('layouts.app')
@section('content')
<section class="container">
<a href="{{ url('article/create') }}" role="btn" class="btn btn-primary float-right">新增</a>
<table class="table table-hover">
@foreach($query as $var)
<tr>
<td>{{ $var->id }}</td>
<td>{{ $var->title }}</td>
<td><a href="{{ url('article/'.$var->id.'/edit') }}" role="btn" class="btn btn-warning">編輯</a></td>
<td>
<form action="{{ url('article/'.$var->id) }}" method="post">
@csrf
@method("DELETE")
<input type="submit" role="btn" class="btn btn-danger" value="刪除" >
</form>
</td>
</tr>
@endforeach
</table>
</section>
@endsection
-----------------
*controller:
app\Http\Controllers\ArticleController.php
/**刪除資料
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Article::destroy($id);
return redirect('article');
}
===============================
*首頁設定:
routes\web.php
Route::get('/','ArticleController@index');
===============================
===============================
*查 make:controller 的使用指南:
每一個指令都包含一個「使用指南」,
它顯示並描述這個指令能夠接受哪些參數和選項
php artisan help make:controller
===============================
*VS code 的 Emmet 用法:
html:5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>
--------------------
div.content>ul>li
<div class="content">
<ul>
<li></li>
</ul>
</div>
沒有留言:
張貼留言