BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::makeAllSearchable() - php

I am trying to run the command php artisan scout:import "App\User" to import user records into search driver as per documentation (Laravel 5.3 Scout Documentation). I keep getting [BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::makeAllSearchable()
as an error. Why am I getting this error? I have included the searchable trait in my users controller and added the scout class to my app/config providers array, so I am struggling to see why the method doesn't exist...

You should not add the trait to the controller but to the model. So in your case to App\User.php
<?php
namespace App;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Searchable;
}

Like Jakub has said, you have to add the Searchable trait to your User model, not to the controller.
If you're using toSearchableArray() on your model, do not forget to include the id column in the array, otherwise it won't work.
You could also comment the toSearchableArray() function, import the existing users and then add it back.

Related

Laravel Model not being found

Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place

laravel 5 method does not exist - why can't i access my function?

I am having trouble calling a method in my Account class.
Here is the method defined in my class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
protected $fillable = ['customer_id','bookmaker_id', 'balance', 'profit'];
public function addProfit($amountToAdd) {
$this->profit = $this->profit + $amountToAdd;
}
.......
......
Here is my method call in my controller
$account->addProfit(5);
I have established that I am returning a single account record with the dd($account) command, so it appears it's definitely a problem with the method.
However, I get this message when I try to run the code:
BadMethodCallException in Macroable.php line 81:
Method addProfit does not exist.
In case someone encounters the same issue, Laravel will only call the Model's method if called on one single object. If you have created a custom function in your Model.php file called getAge on a User model, for instance, you have to make sure that in the expression $object->getAge(), $object is one object. This is where using ->first() instead of just ->get() becomes relevant

Extending Laravel 5 PasswordBroker so that I can use another email function

I want to use my own method for the emailResetLink, because I want to send emails with styling and I'm using CssToInlineStyles.
First I tried extending the PasswordBroker:
in PasswordController changed use Illuminate\Contracts\Auth\PasswordBroker to App\PasswordBroker
created new empty class, that extends the PasswordBroker
namespace App;
use Illuminate\Auth\Passwords\PasswordBroker as PasswordBrokerBase;
class PasswordBroker extends PasswordBrokerBase {
}
And I got this error (the newly created class is empty):
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable.
Then I tried to create contextual binding with:
$this->app->when('Illuminate\Auth\Passwords\PasswordBroker')
->needs('Illuminate\Contracts\Mail\Mailer')
->give('App\Services\Mailer');
But nothing happened (it still uses the old Mailer).
Any ideas of how can I achieve the goal?

Laravel - Call to undefined method TrainingFacade::save()

I'm building a Laravel 4.2 app and I'm using Creolab's Modules package. It's an app for managing company trainings, so my module is called Trainings. All works well except saving newly created trainings. The error I receive when I submit the Create New form is:
Call to undefined method Roche\Trainings\Facades\TrainingFacade::save()
These are my TrainingsController, Training Model, TrainingFacade, Trainings Service Provider and routes. Nothing I try seems to fix it.
You don't need to change your facade alias here, but you have other error here.
In your AdminTrainingsController you want to use Training model so you import Training before your class definition this way:
use Training;
but model Training is not in root namespace, in root namespace there is TrainingFacade because you probably define it in your app.php file this way:
'Training' => 'Roche\Trainings\Facades\TrainingFacade',
That's ok, you don't need to change this facade alias.
So now, Laravel inject into your constructor not Training model but TrainingFacade (because Training in root namespace is alias for TrainingFacade).
What you need here is importing correct namespace for your Training model.
When we look at your Training model class we see the code is:
<?php namespace Roche\Trainings\Models;
class Training extends \Eloquent {
protected $guarded = array('id');
}
so it's in Roche\Trainings\Models namespace.
So to make it work in AdminTrainingsController you need to change:
use Training;
into:
use Roche\Trainings\Models\Training;
Now Laravel will inject into constructor Training model (and not TrainingFacade as it was doing).
EDIT
You could also make some tests, for example changing in your app.php your TrainingFacade alias, for example to:
'TrainingFacade' => 'Roche\Trainings\Facades\TrainingFacade',
but if you had use Training as you had, you would get the following error:
Class Training does not exist
because there would be no Training class in global namespace now (you changed alias for facade).
And if you removed use Training; completely, you would get:
Class Roche\Trainings\Controllers\Admin\Training does not exist
because by default class is loaded from current namespace, and your AdminTrainingsController is in Roche\Trainings\Controllers\Admin namespace
I hope now everything is clear, if it's not you could also look at How to use objects from other namespaces and how to import namespaces in PHP where I explained more about namespaces

Referring to an Eloquent model from package

I am trying to use an Eloquent model inside the Sentry package (this could've been any package).
I've refered to it using use App\Models\User; in the top, and User:: when I need to call it.
However, I am getting this error: Class 'App\Models\User' not found
What am I doing wrong?
The current code is here: http://paste.laravel.com/H19
I assume thath your models are in the App part of the application.
In laravel, the App namespace is the top level namespace (the default one).
So to call your models, you have to call \User (because \ refer to the top level namespace)
Alternatively you can do:
use \User as User
And then you can call it User instead of \User

Categories