Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to cast ObjectId in Model, instead in where condition #2752

Closed Unanswered
masterbater asked this question in Q&A
Discussion options

Is this correct? Its not working. but wrapping it

not working

WorkoutLogs::where('createdBy', '5be9ce7b3de6dd77db832950')->orderBy('createdAt', 'desc')->paginate(10);
protected $casts = [
 'createdBy' => ObjectId::class,
 ];

this works

WorkoutLogs::where('ruleType', 'Vitals')->where('createdBy', new BSONObjectId('5be9ce7b3de6dd77db832950'))->orderBy('createdAt', 'desc')->paginate(10);

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

It's working fine for me.

$stringId = (string) (new ObjectId());
Client::query()->create(['cclient_id' => $stringId, 'name' => 'Young Gerald']);
Client::query()->where('cclient_id', $stringId)->first();

What version you are using?

You must be logged in to vote
2 replies
Comment options

I need to do an auto casting fields into ObjectId, but they dont have it so I just use BSONObjectId to cast objectId, I do dynamic fields for where conditions so I just detect

 if (is_string($value) && preg_match('/^[0-9a-f]{24}$/i', $value) === 1) {
 // Check if the value is a valid BSONObjectId string and cast it
 $value = new BSONObjectId($value);
 $query->where($id, $value);
}
Comment options

While replicating this issue in a fresh project, please consider a not-reserved name for your id field. (the createdAt is reserved. try somethingCreatedAt)

Comment options

When querying the set wont be called, for solution, we just need to modify

$objectId= $this->convertToObjectId($objectIdString); //Supports conversion of string, array of string or collection of string
Client::query()->create(['cclient_id' => $objectId, 'name' => 'Young Gerald']);
Client::query()->where('cclient_id', $objectId)->first();

No checking of its valid objectid string since most of the source of objectid is from the collection itself

<?php
namespace App\Traits;
use MongoDB\BSON\ObjectId;
trait ObjectIdConversion
{
 public function convertToObjectId($value)
 {
 // Check if the value is an instance of ObjectId and skip conversion
 if ($value instanceof ObjectId) {
 return $value;
 }
 // Check if the value is a Collection
 if ($value instanceof \Illuminate\Support\Collection) {
 // Convert Collection to an array
 $value = $value->toArray();
 }
 // Check if the value is an array
 if (is_array($value)) {
 foreach ($value as $key => $item) {
 // Recursively call the function if the item is an array
 $value[$key] = $this->convertToObjectId($item);
 }
 return $value;
 } else {
 // Convert the value to an ObjectId
 return new ObjectId($value);
 }
 }
 public function convertObjectIdToString($value)
 {
 if ($value instanceof \Illuminate\Support\Collection) {
 // Convert Collection to an array
 $value = $value->toArray();
 }
 // Check if the value is an array
 if (is_array($value)) {
 foreach ($value as $key => $item) {
 // Recursively call the function if the item is an array
 $value[$key] = (string) $item;
 }
 return $value;
 } else {
 // Convert the value to an ObjectId
 return (string) $value;
 }
 }
 
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /