php - Laravel print error if bad query -


i'm laravel newbie, have written simple code:

route::get('users/{uname}/{uid}', function($uname, $uid) {     $u = db::table('users')->where('login', $uname)->first();      return view::make('users',          array(             'username'  => $u->login,             'userid'    => $u->uid         )     ); }); 

i have 2 questions regarding code,

first: code valid?

second: how can show 404 error when query not found? example if write site.com/users/badusername/badid show 404 error, not blank page.

thanks in advance !

p.s how can make $u query 2 wheres? example login = ? , uid = ?

first: code looks ok. make use of little un-documented feature of dynamic methods, , call that:

db::table('users')->wherelogin($uname)->first(); 

second: in order throw 404 error, can call

app::abort(404); 

third: can chain where() methods, e.g.:

$u = db::table('users')->where('login', $uname)->where('uid', $uid)->first(); 

the query above doesn't make sense though, uid , username unique , fetching 1 of them should enough. if have eloquent model users, use model's find() or findorfail() methods search primary key. findorfail() useful, throws 404 exception if model cannot found, no need wrap in if statement.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -