is it possible to call function in polymorphic way? - codeigniter

in codeigniter, is it possible to call function in polymorphic way? like:
public function abc(){ ...some code ...}
public function abc($para){ ...some code ...}
public function abc($para1,$para2){ ...some code ...}

That's not polymorphism, that's function overloading, and it's only kind of supported.

Related

Getting data from two separate models in laravel

I wanted to get data which is related to an id and I used the find($id) method to get those data, now I wanna get data from two tables which have one to many relationship.
How can I get data which is related to the same id from two table?
I try to this way but it hasn't worked:
public function show($id)
{
$post=Clients::find($id);
return view('pet.shw',['post'=>$post,'pets'=>$post->pets]);
}
Why you dont use with() I have simple solution but maybe not best solution:
Post::with('pets')->where('id',$id)->first();
Maybe below code is work to i dont test it:
Post::with('pets')->find($id);
Of course you should have comments method in your Post Object:
public function pets(){
return $this->hasMany(Pet::class);
}
hope help
You need to first define a relationship between your Client model and Pet model
So, in App\Client, you would have the following relationship:
public function pets()
{
return $this->hasMany(Pet::class);
}
and in App\Pet, you would have the following relationship:
public function client()
{
return $this->belongsTo(Client::class)
}
You should then be able to do this in your Controller:
public function show($id)
{
$post = Client::with('pets')->find($id);
return view('pet.shw')
->withPost($post);
}
and accesses your relationship like this in the pet.shw view:
foreach($post->pets as $pet) {}
For more information read about Eloquent Relationships

Serialize Eloquent collection append sometimes

I want to add value from accessor to collection in specific cases. There is array $appends for it, but I don't want to add the values always. Maybe is there a method like makeVisible for it?
Lets imagine I have models Book and Author:
class Book extends Model
{
protected $fillable = ['title', 'author_id'];
public function author()
{
return $this->belongsTo(App\Author::class);
}
public function getAuthorNameAttribute()
{
return $this->author->name;
}
}
And I want to return books collection with author name just call:
Route::get('books', function () {
return App\Book::all()
->appends(['author_name']);
});
And yes I'm able to use map() but I just find another (more beautiful decision)
Yes, Laravel eloquent has a method called makeVisible().
If you would like to make some typically hidden attributes visible on
a given model instance, you may use the makeVisible method. The
makeVisible method returns the model instance for convenient method
chaining.

Use Eloquent Model with more than one methods

Suppose I have a Model called ManageUser.
I have three methods. public static function registerusr(), public static function updateuser() and public static function removeuser().
In my UserController i called them like
$data=ManageUser::registeruser()
$data=ManageUser::updateuser()
$data=ManageUser::removeuser()
This is the fluent way to call the models methods.I am bit confused about Eloquent relationship over normal fluent query.I understand that its optimized the queries.
If i use Eloquent relationship how can i call the different methods in controller ?
You can use relationships inside any of your model methods. Just an example:
public static function updateUser($user, $roles)
{
$user->update(['status' => 1]);
return $user->roles()->sync($roles);
}
And call the method as you call it now:
ManageUser::updateUser($user, $roles);

Laravel + Eloquent - Passing through custom function first

What I want to do is quite simple I think. In my controller I have the code:
$list = SiteCategory::where('type','=','A')->get();
Which returns a standard eloquent collection object. However, sometimes when I retrieve categories, I want them to be ordered in a specific way first. So can I have some function in my model like:
Class SiteCategory extends Eloquent {
public function mySpecialFunction(){
// retrieve all categories, manipulate them in some way and return.
}
}
How do I then call this function? I don't understand, and the tutorials and questions I've read do not help. For example, in this question on SO, he seems to imply he can call his function something like this:
SiteCategory->mySpecialFunction()
I don't get it?
You can use a scope method inside your Model like this
Class SiteCategory extends Eloquent {
public function scopeMySpecialFunction($query){
// retrieve all categories, manipulate them in some way and return.
}
}
Then you can call this function like normal built in Eloquent functions like
SiteCategory::mySpecialFunction();
If you want to pass any arguments into this function then you can add parameters like this
public function scopeMySpecialFunction($query, $param1, $param2){
// retrieve all categories, manipulate them in some way and return.
}
Notice the first $query parameter, it's the first parameter of a scope method and $query is an instance of Illuminate\Database\Eloquent\Builder, you can use $this inside that function and can chain methods like
SiteCategory::mySpecialFunction()->find(1);
In this case you have to return the $query from your function to chain other methods, like:
public function scopeMySpecialFunction($query, $param1, $param2){
// do something
return $query;
}

Eloquent: Is chaining wheres (wrapped into functions) possible?

I have Project and Task models.
I want to do something like:
$project->tasks()->active()->get()
Where $project is a Project object, tasks() is a hasMany() relation, active() should be a function having return $this->whereCompleted(NULL);.
The question is if the whole idea is possible and where should I place the active() function?
You can use Query Scopes : http://laravel.com/docs/eloquent#query-scopes
You just have to put this method in your Task model :
public function scopeActive($query)
{
return $query->whereCompleted(NULL);
}

Resources