lundi 17 août 2020

Unable to POST to PHP backend server | "exception":"[object] (BadMethodCallException(code: 0): Method App

Not sure what am i missing here..I'm trying to POST some data from my App to a PHP based backend system(Laravel framework). Here's the details of my attempt & failure :

ERROR from Storage/Logs when my app attempts to POST API call is made :

[2020-08-17 07:58:43] development.ERROR: Method App\Http\Controllers\API\ProductOrderAPIController::store does not exist. {"userId":74,"exception":"[object] (BadMethodCallException(code: 0): Method App\\Http\\Controllers\\API\\ProductOrderAPIController::store does not exist. at /home2/vegans/public_html/shopcontrol/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:68)
[stacktrace]

Table name :

product_orders

From api.php :

Route::resource('product_orders', 'API\ProductOrderAPIController');

My ProductOrderAPIController.php :

<?php

namespace App\Http\Controllers\API;


use App\Models\ProductOrder;
use App\Repositories\ProductOrderRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Support\Facades\Response;
use Prettus\Repository\Exceptions\RepositoryException;
use Flash;

/**
 * Class ProductOrderController
 * @package App\Http\Controllers\API
 */

class ProductOrderAPIController extends Controller
{
    /** @var  ProductOrderRepository */
    private $productOrderRepository;

    public function __construct(ProductOrderRepository $productOrderRepo)
    {
        $this->productOrderRepository = $productOrderRepo;
    }

    public function index(Request $request)
    {
        try{
            $this->productOrderRepository->pushCriteria(new RequestCriteria($request));
            $this->productOrderRepository->pushCriteria(new LimitOffsetCriteria($request));
        } catch (RepositoryException $e) {
            Flash::error($e->getMessage());
        }
        $productOrders = $this->productOrderRepository->all();

        return $this->sendResponse($productOrders->toArray(), 'Product Orders retrieved successfully');
    }

    public function show($id)
    {
        /** @var ProductOrder $productOrder */
        if (!empty($this->productOrderRepository)) {
            $productOrder = $this->productOrderRepository->findWithoutFail($id);
        }

        if (empty($productOrder)) {
            return $this->sendError('Product Order not found');
        }

        return $this->sendResponse($productOrder->toArray(), 'Product Order retrieved successfully');
    }
}

My apps/Models/ProductOrder :

application/x-httpd-php ProductOrder.php ( C++ source, ASCII text )
<?php

namespace App\Models;

use Eloquent as Model;

/**
 * Class ProductOrder
 * @package App\Models
 * @version August 31, 2019, 11:18 am UTC
 *
 * @property \App\Models\Product product
 * @property \Illuminate\Database\Eloquent\Collection option
 * @property \App\Models\Order order
 * @property double price
 * @property integer quantity
 * @property integer product_id
 * @property integer order_id
 */
class ProductOrder extends Model
{

    public $table = 'product_orders';
    


    public $fillable = [
        'price',
        'quantity',
        'product_id',
        'order_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'price' => 'double',
        'quantity' => 'integer',
        'product_id' => 'integer',
        'order_id' => 'integer'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'price' => 'required',
        'product_id' => 'required|exists:products,id',
        'order_id' => 'required|exists:orders,id'
    ];

    /**
     * New Attributes
     *
     * @var array
     */
    protected $appends = [
        'custom_fields',
        'options'
    ];

    public function customFieldsValues()
    {
        return $this->morphMany('App\Models\CustomFieldValue', 'customizable');
    }

    public function getCustomFieldsAttribute()
    {
        $hasCustomField = in_array(static::class,setting('custom_field_models',[]));
        if (!$hasCustomField){
            return [];
        }
        $array = $this->customFieldsValues()
            ->join('custom_fields','custom_fields.id','=','custom_field_values.custom_field_id')
            ->where('custom_fields.in_table','=',true)
            ->get()->toArray();

        return convertToAssoc($array,'name');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     **/
    public function product()
    {
        return $this->belongsTo(\App\Models\Product::class, 'product_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function options()
    {
        return $this->belongsToMany(\App\Models\Option::class, 'product_order_options');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     **/
    public function order()
    {
        return $this->belongsTo(\App\Models\Order::class, 'order_id', 'id');
    }
        /**
    * @return \Illuminate\Database\Eloquent\Collection
    */
    public function getOptionsAttribute()
    {
        return $this->options()->get(['options.id', 'options.name']);
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire