Skip to content

Commit 70f7dde

Browse files
authored
增加範例檔案 (#3)
* 增加作為範例的類別模板 * Edit ServiceProvider.php: 設置 publisheds 列表
1 parent c87e1f2 commit 70f7dde

File tree

8 files changed

+179
-2
lines changed

8 files changed

+179
-2
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
| Stub Storage Paths
88
|--------------------------------------------------------------------------
99
|
10-
| 暫無說明
10+
| 這邊妳可以指定 laravel-stubs 要讀取哪些路徑。
11+
|
12+
| Here you may specify an array of paths that should be read for your stubs.
1113
|
1214
*/
1315

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Dummy;
6+
use App\Http\Resources\Dummy as DummyResource;
7+
use Illuminate\Http\Request;
8+
9+
class DummyController extends Controller
10+
{
11+
/**
12+
* @param \Illuminate\Http\Request $request
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function __invoke(Request $request)
16+
{
17+
$query = Dummy::query();
18+
19+
$query->orderByPriority();
20+
21+
$records = $query->paginate($request->query('limit', 30));
22+
23+
return DummyResource::collection($records);
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Dummy;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class DummyFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = Dummy::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
// ...
26+
];
27+
}
28+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
8+
class Dummy extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'priority',
19+
'is_published',
20+
// ...
21+
];
22+
23+
/**
24+
* The attributes that should be cast.
25+
*
26+
* @var array
27+
*/
28+
protected $casts = [
29+
'is_published' => 'boolean',
30+
// ...
31+
];
32+
33+
/* =========================================================================
34+
* = Scopes
35+
* =========================================================================
36+
*/
37+
38+
/**
39+
* 按照優先度排序
40+
*
41+
* ```
42+
* $query->orderByPriority();
43+
* ```
44+
*
45+
* @param \Illuminate\Database\Eloquent\Builder $query
46+
* @param boolean $reversed
47+
* @return void
48+
*/
49+
public function scopeOrderByPriority($query, $reversed = false)
50+
{
51+
$query->orderBy('priority', $reversed ? 'asc' : 'desc');
52+
}
53+
54+
/**
55+
* 查詢僅上架
56+
*
57+
* ```php
58+
* $query->published();
59+
* ```
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $query
62+
* @param boolean $value
63+
* @return void
64+
*/
65+
public function scopePublished($query, $value = true)
66+
{
67+
$query->where('is_published', $value);
68+
}
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use App\Models\Dummy;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class DummyResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
15+
*/
16+
public function toArray($request)
17+
{
18+
return [
19+
// ...
20+
];
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Dummy;
6+
use Illuminate\Database\Seeder;
7+
8+
class DummiesTableSeeder extends Seeder
9+
{
10+
/**
11+
* Run the database seeds.
12+
*
13+
* @return void
14+
*/
15+
public function run()
16+
{
17+
Dummy::factory(10)->create();
18+
}
19+
}

publishes/resources/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Stubs
2+
3+
此目錄用來放置 `stub` 檔案。
4+
5+
`artisan make:a...` 將會讀取當前目錄來產生類別,並將檔案依照命名空間自動放置到相對位置。
6+
7+
This directory contains your stub files.
8+
9+
More information about the usage of this directory in [here](https://github.com/A2Workspace/laravel-stubs#usage--%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8).

src/ServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class ServiceProvider extends IlluminateServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
// ...
16+
$this->publishes([
17+
__DIR__ . '/../publishes/config.php' => config_path('stubs.php'),
18+
__DIR__ . '/../publishes/resources' => resource_path('stubs'),
19+
], '@a2workspace/laravel-stubs');
1720
}
1821

1922
/**

0 commit comments

Comments
 (0)