Add items to paginator laravel 4 - php

So i have a problem when i want to add element to Paginator collection, i got this error :
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'add'
This is my code :
$data = $users->whereBetween('program_date',[$from,$to . ' 23:59:59'])->paginate(10);
$tests = $test->whereBetween('created_at',[$from,$to . ' 23:59:59'])->paginate(10);
foreach ($tests as $sms) {
$camp = new Campagne();
$camp->user_id = $sms->user_id;
$data->add($camp); // i got error in this line!!
}
so please if someone has any idea how to use ->add($element) with laravel pagination i will be very appreciative.

I don't think Collection has a function called add. also I am not sure you can loop through, at least not the models, with straight from Paginator. I believe you need to change it to
foreach( $tests->getItems() as $sms ){
.....
}
the reason it won't work this way is when you paginate a collection you get an instance of Illuminate\Pagination\Paginator class and when you loop through, you are basically looping class object not your items.

Related

How do I filter item(product) with multiple parameter in MongoDB / PHP?

I am using MongoDB with Laravel (php framework).
Using post method I am sending filter parameter to controller function.
ItemController.php
public function search_item(Request $request){
$item = $request->all();
$item = $this->item->search_item($item);
return response()->json($item,200);
}
Item.php (model file)
public function search_item($item){
$items = new item();
foreach($item as $key => $value) {
$items = $items::where($key, '=', $value)->get();
}
$items = json_decode(json_encode($pro));
return $items;
}
If I pass only one parameter then it's give me result perfectly but if I pass more then one parameter it's give me an error.
Non-static method Illuminate\Support\Collection::where() should not be called statically
Let me explain with example :
URL : http://localhost/use_good_store/public/search-item?brand_name=Bajaj
If I post above url it's give me perfect result but if I pass more then one parameter like
http://localhost/use_good_store/public/search-item?brand_name=Bajaj&model=Boxer it gives me above error.
There are a couple of things I would change in this code.
First, to address the error you're getting, you're accessing your search method incorrectly. It's an instance method, not a class (or static) method.
So instead of $this->item::search_items($item), it should be $this->item->search_items($item).
But I wouldn't recommend that. Since you're using the model anyway, go ahead and store a static search method on it for future use. It doesn't make much sense for it to be an instance method; you can't really call it on an instance because the point of it is to find many other instances.
Additionally, your query probably isn't going to work out the way you want since you're continually replacing the $items value in the for loop. if you're going to be using the QueryBuilder, you can just keep adding where() clauses to it all day long.
For this, I'd recommend adding a static searchItems() method that returns the results of a query which you would then convert to JSON in your controller, like so:
//Item.php model
public static function searchItems($itemSearchAttributes)
{
return static::where(function($query) use ($itemSearchAttributes){
foreach ($itemSearchAttributes as $key => $value){
$query->where($key, '=', $value);
}
})->get();
}
//ItemController.php handler method
public function search_item(Request $request){
$items = Item::searchItems($request->all());
return $response->json($items);
}
Worth noting; your search method here will exclude any records that do not match -all- of the provided key/values.
Try to make it like this :
$items = new item();
foreach($item as $key => $value){
$items = $items->where($key, '=', $value);
}
$items = $items->get();

Illuminate Eloquent manipulate result

In Eloquent 5.2.7 I was able to add to the result set using
foreach($page as $xp => $xv) {
$content[0]->{$xp} = $xv;
}
Where $content was a DB::table()->get() result.
Now, on the latest version I get the following errors:
Indirect modification of overloaded element of Illuminate\Support\Collection has no effect in
and
Undefined offset: 0 in /vendor/illuminate/support/Collection.php
I think I understand the why, and it's to do with PHP and ArrayAccess and offsetGet but I cannot figure out how to effectively do what I am doing "the right way"
You can also use the collection map() method to loop through and modify the results:
DB::table()->get()->map(function($content) use ($page) {
foreach($page as $xp => $xv) {
$content->{$xp} = $xv;
}
return $content;
});
After some more debugging, looks like I can manipulate the data like that, I just cannot pass it empty data - will need to check for empty arrays.
Also in order to display I needed to add $content->all().

