I'm japanese student and new to Laravel.
I used the Eloquent's local scope like this:
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
public static function scopeSample($query) {
return $query->where('name');
}
}
For reasons of the security, I changed access-modifier into private:
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
private static function scopeSample($query) {
return $query->where('name');
}
}
And I had exception:
(1/1) BadMethodCallException
Call to undefined method App\Entities\Domains\Test::sample()
Why I can't use local scope in private access? Please tell me the answer.
I don't understand the reason that you want to add the scope functions as private. The scope is used to create a query, that will be done from outside of the class so that shouldn't be private.
Also the function shouldn't be static, see https://laravel.com/docs/5.6/eloquent#local-scopes for more information.
Sorry, It is necessary to define some methods to access scopeSample...
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
private static function scopeSample($query) {
return $query->where('name');
}
public static function getTable() {
$table = self::sample()
->get();
return $table;
}
public static function getTableCount() {
$table = self::sample()
->get();
if ($table->count() === 0) {
return false;
}
else
{
return true;
}
}
}
Actually I want to share the scope with different public methods.
I'm sorry for confusing you.
Sign in to participate in this thread!