DrPrez said:
I believe you can only use $table without $this->
Then the debugger screams:
Undefined variable: table
$table is an instance variable, not a static variable, so you can't use it in a static function (basic PHP).
thepsion5 said:
$table is an instance variable, not a static variable, so you can't use it in a static function (basic PHP).
So what's the solution? Is it impossible to add variables to a model in Laravel?
If i remove the "static" from the method it looks like this
public function getCustomers()
but then Laravel again screams
Non-static method Vorlauf::getCustomers() should not be called statically, assuming $this from incompatible context
Thanks, Sharping
You could try and define $table as a static variable. Or could use something like this:
public function getDistinctCustomerNames()
{
$customer = new Customer;
return DB::select("SELECT DISTINCT customer FROM ".$customer->table." ORDER BY customer ASC");
}
No chance! :-( This only happens if the class extends Eloquent...
If i put the method directly into the Controller it works.
Is this really so complicated?
I just want to define $table once "in the header" to use it in several methods of the Model (without hardcoding the table name everytime inside the query):
<?php
class CustomerModel extends Eloquent {
public $table = 'customers';
public $timestamps = false;
public static function getCustomers(){return DB::select("SELECT * FROM ".$this->table);}
public static function getSomething(){return DB::select("SELECT * FROM ".$this->table);}
public static function getCities(){return DB::select("SELECT * FROM ".$this->table);}
}
DrPrez said:
Sorry my bad, correct way is...
public static $variable = 'value'; public static function whatever() { dd(self::$variable); }
That's what I meant when I suggested that Sharping declare $table as a static variable.
voronovigor liked this reply
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community