我不想要json响应。我想为此输出使用api资源。谁能帮我这个忙。帮助将得到高度重视。 这是新闻模型
<?php
namespace Modules\Newsletter\Entities;
use Brexis\LaravelWorkflow\Traits\WorkflowTrait;
//use Hyn\Tenancy\Abstracts\TenantModel as TenancyModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class News extends Model {
use WorkflowTrait;
protected $table = 'news_info';
protected $fillable = [
'title', 'header', 'description', 'status', 'created_by', 'media_url', 'media_thumbnail',
];
public function reviews() {
return $this->morphMany(NewsReview::class, 'reviewable');
}
public function reviewsCountByCategory() {
return $this->morphMany(NewsReview::class, 'reviewable')
->select(
'reviewable_id',
DB::raw("COUNT(CASE WHEN review_reaction=1 THEN 1 ELSE NULL END) as review_bad"),
DB::raw("COUNT(CASE WHEN review_reaction=2 THEN 1 ELSE NULL END) as review_average"),
DB::raw("COUNT(CASE WHEN review_reaction=3 THEN 1 ELSE NULL END) as review_good")
)
->groupBy('reviewable_id');
}
}
这是评论模型
<?php
namespace Modules\Newsletter\Entities;
use App\User;
//use Hyn\Tenancy\Abstracts\TenantModel as TenancyModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class NewsReview extends Model {
use SoftDeletes;
protected $fillable = [
'review_text',
'review_reaction',
'is_visible',
'reviewed_by',
'reviewable_id',
'reviewable_type'
];
public function reviewable() {
return $this->morphTo();
}
public function news(){
return $this->belongsTo(News::class);
}
public function reviewer() {
return $this->hasOne(User::class, 'id', 'reviewed_by');
}
}
这是我在Controller内部的功能
public function searchNews(Request $request,$title) {
try {
$result=News::with('reviewsCountByCategory')->where('status', 'pre_validation')
->where('title', 'LIKE',"%$title%")
->orderBy('title', 'asc')->paginate(3);
return response()->json(['status' => TRUE, 'data' => $result], 200);
} catch (\Exception $e) {
return response()->json(['status' => FALSE, 'msg' => 'Internal Server Error','error' => $e->getMessage()], 500);
}
}
这是我现在得到的最新回应
{
"status": true,
"data": {
"current_page": 1,
"data": [
{
"id": 8,
"title": "AndraPradesh CM Tested Positive",
"header": "Todays Headline",
"description": "Corona Virus spreds Globaly",
"status": "pre_validation",
"created_by": 2,
"media_type": null,
"media_url": "www.abc.com",
"media_thumbnail": null,
"is_send_with_newsletter": 0,
"created_at": "2020-07-24 11:28:34",
"updated_at": "2020-07-24 11:28:34",
"deleted_at": null,
"reviews_count_by_category": []
},
{
"id": 2,
"title": "Anurag singh",
"header": "Todayfdfdf",
"description": "Hoell",
"status": "pre_validation",
"created_by": 1,
"media_type": null,
"media_url": "www.abc.com",
"media_thumbnail": null,
"is_send_with_newsletter": 0,
"created_at": "2020-07-22 11:13:50",
"updated_at": "2020-07-22 11:13:50",
"deleted_at": null,
"reviews_count_by_category": [
{
"reviewable_id": 2,
"review_bad": 0,
"review_average": 1,
"review_good": 0
}
]
},
{
"id": 9,
"title": "Chandigarh is famous for its natural Beauty",
"header": "Todays Headline",
"description": "Corona Virus spreds Globaly",
"status": "pre_validation",
"created_by": 2,
"media_type": null,
"media_url": "www.abc.com",
"media_thumbnail": null,
"is_send_with_newsletter": 0,
"created_at": "2020-07-24 11:29:18",
"updated_at": "2020-07-24 11:29:18",
"deleted_at": null,
"reviews_count_by_category": []
}
],
这是我的评论资源
<?php
namespace Modules\Newsletter\Transformers;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\Resource;
class ReviewResource extends Resource {
/**
* @param Request
* @return array
*/
public function toArray($request) {
return [
'review_id' => $this->id,
'review_text' => $this->review_text,
'review_reaction' => new NewsResource($this->reviewsCountByCategory),
'is_visible' => $this->is_visible,
'reviewed_by' => new UserResource($this->reviewer),
'news' => new NewsResource($this->reviews),
];
}
}
这是我的NewsResource
<?php
namespace Modules\Newsletter\Transformers;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\Resource;
class NewsResource extends Resource {
/**
* @param Request
* @return array
*/
public function toArray($request) {
return [
'news_id' => $this->id,
'title' => $this->title,
'header' => $this->header,
'description' => $this->description,
'status' => $this->status,
'media_url' => $this->media_url,
'media_thumbnail' => $this->media_thumbnail,
];
}
}
但是我不想要json响应。我想为此输出使用api资源。谁能帮我这个忙。帮助将得到高度重视。
答案 0 :(得分:0)
在控制器的功能内部,而不是此
return response()->json(['status' => TRUE, 'data' => $result], 200);
NewsResource.php
public function toArray($request) {
$reviews = [];
foreach($this->reviews as $review){
array_push($reviews, [
'review_id' => $review->id,
'review_text' => $review->review_text,
'is_visible' => $review->is_visible,
]);
}
return [
'news_id' => $this->id,
'title' => $this->title,
'header' => $this->header,
'description' => $this->description,
'status' => $this->status,
'media_url' => $this->media_url,
'media_thumbnail' => $this->media_thumbnail,
'reviews' => $reviews,
];
}
使用NewsResource
return NewsResource::collection($result);