-1

I want to make Restful API with Laravel and I want to write a script that is going through the CSV file, and firstly POST Animal, then get Animal ID from response, and POST AnimalDate, but It's not working as I want and I'm getting this following error:

Call to undefined method App\Animal::getAnimalDateAttribute()

I have models like Animal and AnimalDate and I want to show response in json like bellow so I used Eloquent Resources with JsonResource:

{
  "id": 1,
  "name": "Gilda Lynch",
  "country": "MO",
  "description": "Soluta maiores aut dicta repellat voluptas minima vel. Qui omnis assumenda maxime.",
  "image": "http://www.abshire.com/",
  "dates": [
    {
      "id": 6,
      "date_from": "2019-11-25 04:03:44",
      "date_to": "2019-09-30 05:47:28",
      "animal_id": 1,
    },
  ]
}

I think that problem is in a relationship between Animal model and AnimalDate model, but I can't fix it so I'm looking for help.

Relationship between these models: Animal hasMany AnimalDate

class Animal extends Model
{

   public function animalDates()
   {
      return $this->hasMany(AnimalDate::class);
   }
}

and AnimalDate belongsTo Animal

class AnimalDate extends Model
{
    public function animal()
    {
        return $this->belongsTo(Animal::class);
    }
}

I created Resources - AnimalDateResource.php

class AnimalDateResource extends JsonResource
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

and AnimalResource:

class AnimalResource extends JsonResource
{
    public function toArray($request)
    {
        // return parent::toArray($request);
        return [
            'id' => $this->id,
            'name' => $this->name,
            'country' => $this->country,
            'description' => $this->description,
            'image' => $this->image_url,
            'dates' => AnimalDateResource::collection($this->animalDates)
        ];
    }
}

In controller I'm just using new AnimalResource($animal) and mehods index and show works perfect.

Is there any solution to POST it like Animal and then AnimalDate, or do I have to POST it first and than show relationship via JsonResouce?

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
VladoS24
  • 47
  • 8
  • the error that somewhere in your code you access ->animaleDate withouth plural, but no code here shows that, can you do a lookup in your project? – mrhn Aug 07 '19 at 08:49
  • Not sure if related, but you have both animalDate and animalDates (pluralised) – user6854465 Aug 07 '19 at 08:51
  • you need to make an variable that can hold the array e.g. **newAnimalDates** the make a foreach loop and push each AnimalDates to your newAnimalDates array. Then you can call `'dates' => newAnimalDates` – Japs Aug 07 '19 at 08:52
  • share full error with screenshots and share full model class of `Animal` – Sohel0415 Aug 07 '19 at 08:53

2 Answers2

0

You can access the relationship directly to you resource if it is One to One. But I dont know if you can access the relationship if it is Many to One. This thing works for me.

    public function toArray($request)
    {
        $newAnimalDates = array();
        foreach($AnimalDates as $animalDates) {
          $newAnimalDates[] = [
            "id": $animalDates->id,
            "date_from": $animalDates->date_from,
            "date_to": $animalDates->date_to,
            "animal_id": $animalDates->animal_id,
          ]
        }  

        return [
            'id' => $this->id,
            'name' => $this->name,
            'country' => $this->country,
            'description' => $this->description,
            'image' => $this->image_url,
            'dates' => $newAnimalDates
        ];
    }
Japs
  • 977
  • 2
  • 10
  • 19
0

Ok so the problem was in AnimalController@store.

Before solution:

public function store(Request $request)
{
    $data = $request->all();
    $animal = Animal::create($data);

    return response()->json($animal, 201);
}

After problem fixing:

public function store(Request $request)
{
    $data = $request->all();
    $animal = Animal::create($data);

    // THIS LINE
    return new AnimalResource($animal);
}
VladoS24
  • 47
  • 8