Can anybody please help me to understand why the following code is working
$x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', 'IT' );}))->get() ;
but when I am using a dynamic value
$id='11';
$x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', $id );}))->get() ;
is saying
Method Illuminate\View\View::__toString() must not throw an exception
In fact it's hard to say because you haven't showed here relevant code, but the problem with code:
$x = $widget->objGallery->galleryItems()->with(array('captions' =>
function($query){ $query->where('locale', $id );
}))->get();
is that variable $id is undefined here. You need to add use to use if in closure, so the code should look like this:
$x = $widget->objGallery->galleryItems()->with(array('captions' =>
function($query) use($id) { $query->where('locale', $id );
}))->get();
You should change your environment to local and have turned on debugging, probably you would then know about this problem. Probably when correcting this code as I showed you won't have the error.
You can't, if you want to query by a relationship you need to use whereHas.
This is, for the conception of eager loading, just clarify... With syntax fix on #Marcin NabiaĆek's response, that where in the "with" function works only on the eager loading data, not in the main query.
Related
I am putting together a small mailing application within my application, and have run into a strange error - even just following the instructions for the advanced queries. I need to get -just- the mailboxes that are named:
$CoreMailboxes = TableRegistry::get('CoreMailboxes');
$query = $CoreMailboxes->find()
->where(function (QueryExpression $exp, Query $q) {
return $exp->isNotNull('name');
});
$query->hydrate(false);
return $query->toArray();
This is a near duplicate, sans "hydrate: false", of the example in the Cake Cookbook. However, it's giving me an error of
Argument 1 passed to App\Model\Table\CoreMailboxesTable::App\Model\Table\{closure}() must be an instance of App\Model\Table\QueryExpression, instance of Cake\Database\Expression\QueryExpression given
The query in the Cookbook is this:
$query = $cities->find()
->where(function (QueryExpression $exp, Query $q) {
return $exp->isNotNull('population');
});
What am I doing wrong?
You do not need to use the query expression for such a simple query..
You can just put the 'IS NOT NULL' in the where...
Now to re-use the query and create a more usable finder(), expressions may be more useful
$result = $this->Table->find()
->where([
'TableName.column_name IS NOT NULL'
])->toArray();
The problem is the instance definition's of your first argument, the doc is clear:
The passed anonymous function will receive an instance of \Cake\Database\Expression\QueryExpression as its first argument, and \Cake\ORM\Query as its second
Maybe you dont set the correct namespaces of this class, try this:
<?php
use \Cake\Database\Expression\QueryExpression as QueryExp;
//more code
//more code
->where(function (QueryExp $exp, Query $q) {
//more code
I've encounter today same error.
Try to add
use Cake\ORM\Query;
use Cake\Database\Expression\QueryExpression;
at beginning of your controller. It's help in my case.
I also try kip's answer but it doesn't work in my case
I've followed the instructions on the Laravel documentation for pagination with appends([]) however I'm having a little trouble with the persistence of these parameters.
Say for example, I pass home?category=Cars&make=Tesla to my view. What is the best way to paginate with them Get requests?
Right now I've passed the category as a parameter to the view as (where category is the model i've grabbed findOrFail with the request('category');)
$category_name = $category_model->name;
And then in my view it's like so:
{{ $vehicles->appends(['category' => $category_name])->links() }}
But when I go between pages in the pagination, this $category_name value doesn't seem to persist. Whats the recommended way to achieve what I want?
Thanks.
You can append the query string in your controller when you paginate the result. I'm not sure if that was your only question or even regarding applying the query string as a condition. So here is a sample showing you how to do both. This should give you an idea of how to do it. I just assumed the column names in this example.
$category = request('category');
$make = request('make');
$vehicles = Vehicle::when($category, function ($query) use ($category) {
return $query->where('category', $category);
})
->when($make, function ($query) use ($make) {
return $query->where('make', $make);
})
->paginate(10);
$vehicles->appends(request()->query());
return view('someview', compact('vehicles'));
I want to build a custom find function that retrieves bands for a given genre, i have tried this but the function can't access to the parameter $genre:
public function findGenre(Query $query, array $options)
{
$genre = $options['genre'];
$bands = $this->find()->contain([
'Genres' => function($q){
return $q->where(['Genres.id' => $genre]);
}
]);
return $bands;
}
I can access the $genre outside the contain() method, but not inside it.
My question is, how can i pass the $genre var to the function($q) inside the contain method.
I found where the problem is, i had to use the keyword use after the function($q), so that part of the code will look like this
$bands = $this->Bands->find()->contain('Genres', function($q) use ($genre){
return $q->where(['Genres.name'=>$genre]);
});
Also,the contain() method returns all the data even if the bands don't belong to a genre, but when i replaced it with matching() it worked just fine.
I hope this will help anyone who is having a similar problem in the future.
I was facing same issue but now it's resolved. I will explain you step by step:
My tables are:
articles: id,name,status,created
tags:id,name,status,created
articles_tags: id,article_id, tag_id
my query is this:
I want to pass my $tag_data['slug'] in matching variable but
directly this variable is not working in query. So I put in simple
$uses variable and now it's working properly.
$uses = $tag_data['slug'];
$contain_article = ['Tags'];
$query = $this->Articles->find('All')
->where(['Articles.status' => '1'])
->contain($contain_article)
->matching('Tags', function ($q) use ($uses) {
return $q->where(['Tags.slug' => $uses]);
});
Please try this :-)
I get some values from laravel query builder. I would like to get and set these values into somewhere of my code. So i get undefined index exception message with the following codes :
laravel query :
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->get();
I want to reach the values set in $generatedRowValue variable.
P.S. : Yeah i can get the values from the query, checked it out with var_dump(); dd(); etc. and i know my values are not undefined.
I'm new on this platform so i don't know how to reach these values.
Any help would be appreciated.
---UPDATE---
This is a simple HTML file which includes php partially.
<?php
$html = new simple_html_dom();
$html->load('<input type="text" value="'.$generatedRowValue['thevalueName'].'">')
?>
So i need $generatedRowValue['thevalueName'] 's value.
get() returns an array of objects. I think you want to use first() instead since you only expect one result.
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->first();
By the way, you can also use the object syntax in your view (if you want)
$generatedRowValue->thevalueName
I'm trying to do a mass update on an eloquent collection.
So I have my query, which looks a bit like this:
\Responder::with('details')
->where('job_number', $project->job_number)
->where('batch_id', ((int) $batch_id) - 1)
->where('updated_at', '<=', $target_time)
->whereHas('transactions', function($q) {
$q->where('status', 'success');
}, '<', 1)
->whereHas('details', function($q) {
$q->where('email', '<>', '');
});
This query object is stored as $query (because I'm re-using it - the same reason I dont want to switch how I'm doing the query), I am then performing an update on the collection, e.g.
$query->update(array('batch_id' => $batch_id));
This works great except it updates all the 'updated_at' timestamps. Now i like the timestamps, they are used extensively elsewhere, so i cant turn them off all together but I thought I could disable them temporarily but I've tried the following:
$query->timestamps = false;
$query->update(array('email_drop_off_index' => $batch_id));
and I can confirm that doesn't work, is there a way to do this?
Any help much appreciated
timestamps = false should be made on your model, but what you are doing is setting the value on the query builder. That's why it is not being picked up.
timestamps is an instance variable so you can't set it statically, and I don't think there is a built-in way to do it from the query builder. So I suggest try instantiating the model first, then create a new query from it, like this:
$responder = new \Responder;
$responder->timestamps = false;
$query = $responder->newQuery()
->with('details')
->where('job_number', $project->job_number)
...; // the rest of your wheres
$query->update(array('email_drop_off_index' => $batch_id));
Here's a possible solution: subclass your Responder model and turn off timestamps in the subclass.
class MassUpdateResponder extends Responder
{
public $timestamps = false;
}
Then use your new class to do the updates. This seems like a bit of a hack, but it should work.
BTW, doing an update like the following worked for me:
$query->timestamps = false;
$query->value = "new value";
$query->save();
The update() method may be doing something different that's causing it to ignore the value of $timestamps.