传递给Illuminate \\ Database \\ Eloquent \\ Model :: __ construct()的参数1必须是数组类型,给定对象

问题描述 投票:0回答:2

我想将产品添加到愿望清单,并按照以下存储库模式存储要向其愿望清单添加产品的用户的产品ID和用户ID,但是当我运行我的代码时,我会得到

参数1传递给Illuminate \ Database \ Eloquent \ Model :: __ construct()必须属于类型数组,给定对象,在...中调用。WishlistRepository.php

这是我的代码:

WishlistController.php

命名空间App \ Http \ Controllers \ Site;使用App \ Http \ Controllers \ Controller;使用Illuminate \ Http \ Request;使用App \ Contracts \ WishlistContract;

class WishlistController extends Controller
{
    protected $wishlistRepository;

    public function __construct(WishlistContract $wishlistRepository)
    {
        $this->wishlistRepository = $wishlistRepository;
    }

    public function show()
    {
        $wishlist  = $this->wishlistRepository->listWishlist();
        return view('site.pages.wishlist', compact('wishlist'));
    }

    public function addToWishlist(Request $request)
    {
        $productID=$request->p_id;
        $userID=auth()->user()->id;
        $data=array('product_id'=>$productID,'user_id'=>$userID);
        $wishlist  = $this->wishlistRepository->addToWishlist($data);
    }
}

WishlistContract.php

<?php
namespace App\Contracts;

/**
 * Interface WishlistContract
 * @package App\Contracts
 */
interface WishlistContract
{
    /**
     * @param string $order
     * @param string $sort
     * @param array $columns
     * @return mixed
     */
    public function listWishlist(string $order = 'id', string $sort = 'desc', array $columns = ['*']);

    /**
     * @param array $params
     * @return mixed
     */
    public function addToWishlist(array $params);

    /**
     * @param  $id
     * @return mixed
     */
    //public function deleteFromWishlist($id);

    /**
     * @param array $params
     * @return bool
     */
    //public function updateWishlist(array $params);

    }


?>

WishlistRepository.php

<?php
namespace App\Repositories;

use App\Models\Wishlist;
use App\Traits\UploadAble;
use Illuminate\Http\UploadedFile;
use App\Contracts\WishlistContract;
use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Doctrine\Instantiator\Exception\InvalidArgumentException;

/**
 * Class WishlistRepository
 *
 * @package \App\Repositories
 */
class WishlistRepository extends BaseRepository implements WishlistContract
{
    use UploadAble;
    public function __construct(Wishlist $model)
{
    parent::__construct($model);
    $this->model = $model;
}

public function listWishlist(string $order = 'id', string $sort = 'desc', array $columns = ['*'])
{
    return $this->all($columns, $order, $sort);
}


    public function addToWishlist(array $params)
    {
        try {
            $collection = collect($params);
            $wishlist = new Wishlist($collection);
            $wishlist->save();
            return $wishlist;
        } catch (QueryException $exception) {
            throw new InvalidArgumentException($exception->getMessage());
        }
    }

    public function removefromWishlist($id)
    {
        $wishlist = $this->findAttributeById($id);

        $wishlist->delete();

        return $wishlist;
    }

    public function updateToWishlist(array $params)
    {
        $wishlist = $this->findWishlistById($params['id']);

        $collection = collect($params)->except('_token');

        $is_filterable = $collection->has('is_filterable') ? 1 : 0;
        $is_required = $collection->has('is_required') ? 1 : 0;

        $merge = $collection->merge(compact('is_filterable', 'is_required'));

        $wishlist->update($merge->all());

        return $wishlist;
    }



}
?>

视图(脚本):

$('body').on('click', '.wishlist', function(e){
    e.preventDefault();
    var product_id=$(this).attr('data-wishlist');

     jQuery.ajax({

              url: "{{ url('/add-wishlist') }}",
              method: 'post',
              data: {
                 "p_id": product_id,"_token": "{{ csrf_token() }}",
              },
              success: function(result){
                // input.val(result.status);

              }});  

});
laravel laravel-5
2个回答
0
投票

您可以通过删除此行来修复它

 $collection = collect($params); 

您不需要它:因为collect会将您的数组转换为Illuminate\Support\Collection对象。

Class WishlistRepository

public function addToWishlist(array $params) {
        try {

            $wishlist = new Wishlist($params);
            // OR
            // $wishlist = new Wishlist();
            // $wishlist->column = $params['column'];
            //...
            $wishlist->save();
            return $wishlist;
        } catch (QueryException $exception) {
            throw new InvalidArgumentException($exception->getMessage());
        }
}

0
投票
 public function addToWishlist(array $params)
    {
        try {
            $collection = collect($params);
            $wishlist = new Wishlist($collection);
            $wishlist->save();
            return $wishlist;
        } catch (QueryException $exception) {
            throw new InvalidArgumentException($exception->getMessage());
        }
    }

您正在将$collection传递给model,希望它具有值。array

当您使Wishlist的新实例按原样传递array时,而不是创建并传递collection

 public function addToWishlist(array $params)
    {
        try {
            return new Wishlist($params);
        } catch (QueryException $exception) {
            throw new InvalidArgumentException($exception->getMessage());
        }
    }
热门问题
推荐问题
最新问题