Lumen: Call trait helper file from model file not working - laravel

I am using my trait file "CommonTrait" inside my model file as below,
use in namespace,
use App\Http\Helpers\CommonTrait;
use inside class,
class LoginHistory extends Model
{
use CommonTrait;
//use inside function as
protected static function getList($req)
{
$reportFilter= $this->searchCommonFilter($reportDateFilter, $req);
//this is my trait function
}
}
but it gives me error like
Using $this when not in object context

you are accessing a method from a static function, that's what the error message means, you can call other static functions/properties using static::function() or static::property.
In your case the function does not seems to be static, you need to either remove the static declaration from the fuinction or make the function which you are calling to a static one.

Related

Moving class binds global helper functions to namespace?

I have a class like this:
namespace App\Bills;
class Addition
{
public function get()
{
return collect();
}
}
Where collect is actually a global helper function. When I rightclick on the classname and click "Move class" and change Namespace to App\Models\Invoice this happens:
namespace App\Models\Invoice;
use function App\Bills\collect;
class Addition
{
public function get()
{
return collect();
}
}
Which breaks my code with
Error: Call to undefined function App\Bills\collect()
because the method collect() is actually a global Laravel helper.
How can I tell PHPSTORM not to escape global functions?

Cannot declare class App\Http\SeoInit\SeoInit, because the name is already in use

I am creating a helper function its show this error
Cannot declare class App\Http\SeoInit\SeoInit, because the name is
already in use.
Here is code
namespace App\Http\SeoInit;
class SeoInit
{
public static function Funct_Name()
{
//
}
}
Thanks.
How to define a helper function into app.php aliases, The function is available in app->http Folder

How to autoload custom class in Laravel?

I wrote custom class with method that returns array.
I need to autoload this class like Auth() class in Laravel, that I could get access to it from any controller not using use
Create one custom helper file
and add function
if (! function_exists('yourcustomclass')) {
function yourcustomclass()
{
use App\Http\yourcustomclassname;
return new yourcustomclassname()
}
}
you can use yourcustomclass() function from anywhere to get yourcustomclassname class object
When accessing class/function from other namespace than you're currently in you have to use Fully-Qualified Class Name (or type use, but you don't want to do that), so instead of Auth::user() you need to write \Auth::user()
\ at the beginning means that class is located in root namespace
Why don't you write super method(s) in App\Http\Controllers\Controller?
Simply call super method(s) in the subclass which extends Controller

Laravel - Running method of another controller in one controller

I have UserController and PetController.
In my UserController, I have rewardUser() method.
in my PetController, I'm using the $user variable which indicates the current logged in user.
How I can run my rewardUser() method from my PetController?
I've been trying to user $user->rewardUser(); but for some reasons its not recognizing my method this way.
"Call to undefined method Illuminate\Database\Query\Builder::rewardUser()"
The best way is to use a trait.
Create a trait file, in App\Common.php, for e.g. Then copy the rewardUser() method to the trait.
Your trait file:
namespace App\Forum;
trait Common {
public function rewardUser() {
// Your code here...
}
}
Then in yourUserController.php and PetController.php, use the trait.
// UserController and PetController.php
namespace App\Http\Controllers
use App\Common; // <- Your trait
class UserController extends Controller {
use Common // <- Your trait
public function doSomething() {
// Call the method from both your controllers now.
$this-rewardUser();
}
}
You can use the straight in as many controllers as you want and you can call the method in the straight using $this->methodName().
Very simply and effective.
It seems like you are missing some structure concepts, but if you really need it, you may use the container to do so:
$userController = app()->make(UserController::class);
return app()->call([$userController, 'rewardUser']);
may be you should define the method rewardUser() in the User Model and import it with use App\User

Joomla 2.5 cant use $this in helper file

I have created a component and a plugin in joomla 2.5 and there is a Helper file in the component which will have many useful functions and I plan to call one of its function which then calls another function in the helper by this code:
$this->getinformation();
and it gives me this error :
Fatal error: Call to undefined method
My questions are:
Why can't I call a function in a helper in Joomla?
How can I call a function inside helper class?
Is there any class structure which i missed in this code?
Helper files are typically called statically and not using $this
First create your helper file and add methods like this:
Class myHelper {
//This method can call other methods
public static function myMethod($var) {
//Call other method inside this class like this:
self::myOtherMethod($var);
}
//This method is called by myMethod()
public static function myOtherMethod($var) {
//Put some code here
}
}
Simply include the helper file like this in the documents that you would to use it:
require_once JPATH_COMPONENT.'/helpers/my_helper.php';
Then use it like this:
myHelper::myMethod($var);
or
myHelper::myOtherMethod($var);
You have to include the helper file and call the function using the classname
Add the following line in the plugin or component:
jimport( 'joomla.filesystem.folder' );
require_once JPATH_ROOT . '/components/com_xxxx/helper.php';
classname::functionname();
OR
If you are working on the same helper file means then call like this
classname::functionname();

Resources