samedi 2 mars 2019

Undefined model by using Join Query in Laravel

I am indexing apples with their specified properties (such as color) using API Laravel. I use join to retrieve apples which are related to a specified brand. but it does not retrieve apples with their own specified properties which are defined in another DB and models.

public function index(Brand $brand)
    {
        try {
            $apples = Apple::join('brands', 'brand_id', 'brands.id')->where('brand_id', $brand->id)->get();    
            if (Request::expectsJson()) {
                return returnSuccessfulResponse(
                    trans('api.response.successful.index'),
                    StructureResource::collection($apples)
                );
            } else {
                return view('welcome');
            }
        } catch (QueryException $exception) {
            return returnQueryException($exception);
        }
    }

Apple model:

public function brand()
    {
        return $this->belongsTo(Brand::class);
    }
public function appleProperties()
    {
        return $this->hasMany(AppleProperty::class);
    }

Resource:

return [
           'id' => $this->brand->id,
           'name' => $this->brand->name,
           'apple-properties' => $this->appleProperties,
       ];

Route:

Route::apiResource('brands/{brand}/apples', 'AppleController');

It is not retrieving appleProperties. I do not understand that reason!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire