Laravel 5.3 class/routes - php

Hej! My question is the next. I started to use Laravel 5.3. How can I do this class in Laravel:
class Vehicle
{
public $vehicletype;
function invt($vehicletype){
$this->vehicletype=$vehicletype;
}
function outvt(){
return $this->vehicletype;
}
}
I already have ajax .post ,the route:
Route::get('/ajax-vehicletype',function(){
$vehicletypevalue=Input::get('vehicletype');
Vehicle::invt($vehicletypevalue);
});
I get error:
Non-static method App\Vehicle::invt() should not be called statically, assuming $this from incompatible context
Thanks.

You can define controller and then define function inside that controller and pass that in your route argument.
class Vehicle extends Controller{
public $vehicletype;
function invt($vehicletype){
$this->vehicletype=$vehicletype;
}
function outvt(){
...........
}
}
In your route file you can define the route as
Route::get('/ajax-vehicletype/{vehicletype}','Vehicle#invt');

You need to learn basic PHP first.
The problem you are facing is because you are calling non-static method statically as it is said in the error. The quick fix would be to define invt method as static: http://php.net/manual/en/language.oop5.static.php

Related

Non-static method Illuminate\Http\Request::url() cannot be called statically

I'm using Laravel 9 and I want to redirect user to same page url, so I tried this:
if(!$status){
alert()->error('Wrong code!');
return redirect(Request::url());
}
And I have included as well the Request but don't know why get this error:
Non-static method Illuminate\Http\Request::url() cannot be called statically
So what's going wrong here? How can I solve this issue?
You can use
return redirect()->back()->withInput();
Or
use Illuminate\Http\Request;
public function yourMethod(Request $request) # inject $request here
{
return redirect($request->url());
}

In yii why we don't use action static

When we create an action in controller then why we don't use it static
Public static function actionLogin(){
}
$this
The whole application get the $this from controller classes, so we cannot use the static keyword.
Because the controller is being instantiated and the actions might access properties of the controller.

what is $this->load->view() in CodeIgniter

$this is use for current class and view is method but what is load. Is this a property?
Is this example correct?
class super{
public $property;
public function superf1()
{
echo "hello";
}
public function col()
{
$this->superf1();
}
$this->property->super1();
}
Yes, load is a property.
Think of it like this:
class Loader {
public function view() {
//code...
}
}
class MyClass {
private $load;
public __constructor() {
$this->load = new Loader();
}
public someMethod() {
$this->load->view();
}
}
This syntax is called chaining.
Your controller inherits CI_Controller. So, if you look in application/system/core/Controller.php you'll find something interesting : $this->load =& load_class('Loader', 'core'); (l.50 with CI2). So, $this->load refer to the file application/system/core/Loader.php which have a function public function view (l.418 with CI2)
In the context of a class that extends CI_Controller (in other words: a controller) the symbol $this is the Codeigniter "super object". Which is, more or less, the central object for CI sites and contains (among other things) a list of loaded classes. load is one of the classes you'll always find there because it is automatically loaded by the CI system.
Technically, the class creates objects of the type CI_Loader. view() is just one of the many methods in the load class. Other commonly used class methods are model(), library(), config(), helper(), and database(). There are others.
So, in short, load is a class used to load other resources.
load is a class belongs to the loader class
codeigniter official documentation
view, model and others are methods
In PHP 8.1
use return("viewname", $data)

How to call a trait method with alias

I'm trying to place a trait inside a class called Page. I also need to rename a trait function so that it doesn't clash with an existing class function. I thought I did all this successfully however I get an error that points to the wrong location?!
Call to undefined function App\Pages\Models\myTraitDefaultScope()
I've also tried: MyTrait\defaultScope($query) instead of trying to rename the conflicting function. But I then get the following error:
Call to undefined function App\MyTrait\defaultScope()
Below is the trait and class contained in separate files.
<?php
namespace App;
use Illuminate\Support\Facades\Auth;
trait MyTrait{
public function defaultScope($query){
return $query->where('active', '1')
}
}
.
<?php namespace Modules\Pages\Models;
use Illuminate\Database\Eloquent\Model;
use App\MyTrait;
class Page extends Model {
use MyTrait{
MyTrait::defaultScope as myTraitDefaultScope;
}
public function defaultScope($query){
return myTraitDefaultScope($query);
}
}
I'm not all that awesome at this so please don't shoot if I've got something badly wrong :)
When you 'use' a trait in your class, the class inherits all the methods and properties of the trait, like if it was extending an abstract class or an interface
So, this method of MyTrait:
public function defaultScope($query){
return $query->where('active', '1')
}
will be inherited by your Page class
As you have aliased this method as: myTraitDefaultScope, to call the method you should call it in the same way you would call every other method of the Page class:
public function defaultScope($query){
//call the method of the class
return $this->myTraitDefaultScope($query);
}
As you're using trait. So it points to the current or parent class. Thus, calling any method should be like $this->method($params); syntax.

Laravel custom Helper class -- can't call non-static methods from other classes

I want to use the Config::get() method in my custom Helper class, but always get an error.
At the top of a Helper.php file I have the following:
use \Illuminate\Config\Repository as Config;
Then, I have a public static function in which I want to get use the Config::get() method to grab a config setting. For simplicity, let's pretend the function is:
public static function getURL() {
return Config::get('assets.url');
}
I have an assets.php file with this url variable set. The Config::get('assets.url') method works elsewhere in my site.
But when trying to use Config::get in my Helper.php file, i get this error:
Non-static method Illuminate\Config\Repository::get() should not be called statically
I obviously can't change the Config::get method to a static one. What can i do?
You can try just importing the Facade instead of trying to get the underlying class.
use Config;
Then just use Config like normal in your class.

Categories