I want to give wishlist functionality in my website so for that i have written code which is listed below. Now when i'm checking whether the product is already listed in the wishlist table and if it is wishlisted then delete it. So for that i have written which is listed inside isAlreadyListed function in WishlistRepository :
return $this->firstOrFail()->where('product_id', $p_id)->where('user_id',$u_id);
then i get following error :
Call to undefined method App\Repositories\WishlistRepository::firstOrFail()
WishlistRepository
<?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 {
if($this->isAlreadyWishlisted($params['product_id'], $params['user_id']))
{
$this->removefromWishlist($params);
}
else
{
$wishlist = new Wishlist($params);
$wishlist->save();
return $wishlist;
}
} catch (QueryException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
public function removefromWishlist($params)
{
$wishlist = $this->isAlreadyWishlisted($params['product_id'],$params['user_id']);
$wishlist->delete();
return $wishlist;
}
public function isAlreadyWishlisted(int $p_id,int $u_id)
{
try {
return $this->firstOrFail()->where('product_id', $p_id)->where('user_id',$u_id);
} catch (ModelNotFoundException $e) {
throw new ModelNotFoundException($e);
}
}
}
?>
Model (Wishlist):
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
class wishlist extends Model
{
/**
* @var string
*/
protected $table = "wishlists";
/**
* @var array
*/
protected $fillable = [
'product_id', 'user_id'
];
public function user(){
return $this->belongsTo(User::class);
}
public function product(){
return $this->belongsTo(Product::class);
}
}
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hw24fs
via IFTTT
Aucun commentaire:
Enregistrer un commentaire