category material many to many

This commit is contained in:
merdiano 2019-07-10 18:47:00 +05:00
parent baf6e73c65
commit 4e7d011fbb
13 changed files with 65 additions and 27 deletions

View File

@ -11,6 +11,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\CategoryMaterial;
use App\Models\Material;
use App\Models\Order;
use App\User;
@ -56,18 +57,24 @@ class AdminController extends Controller
$this->data['field']['start_name'] = 'start';
$this->data['field']['end_name'] = 'end';
$this->data['field']['label'] = 'range';
$this->data['categories'] = Category::where('depth',1)
->select('categories.*',
DB::raw("(Select COUNT(orders.id) From orders
Where orders.category_id = categories.id and orders.payed = 1{$filter_by_date}) as orders"),
DB::raw("(Select SUM(orders.price) From orders
Where orders.category_id = categories.id and orders.payed = 1{$filter_by_date}) as total"),
DB::raw('SUM(materials.view) as views'),
DB::raw('SUM(materials.like) as likes'))
->leftJoin('materials','categories.id','=','materials.category_id')
->groupBy('categories.id')
->get();
// $this->data['categories'] = Category::where('depth',1)
// ->select('categories.*',
// //DB::raw("(Select COUNT(orders.id) From orders
// //Where orders.category_id = categories.id and orders.payed = 1{$filter_by_date}) as orders"),
// //DB::raw("(Select SUM(orders.price) From orders
// //Where orders.category_id = categories.id and orders.payed = 1{$filter_by_date}) as total"),
// DB::raw('SUM(materials.view) as views'),
// DB::raw('SUM(materials.like) as likes'))
//// ->leftJoin('materials','categories.id','=','materials.category_id')
// ->groupBy('categories.id')
// ->get();
$this->data['categories'] = CategoryMaterial::with(['category:name,id'])
->selectRaw('category_id, SUM(orders_count) as orders, SUM(orders_payed_count) as total, SUM(views) as views, SUM(likes) as likes')
->groupBy('category_id')
->distinct('category_id')
->get();
//dd($this->data['categories']);
return view('backpack::dashboard', $this->data);
}
@ -79,7 +86,7 @@ class AdminController extends Controller
$filter_by_date = " and (orders.created_at BETWEEN {$start_date} and {$end_date})";
$category = Category::findOrfail($cat_id);
$materials= Material::where('category_id',$cat_id)
$materials= $category->materials()
->select('materials.title','materials.view','materials.like',
DB::raw("(Select COUNT(orders.id) From orders
Where orders.material_id = materials.id and orders.payed = 1{$filter_by_date}) as orders"),

View File

@ -6,7 +6,7 @@ use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\MaterialRequest as StoreRequest;
use App\Http\Requests\MaterialRequest as UpdateRequest;
use App\Http\Requests\MaterialRequest as UpdateRequest;
use Backpack\CRUD\CrudPanel;
/**
@ -38,6 +38,15 @@ class MaterialCrudController extends CrudController
$this->crud->addColumns([
['name' => 'title','type' => 'text', 'lable' => trans('admin.title')],
[
// n-n relationship (with pivot table)
'label' => "Categories", // Table column heading
'type' => "select_multiple",
'name' => 'categories', // the method that defines the relationship in your Model
'entity' => 'categories', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Models\Category", // foreign key model
],
['name' => 'view','type' => 'text', 'lable' => trans('admin.view')],
['name' => 'rating','type' => 'text', 'lable' => trans('admin.like')],
['name' => 'price','type' => 'text', 'lable' => trans('admin.price')],
@ -49,8 +58,15 @@ class MaterialCrudController extends CrudController
$this->crud->addFields([
['name' => 'title','type' => 'text', 'lable' => trans('admin.title'),'tab' => trans('admin.material.firsttab')],
['name' => 'category_id', 'type' => 'select', 'entity' => 'category', 'attribute' =>'name', 'model' => 'App\Models\Category',
'lable'=>trans('admin.category'), 'tab' => trans('admin.material.firsttab')],
[
'name' => 'categories',
'type' => 'select2_multiple',
'entity' => 'categories',
'attribute' =>'name',
'model' => 'App\Models\Category',
'pivot' => true,
'lable'=>trans('admin.category'), 'tab' => trans('admin.material.firsttab')
],
['name' => 'desc','type' => 'textarea', 'lable' => trans('admin.desc'),'tab' => trans('admin.material.firsttab')],
['suffix' => 'man.','attributes' => ["step" => "any"],'name' => 'price','type' => 'number', 'lable' => trans('admin.price'),'tab' => trans('admin.material.secondtab')],

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Http\Requests\ProfileRequest;
use App\Models\Category;
use App\Models\CategoryMaterial;
use App\Models\Material;
use App\Models\Order;
use Carbon\Carbon;
@ -46,7 +47,7 @@ class HomeController extends Controller
//todo restrict sort to be only {'all','rate','date'}
if($cat_id != 0){
$cat = Category:: findOrFail($cat_id);
$materials = Material::where('category_id',$cat_id);
$materials = $cat->materials();
}
else{
@ -107,6 +108,8 @@ class HomeController extends Controller
$material->view ++;
$material->save();
CategoryMaterial::where('material_id',$material_id)
->update(['views'=> DB::raw('views + 1')]);
// dd($material->details);
if(!$watch_list_cookie){
$watch_list = [1 => $material_id];
@ -147,8 +150,8 @@ class HomeController extends Controller
return view('material',compact('material'))
->with('liked',$liked)
->with('order',$order)
->with('cat',$material->category);
->with('order',$order);
// ->with('cat',$material->category);
}
public function like($material_id){
@ -161,6 +164,8 @@ class HomeController extends Controller
Cookie::queue('likelist', json_encode($like_list), 450000);
$material->like ++;
$material->save();
CategoryMaterial::where('material_id',$material_id)
->update(['likes'=> DB::raw('likes + 1')]);
}
else{
$like_list = json_decode($like_list_cookie,true);
@ -169,6 +174,8 @@ class HomeController extends Controller
$like_list[]=$material_id;
$material->like ++;
$material->save();
CategoryMaterial::where('material_id',$material_id)
->update(['likes'=> DB::raw('likes + 1')]);
Cookie::queue('likelist', json_encode($like_list), 450000);
}
}

View File

@ -34,6 +34,7 @@ class OrderController extends Controller
$order->oid = str_replace('-','',Str::orderedUuid());
$order->save();
//todo update order_count on category_material
return $this->register($order);
}

View File

@ -27,7 +27,11 @@ class MaterialRequest extends FormRequest
{
return [
'title' => 'required|min:5|max:255',
'category_id' => 'required|integer'
'desc' => 'required',
'price' => 'required|numeric',
'size' => 'required|numeric',
'duration' => 'required|numeric',
// 'category_id' => 'required|integer'
];
}

View File

@ -36,7 +36,7 @@ class Category extends Model
*/
public function materials(){
return $this->hasMany(Material::class);
return $this->belongsToMany(Material::class,'category_material');
}
/*
|--------------------------------------------------------------------------

View File

@ -47,8 +47,8 @@ class Material extends Model
|--------------------------------------------------------------------------
*/
public function category(){
return $this->belongsTo(Category::class);
public function categories(){
return $this->belongsToMany(Category::class,'category_material');
}
public function orders(){
return $this->belongsTo(Order::class);

View File

@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddToOrdersTable extends Migration
class AddToOrdersTable2 extends Migration
{
/**
* Run the migrations.

View File

@ -15,6 +15,8 @@ class AlterMaterialsTable extends Migration
{
Schema::table('materials', function (Blueprint $table) {
$table->dropColumn('duration');
$table->dropForeign(['category_id']);
$table->dropColumn('category_id');
// $table->unsignedDecimal('duration')->nullable();
//
});

View File

@ -26,7 +26,7 @@ class AddToMaterialsTable extends Migration
public function down()
{
Schema::table('materials', function (Blueprint $table) {
//
$table->dropSoftDeletes();
});
}
}

View File

@ -15,6 +15,7 @@ return[
'material.thirdtab' => 'Üçünji',
'material.fourthtab' => 'Dördünji',
'material.extras' => 'Goşmaça',
'material.image' => 'Cover Image',
'download_count' => 'Download limidy',
'day_count' => 'Download gün limidy',
'duration' => 'Dowam wagty',

View File

@ -8,7 +8,7 @@
<div class="tab-content col-lg-9 col-md-9 col-sm-9 col-xs-12">
<div id="redak-tc" class="tab-pane fade active in" role="tabpanel">
<div class="for-padding">
<h2>@lang('content.searh_result',['key' => $key])</h2>
<h2>@lang('content.search_result',['key' => $key])</h2>
<div class="platny-inner-tab">
<ul role="tablist" >
<li class="@if($sort == 'new')active @endif">

View File

@ -68,12 +68,12 @@
<tbody>
@foreach($categories as $category)
<tr>
<td>{{$category->name}}</td>
<td>{{$category->category->name}}</td>
<td>{{$category->orders}}</td>
<td>{{$category->total}}</td>
<td>{{$category->views}}</td>
<td>{{$category->likes}}</td>
<td><a href="{{route('cat_stats',$category->id)}}">details</a> </td>
<td><a href="{{route('cat_stats',$category->category->id)}}">details</a> </td>
</tr>
@endforeach
</tbody>