I want to use an array of ids as a condition to my paginate function, but I get this error 'Cannot convert value to integer', I understand that a array is not an integer, but how could I make the condition look for all the values in the array.
$friendsid = explode(',', $this->Auth->user('friends'));
$this->paginate = [
'conditions' => [
'Users.id' => $friendsid,
]
];
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);
You could try it:
$friendsid = explode(',', $this->Auth->user('friends'));
$query = $this->Users->find()
->where(function ($exp, $q) use ($friendsid) {
return $exp->in('Users.id', $friendsid);
});
$this->set('users', $this->paginate($query));
$this->set('_serialize', ['users']);
Related
I'm not an expert of Collections concept in CakePhp 4, and I don't know how to pass a variable in Collection::map()
$item = [
'attributes' => [
'class' => 'mon-li-{{id}}',
'data-truc' => 'li-{{id}}'
],
'linkAttrs' => ['class' => 'mon-lien', 'style' => 'text-transform: uppercase']
];
$id = 5;
$item = collection($item)
->map(function ($value, $key){
return preg_replace('/{{id}}/', $id, $value); // $id is undefined
})
->toArray();
It gives : Notice (8): Undefined variable: id
How can I do for my function to be able to know $id ?
The use keyword helps with this:
$item = collection($item)
->map(function ($value, $key) use ($id) { // <-- See `use`
return preg_replace('/{{id}}/', $id, $value);
})
->toArray();
PHP Manual: Anonymous Functions
I tried to code a request with search system. Here the code:
$search = request()->get('search');
if(Auth::user()->hasRole('admin') || true)
{
list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
$prestations = Prestation::with(
'service:id,name',
'facility:id,name'
)
->orWhere('service:name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
}
The problem is that I don't know how to do something like I tried with the orWhere -> get all service name (from the relationship "with") which are equal to my $search.
Thank you.
Try this query.
$prestations = Prestation::with(
[
'service' => function($service) use($search){
$service->select(['id','name'])->where('name', $search);
},
'facility' => function($facility) {
$facility->select(['id','name'])
}
]
);
As you can see in my piece of code below, $similarImages is being passed to the view, however, it is possible that there are no other similar images which means I'll pass an undefined variable. This is why I need to pass 'similarImages' => $similarImages only if $similarImages exists.
$tag = Tag::whereHas('images', function($q) use ($id) {
return $q->where('image_id', $id);
})->first();
if (!empty($tag)) {
$tagId = $tag->id;
}
$recentImages = Image::where('user_id', $authorId)->orderBy('created_at', 'desc')->limit(9)->get();
if (!empty($tagId)) {
$similarImages = Image::whereHas('tags', function($q) use ($tagId) {
return $q->where('tag_id', $tagId);
})->orderBy('created_at', 'desc')->limit(9)->get();
}
return view('specificImage', ['image' => $image, 'recentImages' => $recentImages, 'similarImages' => $similarImages, 'author' => $author, 'comments' => $comments]);
Make use of the null-coalescing operator to have a default value:
[ .. 'similarImages' => $similarImages ?? collect(), .. ]
This will use $similarImages if it is defined and not null otherwise it will assign an empty collection created by collect().
I like the idea of it always being a collection, but that's just me.
$array is always pass to the view
and if isset $similarImages, just prepend the $similarImages into $array
if not isset $similarImages just return $array (in this case $similarImages won't exists in blade)
here also you can use array_add($array, 'similarImages', $similarImages)
$tag = Tag::whereHas('images', function($q) use ($id) {
return $q->where('image_id', $id);
})->first();
if (!empty($tag)) {
$tagId = $tag->id;
}
$recentImages = Image::where('user_id', $authorId)->orderBy('created_at', 'desc')->limit(9)->get();
if (!empty($tagId)) {
$similarImages = Image::whereHas('tags', function($q) use ($tagId) {
return $q->where('tag_id', $tagId);
})->orderBy('created_at', 'desc')->limit(9)->get();
}
$array = [
'image' => $image,
'recentImages' => $recentImages,
'author' => $author,
'comments' => $comments
];
if (isset($similarImages))
{
$array = array_prepend($array, $similarImages, 'similarImages')
}
return view('specificImage', $array);
I'm building a Products API. I need to return a collection of products and a variable telling me if I have more results starting from the last the answer has just returned me to show or hide a load more button. This is all I have until now:
$query = Product::query();
$query->where('category_id', $request->get('category_id'));
$query->orderBy('order', 'asc')
->orderBy('name', 'asc')
->skip($skip)
->take($take);
And this is how I return it:
return [
'products' => $query->get(['id', 'name', 'front_image', 'back_image', 'slug']),
'has_more' => ????
];
How can I calculate the has_more?
The easiest approach would be to query the database twice:
$query = Product::query()
->where('category_id', $request->get('category_id'))
->orderBy('order', 'asc')
->orderBy('name', 'asc');
$count = $query->count();
$hasMore = $skip + $take < $count;
$models = $query->skip($skip)
->take($take)
->get(['id', 'name', 'front_image', 'back_image', 'slug']);
return [
'products' => $models,
'has_more' => $hasMore
];
You can just get the count of the entire records and then simply do the check for has more like so:
<?php
$query = Product::query()
->where('category_id', $request->get('category_id'));
->orderBy('order', 'asc')
->orderBy('name', 'asc');
$count = $query->count();
return [
'products' => $query->skip($skip)
->take($take)
->get(['id', 'name', 'front_image', 'back_image', 'slug']),
'has_more' => ($hm = ($count - ($take + $skip))) > 0 ? $hm : false
];
I have this query:
Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->update(array(
'status' => 'stopped',
));
The problem is that the amount of records it has to go through to do the update causes it to take around 15 minutes or so to finish.
I would like to split it up so the select and update queries are separate entities. Something sort of like this:
$pending = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
$pending->update(array(
'status' => 'stopped',
));
How would I go about doing this? Or is there an easier way?
Thanks!
I wasn't thinking, I figured out the answer. I had to run the second part in a foreach like so:
$records = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
foreach ($records as $record) {
DB::table('sendqueue')
->where('ID', $record->ID)
->update(['status' => 'stopped']);
}
protected $table="user";
public function updateUser($id,$username)
{
$resultData = array();
$updateArray = array('user_name'=>$username);
$update=DB::table('user')
->where('user_id', $id)
->update($updateArray);
return $resultData['status'] = true;
}
$my_id = preg_replace ('#[^0-9]#', '', $request->id);
if (! empty ($my_id)) {
$this->c->where ('id', $my_id )->update ( [
'first_name' => $request->get ( 'first_name' ),
'last_name' => $request->get ( 'last_name' ) ,
'phone' => $request->get ( 'phone' )
] );`enter code here`
\Session::flash ('message', 'Update Successful');
return redirect ('customer');
}
$this->edit ();
http://developer.e-power.com.kh/update-query-in-laravel-5-2/