0

I have a laravel fetching controller like below,

public function fetchbywhere()
{

    $result=User::find(2);
    return View::make('fetchbywhere')->with('resultrow',$result);

}

My Model file of User.php like,

 use Illuminate\Auth\UserTrait;
 use Illuminate\Auth\UserInterface;
 use Illuminate\Auth\Reminders\RemindableTrait;
 use Illuminate\Auth\Reminders\RemindableInterface;

 class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;
protected $table = 'users';
protected $hidden = array('password', 'remember_token');

}

and my routes.php file ,

Route::get('select-by-where',array('as'=>'select_by_where','uses'=>'Test@fetchbywhere'));

and my view file named fetchbywhere.blade.php,

 <title>Fetch by Where clouse</title>

 <h2>Users - Where clouse</h2>

@foreach($resultrow as $data)
<ul>
  <li>{{$data->name}}</li>
</ul>
@endforeach

my database have fields like

id|name|email|age|created_at|updated_at

am getting error like Trying to get property of non-object (View: E:\wamp\www\new_laravel\app\views\fetchbywhere.blade.php)

anybody please give me a solution. t

thanks

2
  • Do you know what a non-object means, and what it means to fetch a property of a non object? Commented Nov 28, 2014 at 14:33
  • no. can u please describe me, Commented Nov 28, 2014 at 14:34

1 Answer 1

2

You are using User::find(2) this returns a single instance of User with id = 2. Or even null if it doesn't exist. In your view you are iterating over $resultrow as if it where a collection and not a single model.

I believe what you actually want to do is something like this:

$result=User::where('some-column', 'operator', 'some-value')->get();
return View::make('fetchbywhere')->with('resultrow',$result);

So an example could be...

$result=User::where('age', '>', '18')->get();
return View::make('fetchbywhere')->with('resultrow',$result);

...to get everybody over 18


Also its "where clause" not clouse ;)

Update

The actual use for the find method is to get only one object with the passed id (primary key in the database). It doesn't return a collection (list) because there no way it could find multiple rows with the same id, since the primary key is unique.

If you only want one user (by a specific id) you have to change your view to directly access the model

<title>Fetch by Where clouse</title>

<h2>Users - Where clouse</h2>

{{ $resultrow->name }}
Sign up to request clarification or add additional context in comments.

3 Comments

great !, its working, but i see in laravel official documentation like amy question. what is the actual use of the find(1) in laravel, can you please...
@Jishad the find(1) means the query will get the user where id = 1 in the database
but i have a primerykey 1 as in my table, then why it is not getting output, ?

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.