I just set up a new model and I am trying to call a custom written static method that I've written from inside a new controller to match the model:
<?php
class NewModelWebController extends AjaxController {
public function newModelView() {
$loggedUser = Auth::user();
$data['user'] = $loggedUser;
$data['allDetails'] = NewModel::getFullWithDetails($loggedUser->user_id);
return View::make('webApp::new-model.view', $data);
}
}
Here is the method definition inside the model class (there are no spaces before the php declaration):
<?php
class NewModel extends Eloquent {
protected $table = 'new_models';
protected $primaryKey = 'new_model_id';
public static function getFullWithDetails($userId) {
return 1; // doesn't matter what I return -- the problem still happens
}
}
The error that is returned is the following:
Call to undefined method Illuminate\Database\Query\Builder::getFullWithDetails()
When I make calls such as NewModel::find(1); it works with no issues, but once I try to make calls on the method I wrote, it does not work.
I tried all of the following commands with no success:
composer update
composer dump-autoload
composer clear-cache
chown -R www-data:www-data ./theProjectFile
Thoughts are appreciated.
Try using scopes instead of actual static methods.
class NewModel extends Eloquent {
public function scopeGetFullWithDetails($userId)
{
//
}
}
Calling is the same. NewModel::getFullWithDetails(1);
I ended up having to recreate the model from scratch. There must have been a problem with file encoding or filetype when I initially created it because originally I just duplicated another model class and then changed the values inside. Creating a brand new file and then pasting the PHP content back in fixed the issue! Thanks for the help!
Related
I have this basic app that i am working on in Laravel. A user asks a query and other users can comment on it.
I have this relationship built in the Comment model-
class Comment extends Model
{
public function query()
{
return $this->belongsTo(Query::class);
}
}
When i run php artisan tinker and create a new instance like so-
$comment = new App\Comment
I get an error-
Cannot make static method Illuminate\Database\Eloquent\Model::query() non static in class App\Comment
The problem i figured was with the name of the function 'query'. Because if i change the name to anything else, it works. I don't get any error.
I found out that there is a function in lluminate\Database\Eloquent\Model with the name 'query' that has this code in it-
public static function query()
{
return (new static)->newQuery();
}
So, am I not allowed to use the word 'query' to name the function in my model?
Method query() is used in Model. So, only rename your method to avoid bad behavior.
I created a User class in Laravel application as follows:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
...
public function hasAnyRoles()
{
return true;
}
The function there is simplified to always return true for the purposes of this example. I pretty much followed this tutorial here to create this class: http://alexsears.com/article/adding-roles-to-laravel-users. I created a controller next as follows:
class WelcomeController extends Controller
{
public function welcomeAction()
{
$user = User::find(Auth::user()->id);
$result = $user->hasAnyRoles();
return Response::make("Result: ".$result);
}
}
I'm able to successfully login to the system, routes are working as intended, the variable $user is correctly initialized and I can get all the information out of it (username, id, email, etc.) but once I call the $user->hasAnyRoles() method I get:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::hasAnyRoles()
If I comment out the respective line in the controller it all works but I cannot call any method of that model without getting that error. Any ideas why this is happening?
As stupid as it is, it turns out that the application was reading the User class from a different file. I had a User_backup.php copied in the same directory just in case and this was the file that the class was read from so any changes to User.php were disregarded. I had to remove the backup file and do a composer update in order to get it all working correctly..
I have created an example model:
class ExampleModel extends Eloquent {
protected $table = 'example_model';
}
Then I tried to call ExampleModel::all() which returned an error function all() does not exist (seems like the class is created but without Eloquent functions). After changing model name (and file name) to Examplemodel (now the model name is one word) it works. The problem is not that I misspelled something as I have checked that like a hundred of times and my file+class names are matching.
Am I missing something about auto loading?
I am on windows so I also tried changing file's name to Examplemodel without changing the class name but it still seems to load the class without Eloquent methods.
Did you do Autoload (dumpautoload)?
Because of this case works for me.
Do composer dump-autoload
As the following works for me:
class ExampleTest extends \Eloquent {
protected $table = 'users';
}
Then on the routes file I have:
Route::get('/', function()
{
$data = ExampleTest::all();
return $data;
});
This gives me the data from my users table.
create a file example.php in models folder and then write this:
class Example extends Eloquent {
protected $table = 'example_model';
}
and then try:
Example::all();
taken from here
I have found a problem. I had a migration with class same as my model. Laravel is auto loading models with migrations and it thought I was referring to migration instead of a model.
After fixing that of course I had to run dumpautoload composer command.
I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views
First please be gentle i am a beginner and im only coding for practise.
I try to pass an instance to the model but i always get this error
Argument 1 passed to Store::__construct() must be an instance of Illuminate\Filesystem\Filesystem, none given
my model
<?php
use Illuminate\Filesystem\Filesystem as File;
class Store extends Eloquent
{
public $timestamps = false;
public function __construct(File $file)
{
$this->file = $file;
}
}
Could please somebody tell me what i am doing wrong?
thank you
EDIT
I just used simply like this in my Controller
public function index()
{
$store = new Store;
return View::make('store', $store);
}
The File class is one of Laravels Facades, which means you do not need to pass it into your models construct.
You can access it from anywhere in Laravel using File::someMethod(). If you use namespaces then you have to access via the root namespace \File::someMethod().
Within your store view you can access the File facade directly with the aforementioned method.
Take a look at the documentation on the file system here http://laravel.com/api/class-Illuminate.Filesystem.Filesystem.html
So you can use File::copy() without having to instantiate a class as it is called from a static method.