jeudi 3 octobre 2019

Paginate Return only Pagination without data Laravel 5

I am trying to paginate my data using laravel paginate.So that's how i am doing that i have store model that have one to many relation with coupon model.

class Store extends Model
{

    protected $fillable =['id','name','heading_1','url','slug','image','status','total_coupon_used_times'];


    public function categories()
    {
        return $this->belongsToMany('App\Category', 'store_categories', 'category_id', 'store_id');
    }
    public function coupons()
    {
        return $this->hasMany('App\Coupon');
    }
}

This is my coupon model that have inverse of one to many relation.

class Coupon extends Model
{
    protected $fillable=['id','name','description','status','coupon_type','coupon_status','coupon_code','discount_amount','coupon_url','coupon_used_times','slug','store_id'];


    public function store()
    {
        return $this->belongsTo('App\Store');
    }
}

Further when i had tried to query my store model with coupons using paginate. I was successfully in doing so.

Here is my store controller in which i had done same thing successfully.

class StoreController extends Controller
{
public function index(Request $request){
        if($request->isMethod('get')) {
            $name=$request->input('letter');
            $category=$request->input('category');
            $categories=Category::all();
            if($name==null || $category==null){
                $stores = Store::with('coupons')->paginate(15);
                $storesWithCount=$this->countCoupons($stores);

                return view('stores', ['stores' => $storesWithCount,'search'=>false,'categories'=>$categories]);
            }else {
                $stores=Store::has('categories')->where('name', 'LIKE', "$name%")->get();
                $storesWithCount=$this->countCoupons($stores);
                return view('stores', ['stores' => $storesWithCount,'search'=>true,'categories'=>$categories]);
            }

        }else{
            abort(403, 'Unauthorized action.');
        }
    }
}

But i had tried to do samething with my coupon model in my coupon controller it gives me only pagination without data.

class CouponController extends Controller
{

public function index(Request $request){
        if($request->isMethod('get')){
            $coupons=Store::paginate()->coupons();

            return view('coupons',['coupons'=>$coupons]);

        }else{
            abort(403, 'Unauthorized action.');
        }

    }
}

So if anyone has idea that what i am doing wrong let me know. Thanks Best Regards.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire