1

I've got the following:

$srv = DB::table('ads')
    ->join('ad_service','ad_service.ad_id', '=', 'ads.id')
    ->select('ads.id')
    ->whereIn('ads.id',[45789,46531])
    ->get();
    Log::info($srv);

and log info gives me

  49 =>   stdClass::__set_state(array(
 'id' => '46531',
  )),
  50 => 
  stdClass::__set_state(array(
 'id' => '46531',
  )),
  51 => 
  stdClass::__set_state(array(
 'id' => '46531',
  )),

What I would like to have:
return the entry ONCE, there are multiple entries with this id in the "ad_service" table , and i would like to retrieve it once. its a many to many relationship. also biggest problem, what is this stdclass set state?
I was expecting something like:

array('id' => '46531', ...,)

thanks guys.

EDIT: Solved it using Eloquent:

$srv = Ad::whereIn('id',[46696,48982,...MORE IDS HERE])->get();

this returns not an object but a collection itself. i do not really know what @ourmandave meant with "you get a collection with std class objects" i mean obviously i could see that but i do not really know why i got it, and why i get it as i wish using eloquent. Any answer to that would be appreciated: Why does DB:: return this std class stuff and Eloquent returns the "wanted format"?

2
  • What version of Laravel are you using? Commented May 4, 2019 at 17:15
  • Im using laravel version 5.2 in this project Commented May 4, 2019 at 17:29

3 Answers 3

1

If you just want one record you could try:

$srv = DB::table('ads')
        ->join('ad_service','ad_service.ad_id', '=', 'ads.id')
        ->select('ads.id')
        ->whereIn('ads.id',[45789,46531])
        ->first();

Change the get to a first(). The reason you are getting multiple records is because get() returns a laravel collection of your ads.

Sign up to request clarification or add additional context in comments.

1 Comment

what i need is 1 array , with 1x the entry. this one returns an error with "Object of class stdClass could not be converted to string"
1

You're actually getting back a lararvel Collection.

From the docs (https://laravel.com/docs/5.8/queries#retrieving-results)

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object. You may access each column's value by accessing the column as a property of the object:

foreach ($users as $user) {
  echo $user->name; 
}

There's a lot of methods they provide for collections to treat them like arrays. (https://laravel.com/docs/5.8/collections#available-methods)

You might also try the distinct() method to get back one of multiples.

  $srv = DB::table('ads')
    ->join('ad_service','ad_service.ad_id', '=', 'ads.id')
    ->select('ads.id')
    ->whereIn('ads.id',[45789,46531])
    ->distinct()
    ->get();

2 Comments

Currently getting: array ( 0 => stdClass::__set_state(array( 'id' => '45789', )), 1 => stdClass::__set_state(array( 'id' => '46531', )), ) Need it in this form though: { "key" => "val , "key2" => "val2" ...} any idea? :/
i mean { { "key" => "val" }, { "key2","val2"} }
0

To convert a model and its loaded relationships to an array, you should use the toArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays

$srv = DB::table('ads')
    ->join('ad_service','ad_service.ad_id', '=', 'ads.id')
    ->select('ads.id')
    ->whereIn('ads.id',[45789,46531])
    ->get()
    ->toArray();

Print array:

Log::info($srv);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.