Laravel 5 - Updating associated models using push()

I am trying to do this:
$volunteer = Volunteer::with('user')->find($id);
$input = $updateVolunteerRequest->all();
$volunteer->fill($this->fillFields($input));
$volunteer->user->fill([
'email' => $input['email']
]);
$volunteer->push();
But push() method does not seem to work.
It throws the following error:
FatalErrorException in Model.php line 1463:
Call to a member function push() on a non-object
It worked in Laravel 4. Is there a new way of doing this in version 5? Or am I doing something wrong.
I checked $volunteer and it returns the model.
The push method loops through all of the loaded relationships on the model and calls push on them, as well. So, it isn't the push on the $volunteer that's failing, it is a push on a related model that is failing.
In Laravel 4, push has the following code:
foreach ($this->relations as $models)
{
foreach (Collection::make($models) as $model)
{
if ( ! $model->push()) return false;
}
}
In this code, if the relationship returned a NULL, there would be no error. Collection::make(NULL) returns an empty array, so the foreach would never execute. However, in Laravel 5, push has this code:
foreach ($this->relations as $models)
{
$models = is_array($models) ? $models : array($models);
foreach ($models as $model)
{
if ( ! $model->push()) return false;
}
}
In this case, if the relationship is null, there will be an error, since array(NULL) returns an array with one entry: the NULL value. So, the foreach loop will execute, and it will try to call push on the NULL value, resulting in your Call to a member function push() on a non-object error.
So, it looks like this is a bug in Laravel 5.
Edit
It looks like the push() method was updated because of an update made to the Collection object. I have submitted a pull request to correct the issue. You can check out the pull request here, which also contains links to reported issues and other background information.
Edit 2
My pull request was merged in, so you should be able to update your Laravel 5 to the newest version and the issue should be corrected.

Zend Framework 2 Update Value of ResultSet Object

Zend framework 2 newb here so bear with me. See the code below:
public function setMediaToTrue($users) {
foreach($users as $user) {
$user->media = true;
}
return $users;
}
This method receive $users ResultSet object and update the media property to true. The problem is I can't modify the property of $users object inside iteration. Things that I have tried :
1. Use $users->current() inside iteration. example below :
$users->current()->media = true;
Create setMedia inside $user model and use it inside iteration:
$user->setMedia(true); // This does not update the property either
I think the problem because ZF2 create new copy of object for each iteration. I was looking at using Hydrator but can't get my head around it.
Thanks

Working with empty Doctrine 2 Association Objects

When creating an association using Doctrine 2 and the Zend Framework, if the associated object is empty e.g. for entity->associated_entity->item if associated_entity is empty, i.e. there is not an associated entity to the original entity, then I get an error Trying to get property of non-object.
I know this is because I am trying to get the item from an empty entity.
What is the standard way to avoid this error?
I am using the code below to get the data, but because the initial associated entity will be returned as '', then it can't then get the item from ''
public function __get($name)
{
if (isset($this->$name)){
return $this->$name;
} else {
return '';
}
}
you could try:
$associatedEntity = $entity->associated_entity;
if ($associatedEntity) {
$item = $associatedEntity->item;
}
Edit:
OK then. Try putting this in your template/view:
<?php
$department = $instruction->department;
if ($department) {
echo $department->department;
}
?>
Edit 2 (after a small discussion in the chat :D):
I think that there is no way to tell to PHP to stop the chain. E.g.
$object1->object2->attribute
If you write it this way, no matter what you put in the __get(), PHP will assume that object2 is an object and will try to fetch the requested attribute.
The easiest solution would be something like that:
<?php foreach ($this->data as $instruction) : ?>
<?php if ($dep = $instruction->department) echo $dep->department ?>
<?php endforeach ?>

Categories