Frage

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..

War es hilfreich?

Lösung

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);

Andere Tipps

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

For example: consider the following users table

id name email

1  AA   aa@gmail.com
2  BB   bb@gmail.com

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());

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top