添加了 工厂模型 但是一次只能创建一家 添加了 api控制器 用于统一api返回结果

This commit is contained in:
lingling 2022-07-08 16:21:20 +08:00
parent b78e3fb983
commit 4530a92837
6 changed files with 75 additions and 5 deletions

View File

@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
protected function create($msg = '', $code = 200, $data = [])
{
$result = [
// 返回状态码
'code'=>$code,
// 返回提示信息
'msg'=>$msg,
// 返回数据
'data'=>$data,
];
return response($result);
}
}

View File

@ -4,7 +4,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UController extends Controller
class UController extends ApiController
{
/**
* Display a listing of the resource.
@ -66,4 +66,10 @@ class UController extends Controller
//
}
public function text(Request $request)
{
//
//return json_encode($request);
return $this->create('请求成功',200, $data = $request->all());
}
}

11
app/Models/Flight.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
use HasFactory;
}

View File

@ -5,6 +5,11 @@ namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
class DatabaseSeeder extends Seeder
{
/**
@ -16,9 +21,15 @@ class DatabaseSeeder extends Seeder
{
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
\App\Models\User::factory()->create([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password')
]);
// $user=new User;
// $user->name=Str::random(10);
// $user->email=Str::random(10).'@gmail.com';
// $user->password=Hash::make('password');
// $user->save();
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
User::factory(50)->create();
}
}

View File

@ -26,3 +26,4 @@ use App\Http\Controllers\UController;
// });
Route::get('/link/{id}', [UController::class, 'index']);
Route::post('/link/{url}', [UController::class, 'add']);
Route::post('/text', [UController::class, 'text']);