laravel实现一个最最最最基础的api
前言
需要将公司内部的广告数据维护在后端,业务非常简单仅做些数据的增删改查,所以不太好意思麻烦后端兄弟。而自己laravel接触了这么久一直没有自己好好写过,这不趁着这个机会边做边学,也能最终落地到业务上去,然后很多理解都是搬代码抄代码来的,理解对不上的地方谅解下我这个菜鸟野路子🌚
业务需求
广告的基本数据(title、description等)同时关联App信息(name、包名等)、图片信息、视频信息
实现
1、首先是数据库设计(database/migrations),通过命令生成model以及数据库迁移文件
php artisan make:model App --migration
总共生成了4张表以及model(feed_ads、apps、images、videos)
添加字段
feed_ads
Schema::create('feed_ads', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('title');
$table->string('description');
$table->string('ad_type');
$table->string('media_type');
$table->unsignedInteger('app_id')->nullable()->index()->comment('App信息');
$table->unsignedInteger('image_id')->nullable()->index()->comment('主配图');
$table->unsignedInteger('video_id')->nullable()->index()->comment('视频');
});
apps
Schema::create('apps', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('applicationId');
$table->string('name');
$table->string('downloadUrl');
$table->string('scheme');
$table->string('chineseName');
$table->string('icon');
$table->string('downloadType');
$table->float('size');
});
Images
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->integer('width');
$table->integer('height');
});
videos
Schema::create('videos', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->string('cover');
$table->integer('width');
$table->integer('height');
});
执行迁移重建表结构
php artisan migrate
2、model
Laravel 包含了 Eloquent,这是一个对象关系映射器(ORM),使与数据库的交互变得很愉快。使用 Eloquent 时,每个数据库表都有一个对应的「模型」,用于与该表进行交互。除了从数据库表中检索记录外,Eloquent 模型还允许您从表中插入,更新和删除记录。
所以不需要额外再写sql语句了,此处仅增加了app、image、video的关联关系,通过外键id关联
class FeedAd extends Model
{
use HasFactory;
protected $fillable = [
'title',
'description',
'ad_type',
'media_type',
'app_id',
'image_id',
'video_id',
];
public function app():BelongsTo
{
return $this->belongsTo(App::class, 'app_id');
}
public function image():BelongsTo
{
return $this->belongsTo(Image::class, 'image_id');
}
public function video():BelongsTo
{
return $this->belongsTo(Video::class, 'video_id');
}
}
2、controllers,这里就是处理所有与用户相关的传入请求,业务中需要随机获取feedAd,我们接收传入的随机获取count,通过随机函数随机获取数据库中一定数量的feedAd,同时遍历加上关联关系返回
public function randomFeedAd(Request $request)
{
$count=$request->count ?? 1;
$ads= FeedAd::inRandomOrder()->take($count)->get();
foreach ($ads as $ad) {
$ad->app=$ad->app;
$ad->image=$ad->image;
$ad->video=$ad->video;
}
return $ads;
}
3、api
定义指向FeedAd控制器行为的路由,当请求http://127.0.0.1:8000/api/random_feed_ad 时会执行randomFeedAd方法将结果返回。
Route::post('/random_feed_ad', [FeedAdController::class, 'randomFeedAd']);
4、数据
虽然完成了上面的步骤,因为没有数据,最后查出来的会是空数据,所以就得填充数据了。一是使用laravel的seeder填充数据,二是再写一个创建FeedAd接口填充数据
生成Seeder文件
php artisan make:seeder FeeAdSeeder
public function run()
{
DB::table('feed_ads')->insert([
'title' => '答题赚钱',
'description' => '强烈建议:想赚钱就来答题赚钱,每日提现不停,秒到账',
'app_id'=>1,
'image_id'=>1,
'video_id'=>1,
'media_type'=>'video',
'ad_type'=>'app',
]);
}
执行seeder填充数据
php artisan db:seed --class=FeedAdSeeder
其他表的数据同理
创建FeedAd
contoller
public function create(Request $request)
{
$feed_ad = new FeedAd($request->all());
$feed_ad->save();
return $feed_ad;
}
Route::post('/create_feed_ad', [FeedAdController::class, 'create']);
至此一个最基本的查询和创建api就完成了,在框架的加持下还是很简单的,一个新手理解了基础的modal、controller、database原理以及之间的关系差不多就能写基础的api了💪
这个人暂时没有 freestyle