I am getting data from wishlist repository where i store user_id and product_id. I am getting this data but i want that product_id data from product repository i have also establish relation in the model of wishlist & product but i cannot get the data from product table. Please help to sort out my issue. I'm getting data in from wishlist controller
$wishlist = $this->wishlistRepository->listWishlist();
Product Model:
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* @var string
*/
protected $table = 'products';
/**
* @var array
*/
protected $fillable = [
'brand_id', 'sku', 'name', 'slug', 'description', 'quantity',
'weight', 'price', 'sale_price', 'status', 'featured',
];
/**
* @var array
*/
protected $casts = [
'quantity' => 'integer',
'brand_id' => 'integer',
'status' => 'boolean',
'featured' => 'boolean'
];
/**
* @param $value
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = $value;
$this->attributes['slug'] = Str::slug($value);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function images()
{
return $this->hasMany(ProductImage::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function attributes()
{
return $this->hasMany(ProductAttribute::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories()
{
return $this->belongsToMany(Category::class, 'product_categories', 'product_id', 'category_id');
}
public function product_attributes()
{
return $this->belongsToMany(Attribute::class, 'product_attributes')->withPivot('value', 'quantity');
}
public function wishlist(){
return $this->hasMany(Wishlist::class);
}
}
Wishlist Model :
<?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);
}
}
Wishlist Repository:
<?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'])!=NULL)
{
$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->model->firstOrFail()->where('product_id', $p_id)->where('user_id',$u_id);
} catch (ModelNotFoundException $e) {
throw new ModelNotFoundException($e);
}
}
}
?>
Product Repository :
<?php
namespace App\Repositories;
use App\Models\Product;
use App\Models\ProductImage;
use App\Traits\UploadAble;
use Illuminate\Http\UploadedFile;
use App\Contracts\ProductContract;
use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
/**
* Class ProductRepository
*
* @package \App\Repositories
*/
class ProductRepository extends BaseRepository implements ProductContract
{
use UploadAble;
/**
* ProductRepository constructor.
* @param Product $model
*/
public function __construct(Product $model)
{
parent::__construct($model);
$this->model = $model;
}
/**
* @param string $order
* @param string $sort
* @param array $columns
* @return mixed
*/
public function listProducts(string $order = 'id', string $sort = 'desc', array $columns = ['*'])
{
return $this->all($columns, $order, $sort);
}
/**
* @param int $id
* @return mixed
* @throws ModelNotFoundException
*/
public function findProductById(int $id)
{
try {
return $this->findOneOrFail($id);
} catch (ModelNotFoundException $e) {
throw new ModelNotFoundException($e);
}
}
/**
* @param array $params
* @return Product|mixed
*/
public function createProduct(array $params)
{
try {
$collection = collect($params);
$featured = $collection->has('featured') ? 1 : 0;
$status = $collection->has('status') ? 1 : 0;
$merge = $collection->merge(compact('status', 'featured'));
$product = new Product($merge->all());
$product->save();
if ($collection->has('categories')) {
$product->categories()->sync($params['categories']);
}
return $product;
} catch (QueryException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
/**
* @param array $params
* @return mixed
*/
public function updateProduct(array $params)
{
$product = $this->findProductById($params['product_id']);
$collection = collect($params)->except('_token');
$featured = $collection->has('featured') ? 1 : 0;
$status = $collection->has('status') ? 1 : 0;
$merge = $collection->merge(compact('status', 'featured'));
$product->update($merge->all());
if ($collection->has('categories')) {
$product->categories()->sync($params['categories']);
}
return $product;
}
/**
* @param $id
* @return bool|mixed
*/
public function deleteProduct($id)
{
$product = $this->findProductById($id);
$product->delete();
return $product;
}
/**
* @param $slug
* @return mixed
*/
public function findProductBySlug($slug)
{
$product = Product::with('product_attributes')->where('slug', $slug)->first();
return $product;
}
/**
* @param $slug
* @return mixed
*/
public function listFeaturedProduct()
{
$product = Product::with('images')->where('featured', 1)->get();
return $product;
}
/*public function getProductAttributes($id)
{
return Category->find($id);
} */
}
?>
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();
dd($wishlist);
return view('site.pages.wishlist', compact('wishlist'));
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire