I have this relations:
Exercise - Muscle
Every Exercies can be related to multiple muscles and every muscle can be related to many muscles.
So they are defined as a ManyToMany relation.
Now i want to query all the muscles related to a particular exercies.
I tried something like this:
$muscle = $muscleRepository->find(9);
$c = new Criteria();
$c->where(Criteria::expr()->in('muscles', [$muscle]));
$res = $er->matching($c);
dd($res->toArray());
But this gives me a notice "Notice: Trying to access array offset on value of type null".
The notices appears whenever I try to do something with the LazyLoadCollection that comes from the ->matching call.
I've tried var_dumping and echoing things but the result is the same, so dd is not to be blamed.
I also tried contains and memberOf operators but with no luck.
Now I got things working writing an ugly workaround for this which involves removing the criteria and applying a ->filter function on the LazyLoadCollection but I would really like to handle this using Criteria class which seems to be made for this specific purpose.
Any idea?
Thanks
Related
I have some difficulties with eloquent models in use-case like that:
$skill1 = new Skill();
$skill1->title = "Test";
$skill1->save();
$skill3 = Skill::findOrFail($skill1->id);
$this->assertEquals($skill1, $skill3);
Asserting gives error, because objects are not equals
Comparasion result here
In case we getting object from relation there are lots of new fields (e.g. original->relation)
I know, that there is "is" method in Eloquent models and it works right, but if I need to use core PHP function like in_array (for example, it is using in Collection's "contains" method), comparasion will not work.
The question is "How to compare these two models and use it like that:"
$this->assertTrue(collect([$skill1])->contains($skill3));
Maybe I can overload compare operator, maybe I can make smthng like IComparable in C#?
Thanks.
Compare the attributes, not the whole models:
$this->assertEquals($skill1->getAttributes(), $skill3->getAttributes());
If you compare a newly created model with a model from the database, $wasRecentlyCreated is different (among other things).
I am developing a feature for an app using Laravel, and I am coming across this weird error that I can't figure out. I have the following code as a helper function to my controller, which was working just fine before I added a call to the following function:
protected function f($p){
$cIds = $cs->select('cs.id')->get();
$cs = DB::table('cs')
->select('cs.id')
->join('pucs', 'cs.id', '=', 'pucs.c_id')
->where('pucs.p_id', '=', (string)$p->id)
->whereIn('cs.id', $cIds)->lists('cs.id');
return $cs;
}
where pucs has a foreign key to cs and a column called p_id, and I only want to return rows where p_id = $p->id. $p is the result of a previous query, and $p->id is an int (I think?). When I open the page on my website, I get the following error:
Missing argument 1 for Illuminate\Database\Query\Builder::lists()
Does anyone have any insight as to what might be causing this problem? My initial thought was that $p->id was an int and that was causing the problem, so I cast it to a string using the (string) operator, as well as trying strval($p->id). I'm out of ideas.
UPDATE: using pluck('cs.id') gives the same error. I thought the issue might have been (string)$p->id, but when I comment out ->where('pucs.p_id', '=', (string)$p->id) I still get the same issue. For reference, I have a very similar line of code in the same helper file:
$bIds = $bs->select('bs.id')->get();
$bs = DB::table('bs')
->whereIn('bs.id', $bIds)->lists('bs.id');
I'm pretty sure it was working before, even while I was getting errors on the earlier piece of code, but now it's not working either.
Anyway, the reason I'm doing this is because I have these awkward tables that have been joined and unioned with other tables, and then selected a subset of cs.id (or bs.id) from that, and I want just the result of the original tables. Also, I was getting some weird errors because MYSQL apparently has a problem sorting unioned tables by id?
When using lists(), you need to pass in the name of the key you want the values for. Normally you would call get() here instead of lists(), but if you call lists('cs.id') then you should get an array of the matching cs.id's.
I have an object, Brand, and I want to print the id of this object.
I am getting the following error when doing return Sentry::getUser()->brand()->get()->id:
Undefined property: Illuminate\Database\Eloquent\Collection::$id
However, if I remove the ->id part, I am getting the whole object just fine, including the id (return Sentry::getUser()->brand()->get())
What am I doing wrong?
You need to use:
return Sentry::getUser()->brand()->first()->id;
Otherwise, you end up with a collection of users (even though that collection may only contain one user).
it might be reserved in a framework you used. My guess s that every object of any kind is a default object-type object that has an id of its own. If possible change the "id" to brand_id or something like that.
If there is only one object and will always be one, you can use return Sentry::getUser()->brand()->first()->id. That retrieves a single object, while get () returns an array of objects even if there is only one match.
I have a query in Laravel that needed to be upgraded. To get things working fine I had to use
DB::select(DB::raw());
instead of
Model::where()->get();
Now the result datatypes differs from the original one which occurs in several errors later in the application.
The simplest way would be to parse the recieved array from $y to look exactly like $x
On the left you see the debugged values on the right (underlined) the used code which the variables result from:
To keep it simple: How to get $y look like $x?
=> Where $y is tagged as {array} I need it to be tagged as {Illuminate\Database\Eloquent\Collection}
You may try this (be careful to sql injection)
$collection = new Illuminate\Database\Eloquent\Collection(DB::select(DB::raw()));
However, Eloquent and Query Builder (ORM generally) makes it incredibly easy to interact with a database, so what query exactly are you say "its not possible" with Model?
I started to use MongoDB 2.4.4, and I have a very iritating case for query-ing some post, by field in php.
In the mongoshell, the db.posts.find({page_id:345671} (for example) gives me a 293 count of document.
The php equivalent:
$connection = new Mongo('mongodb://localhost:27017');
$db = connection->selectDB('post_db');
$posts = $db->posts->find(array('page_id' => 345671));
Alway return a zero, but, when the a find array is empty, it gives back the entire collection.
Also, ->explain() and .explain() gaves me different params.
What am I do wrong? There's no sharding, no indexes, just some test data, i'm in the begining of the things.
SOLVED, many thanks to Vitaly Muminov!
"i'm not quite sure about that, but you can try setting ini_set('mongo.native_long', 1); or wrapping your numbers into MongoInt64 class"
The solution is wrap the number to MongoInt64!