File: /home/imensosw/www/amanda/app/Http/Controllers/ProductCommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Product_image;
use App\Product_price;
use App\Product_video;
use DB;
use App\Category;
use App\Tag;
use App\Product_tag;
use App\ProductComment;
class ProductCommentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$limit = config('constants.limit');
$search = $request->search;
$product_id = $request->filter;
$sortby = $request->sortby;
if($sortby=="product_id"){
$orderType="product_comments.product_id";
}
else if($sortby=="id"){
$orderType = "product_comments.id";
}
else{
$orderType ="product_comments.created_at";
}
$orderBy = "DESC";
$comment = ProductComment::
leftjoin('products','products.id','product_comments.product_id')
->leftjoin('users','users.id','product_comments.user_id')
->where(function($q) use ($search)
{
$q->orWhere('product_comments.message', 'like', '%' . $search . '%');
})
->when($product_id,function($q) use ($product_id)
{
if ($product_id!="" ) {
$q->where('product_comments.product_id',$product_id);
}
})
->select('product_comments.*','products.name as pro_name', 'users.name as user_name')
->orderBy($orderType, $orderBy)
->groupBy('product_comments.id')
->paginate($limit);
return response()->json($comment,200);
}
public function getProductList()
{
$products = Product::all();
return response()->json($products,200);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// $status = $product->delete();
$status = ProductComment::find($id);
$status->delete();
return response()->json([
'status' => $status,
'message' => $status ? 'Product Comment Deleted!' : 'Error Deleting Product Comment'
]);
}
}