mardi 18 février 2020

Argument 1 passed to Illuminate\\Database\\Eloquent\\Model::__construct() must be of the type array, object given

I want to add product to wishlist and following repository pattern to store product id and user id of the user who wants to add product to his wishlist but when i run my code i get

Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given, called in .... WishlistRepository.php

This is my Code:

WishlistController.php

namespace App\Http\Controllers\Site; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use 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;
    }



}
?>

view (script):

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

     jQuery.ajax({

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

              }});  

});


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SBMTrb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire