paginate() method in Laravel 5 not working - pagination

I am working on laravel 5 project. I have stuck on pagination thing. I am trying to access paginate() method, but it says this method is not defined. This is my code where i have used paginate() method
public function show($id){
$codes = DB::table('soada1s')->where('couponcodefile_id','=', $id)->get()->paginate(10);
return view('admin',compact('codes'));
}
I have also included below class in my controller
use Illuminate\Pagination\Paginator;
Please tell me what is wrong?
thanks.

What is the exact error message you're getting?
I think you need to use the paginate function just after your where function.
DB::table('soada1s')->where('couponcodefile_id','=', $id)->paginate(10);
The get() function will return a collection object, and I don't think you can call the paginate function on a collection object.

Related

How to use default constructor in codeIgniter4?

I want to use controller to load directly some models by using default constructor, but CodeIgniter4 was removed __construct method from CodeIgniter4 framework and I got error message Cannot call constructor. please see code below:
public function __construct(){
parent::__construct();
//Do magic task here
}
Instead of CI3 is working by using above code, Can anyone suggest me in CI4?
Thank You for your tips or comments!
Best Regards!
CI4 default controller namespaced CodeIgniter\Controller doesn't have a class constructor. So if you're extending directly from it, parent::__construct() can't be called.
If you really need a constructor for every one of your controller you should modify App\Controllers\BaseController and making your others controllers extending it with the code you provided.
Also if your goal is to execute some code before or after your controller is called you should check out Filters in CI4. They are perfectly designed for this need : https://codeigniter4.github.io/userguide/incoming/filters.html
You can use constructor in CI4 to load models
Add the code in your Controller class and it should work.
protected $userModel;
public function __construct()
{
$this->userModel = new UserModel();//Create a instance of the model
helper('form', 'url');
}

Using Custom Functions with "I" in Page Object and Custom Helper in CodeceptJS

Hi CodeceptJS Community,
Is there a way to use custom defined functions (under steps_file.js) as I. customFunction() in page object files.
Is there a way to use native codeceptjs functions (like I.click()) in my custom helper files
I couldn't find any help in the documentation and in other sources. Is there any way to achieve this?
And is there any way to use xpath locators in puppeteer helper?
this.helpers['Puppeteer'].page.click(xpath);
I had the same problem to use custom steps in pageObjects.
To avoid it, i passed the actor (I) as parameter to my pageObject function.
page object:
const I = actor();
module.exports = {
doSomething(I){
I.login();
}
};
Test scenario:
Scenario('Test something' (I,pageObject)=>{
pageObject.doSomething(I)
})
In this case, pageObjects will has access to all custom steps from I :)
Thank you for your sharing Matheus. I have used a different solution. Instead of writing "I" object in every page object method (which was also one option for me), I have created a custom helper file and written all methods using puppeteer helper like below;
async method() {
await this.helpers['Puppeteer'].click(xpath);
}
I can call this method both in tests and page objects
I.method();
I was facing the same issue and when I looked into the typescripts definitions I noticed that actor() which is required in every page object etc. has custom steps arguments.
So this worked for me to extend the const I = actor(); witht the custom steps form steps_file.js;
const customSteps = require('./steps_file');
const I = actor(customSteps());
After that, I can use all methods in page objects like in tests scenarios which are accessing the methods from steps_file.js

stub an object without requiring a method

I have something like:
sandbox.stub(rp, 'get').resolves(successResponse)
which returns my custom response when it hits this code:
return await rp.get(url, options)
But how can I do something like this:
sandbox.stub(rp).resolves(successResponse)
Which can return a custom response when it hits this code?
return await rp(url, options)
When I attempt "stubbing" the entire object, I get this error when I run the test:
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:70:21)
at stub (node_modules\sinon\lib\sinon\stub.js:58:44)
at Object.stub (node_modules\sinon\lib\sinon\collection.js:93:33)
rp is request-promise-native, which wraps request
From #Troopers' link in the comments above, it seems like this isn't possible: Doing this is not technically possible without faking the entire module loading system.
So I followed the suggestion here: https://github.com/request/request/issues/2181 and used mock-require to stub rp. I also changed up my code that used to call rp.get(), so that it just calls rp(), since I couldn't also figure out how to stub both rp() and rp.get()
You might find the Sinon HowTo about Link Seams helpful: http://sinonjs.org/how-to/link-seams-commonjs/

Call to a member function model() on a non-object error on opencart 2.0

I am use <?php $this->load->model('tool/image'); ?> this on tpl file
i am getting this error Call to a member function model() on a non-object in /home/host/public_html/site/admin/view/template/module/module_name.tpl
any one please solve my problem
thanks
The error means that $this->load is not what you think it is.
I've looked at the docs briefly and the model property is available only inside the controller. You are trying to access it in the view. You need to pass the model object to the view.
Try this in the controller
$this->data['load'] = $this->load;
Then in the view you should be able to do this:
<?php $load->model('tool/image'); ?>
I solve this suppressing the load call, I believe that now thee tpl is executed in the load, then when you call $this->load, you are calling load in load.
If you put
$this->load->model('tool/image');
it should to work, at least it worked in my code...

Kohana helper attribute

I have a question that keeps bothering me. Currently, I have started using Kohana 3.2 Framework. I've written a helper to handle some functionality - I have a number of methods, which are (as it should be) declared STATIC. But, all of these methods are somehow working with the database, so I need to load a model. Currently, every method has a non-static variable like this:
$comment = new Model_Comments;
$comment->addComment("abc");
OK, it seems to be working, but then I wanted to get rid of this redundancy by using class attribute to hold the instance of the model (with is class as well).
Something like this:
private static $comment; // Declaring attribute
self::$comment = new Model_Comment; // This is done within helper __constuct method
self::$comment->addComment("abc"); // And call it within the method.
But, I got failed with: Call to a member function addComment() on a non-object
Question is: is it possible to do it ? Maybe there are some other approaches ?
Sorry for a long story and, thanks in advice! :P
A static method cannot call a non-static method without operating on an instance of the class. So, what you're proposing won't work. There may be a way do accomplish something similar, but what about trying the following:
You could implement the singleton or factory pattern for your "helper" class. Then, you could create the model (as an attribute) as you instantiate/return the instance. With an actual instance of your "helper" class, you won't have to worry about the static scope issues.
In other words, you can create a helper-like class as a "normal" class in your application that, upon creation, always has the necessary model available.
I'd be happy to help further if this approach makes sense.
David

Resources