Lavarel 快速學習自我挑戰 Day10


Model Manipulation

Dates

  1. 搜尋套件 composer search carbon
  2. 引用套件 use Carbon\Carbon;
  3. 新增 routes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Route::get('/dates', function(){

$date = new DateTime('+1 week');

echo $date->format('m-d-Y');

echo '<br>';

echo Carbon::now()->addDays(10)->diffForHumans();

echo '<br>';

echo Carbon::now()->subMonth(5)->diffForHumans();

echo '<br>';

echo Carbon::now()->yesterday()->diffForHumans();

});

Accessors (pull data out of database)

  1. 修改 model
1
2
3
4
5
6
public function getNameAttribute($value){

// return ucfirst($value); //第一個字大寫
return strtoupper($value); //全部變成大寫

}
  1. 修改 routes
1
2
3
4
5
6
7
Route::get('/getname', function(){

$user = User::find(1);

echo $user->name;

});

Mutators - Mutators 設定官方文件

  1. 修改 model
1
2
3
4
5
public function setNameAttribute($value){

$this->attributes['name'] = strtoupper($value);

}
  1. 修改 routes
1
2
3
4
5
6
7
8
9
Route::get('/setname', function(){

$user = User::find(1);

$user->name= "william";

$user->save();

});

Query Scopes - Query Scopes 設定官方文件

  1. 修改 PostsController
1
2
3
4
5
6
7
public function index()
{
$posts = Post::latest();

return view('posts.index', compact('posts'));
}

  1. 修改 Post model
1
2
3
4
5
public static function scopeLatest($query){

return $query->orderBy('id', 'asc')->get();

}

上傳檔案

新增 Views

  1. 修改第三個參數 'files'=>true 來新增 enctype ,並加入上傳的 input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{!! Form::open(['method'=>'POST', 'action'=>'PostsController@store', 'files'=>true]) !!}


<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class'=>'form-control']) !!}
</div>

{{csrf_field()}}

<div class="form-group">
{!! Form::file('file', ['class'=>'form-control']) !!}
</div>


<div class="form-group">
{!! Form::submit('Create Post', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}

取得上傳檔案資訊

  1. 修改 PostsController
1
2
3
4
5
6
7
8
9
10
11
12
public function store(CreatePostRequest $request)
{
$file = $request->file('file'); //取得檔案,會以暫存檔(temp)呈現

echo "<br>";

echo $file->getClientOriginalName(); //取得原始名稱

echo "<br>";

echo $file->getClientSize(); //取得檔案大小
}

將上傳的檔案寫入資料庫 (persist file data into database)

  1. 新增一個 migration 並關聯到 posts
    php artisan make:migration add_path_column_to_posts --table=posts
  2. 新增 migration 到資料庫
    php artisan migrate
  3. 讓 path 欄位可寫入
    protected $fillable = ['path'];
  4. 設定 PostsController 讓檔案可以寫入資料庫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public function store(CreatePostRequest $request)
{
$input = $request->all();

if($file = $request->file('file')){

$name = $file->getClientOriginalName();

$file->move('images', $name);

$input['path'] = $name;

}

Post::create($input);
}

顯示上傳的圖片

  1. 新增 image-container 到 index.blade.php
1
2
3
4
5
<div class="image-container">

<img height="100" src="{{$post->path}}" alt="">

</div>
  1. 新增目錄到 Post model
    public $directory = '/images/';
  2. 用 Accessors 新增圖片路徑
1
2
3
4
5
public function getPathAttribute($value){

return $this->directory . $value;

}
Share