2
{
"data": {
   "username": "candy",
   "certificates": [
   {
   "id": 11,
   "category_id": 1,
   "certname": "cert name test",
   "created_at": "2018-08-18 00:58:12",
   "updated_at": "2018-08-18 00:58:12"
   }
   ]
 }
}

Above is a response by using Eloquent: API Resources.

I would like to put category name instead of category_id.

Below is resource class

    public function toArray($request)
{
    return [
        'nickname' => $this->nickname,
        'certificates' => $this->certificates,
    ];
}

certificates are in array (hasMany relationship)

certificates belongsTo category

6
  • Not really sure what you’re asking here... what are you trying to do? Commented Sep 25, 2018 at 11:33
  • change like "category_id": 1 to "category": engineering Commented Sep 25, 2018 at 11:48
  • i want to show string instead of integer. Commented Sep 25, 2018 at 11:48
  • i can change by using Accessor but want to know other ways. Commented Sep 25, 2018 at 11:51
  • Use laravel.com/docs/5.7/eloquent-resources or when you’re in the controller, use a map() on the collection to rebuild the array laravel.com/docs/5.7/collections#method-map Commented Sep 25, 2018 at 11:58

3 Answers 3

2

In this case, in the resource class you can fetch category name for each certificate and attach it.

public function toArray($request)
{

    $certificateList = $this->certificates;

    foreach($certificateList as $ceritificate) {

         // fetch only the category name from the database.
         // attach to the certificate.

         $categoryName = Category::where('id', $certificate->category_id)->pluck('name')->first();
         $certificate->setAttribute('category_name, $categoryName);
    }

    // then as usual
    return [
         'nick_name' => $this->nick_name,
         'certificates' => $certificateList
    ];
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I slightly modified your code and solved my issue.
0

This Might Help, Run a foreach loop

$category = "Your SELECT Query";
foreach ($category as $value) {
               $categoryId = json_decode($value->category_id);
               $x = Category::select('certname')->whereIn('id', $categoryId)->get();
               foreach ($x as $as) {
                   $y[] = $as->certname;
               }
               $value->certname = $y;

Comments

0

you can just do it like this

in controler

 return    CertificateResource::collection(Certificate::with('category')->get());

in CertificateResource

 return [
    'nickname' => $this->nickname,
    'certificates' => $this->certificates,
    'category_name'=>$this->whenLoaded('category', function () {
            return $this->category->category_name;
        }),
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.