Commit f6da2f63 by Francisco Salazar

productos relacionados a subcategorias

parent 93d8203d
......@@ -7,6 +7,8 @@ use App\Http\Controllers\Controller;
use Onestartup\Shop\Model\ProductCategoryShop as Category;
use Onestartup\Shop\Model\ProductSubCategoryShop as SubCategory;
use Onestartup\Shop\Model\ProductShop as Product;
use Onestartup\Shop\Requests\RequestSubCategory;
......@@ -18,8 +20,9 @@ class SubCategoryController extends Controller{
return $subcategories = $category->subcategories()->get();
}
$subcategories = $category->subcategories()->paginate(30);
$products = Product::pluck('name', 'id');
return view("shop::subcategory.index")
->with("products", $products)
->with("subcategories", $subcategories)
->with("category", $category);
}
......@@ -27,8 +30,10 @@ class SubCategoryController extends Controller{
public function edit($category_id, $id){
$category = Category::find($category_id);
$subcategory = SubCategory::where("category_id", $category_id)->find($id);
$products = Product::pluck('name', 'id');
return view("shop::subcategory.edit")
->with('products', $products)
->with("subcategory", $subcategory)
->with("category", $category);
}
......@@ -37,6 +42,8 @@ class SubCategoryController extends Controller{
$subcategory = new SubCategory($request->all());
$subcategory->category_id = $category_id;
$subcategory->save();
$subcategory->related_products()->sync($request->related_products);
if (isset($request->portada)) {
$file = $request->file('portada');
......@@ -59,6 +66,7 @@ class SubCategoryController extends Controller{
$subcategory = SubCategory::where("category_id", $category_id)->find($id);
$subcategory->fill($request->all());
$subcategory->save();
$subcategory->related_products()->sync($request->related_products);
if (isset($request->portada) && $request->portada != null ) {
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRelatedProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('related_products', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')
->references('id')
->on('products_shop')
->onDelete('cascade');
$table->integer('subcategory_id')->unsigned();
$table->foreign('subcategory_id')
->references('id')
->on('product_subcategories_shop')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('related_products');
}
}
......@@ -66,4 +66,8 @@ class ProductShop extends Model
public function tags(){
return $this->belongsToMany('Onestartup\Shop\Model\ProductTag', 'product_product_tags', 'product_id', 'tag_id');
}
public function subcategories_related(){
return $this->belongsToMany('Onestartup\Shop\Model\ProductSubCategoryShop', 'related_products', 'product_id', 'subcategory_id');
}
}
......@@ -24,4 +24,8 @@ class ProductSubCategoryShop extends Model
public function products(){
return $this->hasMany('Onestartup\Shop\Model\ProductShop', 'subcategory_id');
}
public function related_products(){
return $this->belongsToMany('Onestartup\Shop\Model\ProductShop', 'related_products', 'subcategory_id', 'product_id');
}
}
<?php
namespace Onestartup\Shop\Model;
use Illuminate\Database\Eloquent\Model;
class RelatedProducts extends Model
{
protected $table = 'related_products';
protected $fillable = ["product_id", "subcategory_id"];
public function products(){
return $this->belongsToMany('Onestartup\Shop\Model\ProductShop', 'product_id');
}
public function subcategories(){
return $this->hasMany('Onestartup\Shop\Model\ProductSubCategoryShop', 'subcategory_id');
}
}
......@@ -71,3 +71,9 @@
</div>
</div>
@endsection
@section('script_extras')
<script>
$('#related_products').select2();
</script>
@endsection
......@@ -20,6 +20,11 @@
</div>
<div class="form-group">
{!! Form::label('related_products', 'Productos relacionados') !!}
{!! Form::select('related_products[]', $products, null, ["class"=>"form-control", "required"=>"required", "multiple" => "multiple", "id" => "related_products", "style" => "width: 100%;"]) !!}
</div>
<div class="form-group">
{!! Form::label('portada', 'Imagén de portada') !!}
@if (isset($subcategory))
<span>
......
......@@ -100,4 +100,10 @@
</div>
</div>
</div>
@endsection
@section('script_extras')
<script>
$('#related_products').select2();
</script>
@endsection
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment