17 Nov - Static Sites to laravel
This commit is contained in:
parent
610aa1e202
commit
5ff57a21a7
3661 changed files with 569001 additions and 771 deletions
95
app/Repositories/ProductRepository.php
Normal file
95
app/Repositories/ProductRepository.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductAttribute;
|
||||
use App\Models\ProductCategory;
|
||||
|
||||
class ProductRepository extends BaseRepository {
|
||||
|
||||
|
||||
public function __construct(Product $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* refresh.
|
||||
*/
|
||||
public function update($data)
|
||||
{
|
||||
|
||||
$data['active'] = isset($data['active']) ? 1 : 0;
|
||||
|
||||
|
||||
if($data['id'] == "new"){
|
||||
$this->model = Product::create($data);
|
||||
}
|
||||
else{
|
||||
$this->model = $this->getById($data['id']);
|
||||
$this->model->fill($data);
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
$this->updateCategories(isset($data['categories']) ? $data['categories'] : array());
|
||||
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array());
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
public function updateCategories($data = array())
|
||||
{
|
||||
foreach ($this->model->categories as $category) {
|
||||
if(($pos = array_search($category->category_id, $data)) !== FALSE){
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
$category->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
foreach ($data as $id) {
|
||||
ProductCategory::create([
|
||||
'product_id' => $this->model->id,
|
||||
'category_id' => $id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateAttributes($data = array())
|
||||
{
|
||||
foreach ($this->model->attributes as $attribute) {
|
||||
if(($pos = array_search($attribute->attribute_id, $data)) !== FALSE){
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
$attribute->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
foreach ($data as $id) {
|
||||
ProductAttribute::create([
|
||||
'product_id' => $this->model->id,
|
||||
'attribute_id' => $id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function delete()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue