I have a code in one of my controller files in laravel, below code is in Teamcontroller.php file:
$faqs = Faq::portal()->get()->toJson();
$glossaries = Glossary::portal()->get()->toJson();
Similarly, we have faq and glossary controller.
I am not able to understand what exactly the above code means. I tried to find function inside the Faq model and Faq controller and glossary model and controller, but i am not able to find any function portal.
Can anyone please explain to me what the above code means and where can I get reference to portal?
Laravel does a lot of magic, in your case it seems to be a Query Scope, from laravel documentation:
Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with scope
This is their example:
class User extends Eloquent {
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeWomen($query)
{
return $query->whereGender('W');
}
}
and it is called as follows:
$users = User::popular()->women()->orderBy('created_at')->get();
So in your case, you should have a method called portalScope in either the given class, or a parent one.
You can read about query scopes here
Related
I have a question ,
public function addNote(makenote $note) {
return $this->makenote()->save($note);
}
why there is note var on save method ?
what does it do?
In this case I believe $note refers to the instance of a related model that you want to save.
public function addNote(MakeNote $note) {
return $this->makenote()->save($note);
}
I would assume that on your Model there is a method that looks something like this. This method tell Eloquent that there is a model that is related to your current model.
public function makenote()
{
return $this->hasMany('App\MakeNote');
}
If you do not pass an instance of MakeNote, the $note in this case, there will be nothing to relate to your current model, thus nothing to save.
I'd really go check out the documentation on Laravel also if you want more tutorials Laracasts is a great resource.
Your question is getting down-voted too, because you should include more information about your question and some more examples of your code.
Well, I've started with Laravel just a few weeks ago, so sorry if I'm repeating something obvious but... here's the thing:
I've got a couple of query scopes in a Photo model:
public function scopeSkipFirst($query)
{
return $query->where('id', '>', 1);
}
public function scopeSearch($query, $search)
{
return $query->where('title', 'LIKE', "%$search%");
}
Now, I want the first one to be executed everytime I make an Eloquent query by that model, like in Photo::all(); and I want the second query scope to be available to any other model.
What's the best practice way to do this? Are both scenarios global scopes? I've been reading a few posts (like this one), but I have no clear ideas about which documentation should I refer (Laravel's 4.2 # Global scopes section; 5.1 Eloquent's # Events; ¿?).
If you want all of your models to have a scopeSearch() method, then it would make sense to move it to a trait and then apply that trait to your models. Something like Searchable:
trait Searchable
{
public function scopeSearch($query, $search)
{
return $query->where($this->getSearchField(), 'LIKE', "%$search%");
}
protected function getSearchField()
{
return 'title';
}
}
I’ve also made the column configurable as not all models may have a title column. For example, when I create an Article model in my applications I’ll have a headline column instead of title.
With the above trait, you can make a model searchable by implementing the trait:
class Photo extends Model
{
use Searchable;
}
You don’t want to make it a global scope. Global scopes are applied to every query. Not every query is going to be a search query, and there also won’t be anything to pass as a search query.
The scopeSkipFirst() method, could be made a global scope if you wanted that to apply any time you queried your Photo model, but I can’t think of a reason why you would want to always skip a particular record. Why have it in the database if you never want to display it?
My Association model looks like this (irrelevant code redacted):
class Association extends Model
{
public function members() {
return $this->hasMany('App\Member');
}
}
My Member model looks like this:
class Member extends Model
{
public function scopeActive($query) {
return $query->where('membership_ended_at', Null);
}
public function scopeInactive($query) {
return $query->whereNotNull('membership_ended_at');
}
}
This is what I want to be able to do:
$association = Association::find(49);
$association->members->active()->count();
Now, I'm aware there's a difference between a Query and a Collection. But what I'm basically asking is if there's some kind of similar scope for collections. Of course, the optimal solution would be to not have to write TWO active methods, but use one for both purposes.
(question already answered in the comments, but might as well write a proper answer)
It is not possible to use a query scope in a Colletion, since query scope is a concept used in Eloquent to add constraints to a database query while Collections are just a collection of things (data, objects, etc).
In your case, what you need to do is to change this line:
$association->members->active()->count();
to:
$association->members()->active()->count();
This works because when we call members as a method, we are getting a QueryBuilder instance, and with that we can start chaining scopes to the query before calling the count method.
Here's an example, using Eloquent in Laravel.
Let's say I'm working on a CMS.
the controller takes the route and looks up the page via the route.
the model provides a static function that uses the route to figure out the id of the row it's looking for
the model then uses itself to perform the database query and returns the result
Example Controller Code:
Route::get('(.*)', function($route)
{
$page = Page::load_by_route($route);
});
Example Model Code:
class Page extends Eloquent {
public static function load_by_route($route)
{
// Explode the route and trace to find the actual id of the row we need.
// ... some lines of code to accomplish it...
// Use the $id we discovered to perform the actual query
$page = Page::find($id)->first();
return $page;
}
}
Before you ask "Why can't you just use Page::where('route', '=', $route)->first() in the first place: I'm not wondering 'how to do' this example. I'm just wondering if it is it bad to to be using Page:: inside the page model?
No, but convention says to use self to reference the current class:
$page = self::find($id)->first();
I am very new to codeigniter but understand OOP and MVC as I do a lot of Rails development. One thing I haven't figured out yet is how to write a class level method in codeigniter and access it in a controller. For example, I have
<?php
class User_model extends Model {
function user_model()
{
parent::Model();
}
public static function get_total_users_count(){
$results = $this->db->query("SELECT * FROM bhr_users GROUP BY userid");
if($results){
return $results->num_rows();
}
return 0;
}
}
?>
I think what I have done here is established a class level method for my model that I should be able to call with User_model::get_total_users_count() Now in my controller which a previous programmer called "Welcome" I have something like:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
$this->load->model('bhr_model');
$this->load->model('user_model');
}
function index()
{
$invite = $this->uri->segment(3);
if($invite == 'invitefriends') {
$pagedata['invitefriends'] = $invite;
} else {
$pagedata['invitefriends'] = '';
}
$pagedata['numberofpeople'] = User_model::get_total_users_count();
$this->load->view('default_page', $pagedata);
}
}
The above method call to get_total_users_count does not work because it says because I am using the db method on a class level function in get_total_users_count. In other words $this has no db method when I reference a class.
So now my question is a bit more theoretical. I always thought that instance methods should only be used when a method is acting on a specific instance of an class. Makes sense, right? However, get_total_users_count is acting on all "users" and counting them. It just seems like that should be a class level method. Do you agree? If do, do you know how I can access the database from withing the framework inside a class level function?
Thanks!
Since you are not instantiating User_model, you must get the CI instance, then use that for your db queries.
Inside get_total_users_count():
$ci_ins =& get_instance();
$ci_ins->db->query();
You can make your class as a helper so it will not be load as a instance. Only the code will be included so you can just call it as:
$sample = class_name::method();
CodeIgnighter works is by instantiating your models as you load them. What Thorpe Obazee said is the correct codeIgnighter way to use your Model.
What you are asking is if you can use a static method as you'd expect in most circumstances, which just isn't how CI works.
To accomplish what you're after, mives points out get_instance() which is the correct way to get at the main CI object. I use that way myself to do what you're doing.
get_total_user_count is more of a function for a user table gateway.
User model should have things like getUsername and getLastLogin.
User Table Gateway should have things like findUserById, createNewUser, and getTotalUserCount
$pagedata['numberofpeople'] = $this->user_model->get_total_users_count();
That's the CI way.