Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

php - laravel BadMethodCall() error while caliing for index in api

the Ptag controller

    <?php

      namespace AppHttpControllers;

    use IlluminateHttpRequest;
    use Validator;
    use AppPtag;
    use AppHttpResourcesPtagResource;
    use IlluminateSupportFacadesAuth;
    use IlluminateHttpResourcesJsonJsonResource;
    use IlluminateValidationRule;


    class PtagController extends Controller
    {
        public function index(Request $request){
            $query=Ptag::query();
    return PtagResource::collection($query);
     }
    }

ptag resource

    <?php

    namespace AppHttpResources;

    use IlluminateHttpResourcesJsonJsonResource;

    class PtagResource extends JsonResource
    {
        /**
        * Transform the resource into an array.
         *
        * @param  IlluminateHttpRequest  $request
        * @return array
        */
       public function toArray($request)
       {
           return [
           'id'=>$this->id,
           'title'=>$this->title
        // 'product'=>$this->product()
            ];
        }
      }

ptag model

    <?php

    namespace App;

     use IlluminateDatabaseEloquentModel;

    class Ptag extends Model
    {
        protected $guarded=[];

         public function product(){
          return $this->belongsToMany('AppProduct');
     }
   }

error is shown as such

ErrorException: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in file /home/rajesh/project/handicom/vendor/laravel/framework/src/Illuminate/Http/Resources/DelegatesToResource.php on line 53


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem:


In DelegatesToResource.php file of vendor folder, we have the following code:

public function offsetExists($offset)
{
    return array_key_exists($offset, $this->resource);
}

https://github.com/laravel/framework/blob/5.7/src/Illuminate/Http/Resources/DelegatesToResource.php

array_key_exists function is deprecated on PHP 7.4.x. So, the error you met is related to PHP, not Laravel.

Solution:


Method 1: The easiest way is to downgrade your PHP version. Naturally, you still have to ensure the requirements of Laravel 5.7 (PHP >=7.1.3)

Method 2: You upgrade your project to higher version. To limit too many changes in your project, you could use version 5.8. In this version, the library no longer uses array_key_exists function.

public function offsetExists($offset)
{
    return isset($this->resource[$offset]);
}

https://github.com/laravel/framework/blob/5.8/src/Illuminate/Http/Resources/DelegatesToResource.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...