1

The Route where I want to retrieve information about a User with id 6

Route::get('/', function() {
        echo '<pre>';
           var_dump(User::find(6)->where('person_id' == 1));
        echo '</pre>';
    });

It gives me:

object(Illuminate\Database\Eloquent\Builder)#176 (4) {
  ["query":protected]=>
  object(Illuminate\Database\Query\Builder)#175 (19) {
    ["connection":protected]=>
    object(Illuminate\Database\MySqlConnection)#178 (15) {
      ["pdo":protected]=>
      object(PDO)#177 (0) {
      }
      ["queryGrammar":protected]=>
      object(Illuminate\Database\Query\Grammars\MySqlGrammar)#192 (3) {
        ["wrapper":protected]=>
        string(4) "`%s`"
        ["selectComponents":protected]=>
        array(11) {
          [0]=>
          string(9) "aggregate"
          [1]=>
          string(7) "columns"
          [2]=>
          ...........

I only want the User object. The page gives me 44327 lines of text..

3 Answers 3

4

As far as I am concerned, you should use a function like get() that will return you those User objects.

Try :

$users = User::find(6)->where('person_id', 1)->get();
var_dump($users);
Sign up to request clarification or add additional context in comments.

Comments

1

You can only use primary id if you use find() or findOrfail().

For example: consider the following users table

id name email

1  AA   [email protected]
2  BB   [email protected]

here id is the primary key. if you want to retrieve data for id 1

$user = User::find(1);

var_dump($user);

So, you can see, your query was invalid

Try the following instead:

 var_dump(User::find(6));

or

var_dump(User::where('person_id', 1)->first());

Comments

0

In Eloquent the where method only returns a query object instance, in that moment the framework has yet to go to the database. To accomplish this you should use a trigger method as pointed out such as get() after the query or the first() method if you expect only one result.

there are of course, methods like find() that automatically search by primary key and execute the query right away.

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.