0

I'm laravel newbie, I have written a 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 this code,

First: Is this code valid?

Second: How can I show 404 error when query is not found? For example if I write site.com/users/badusername/badid then show 404 error, not blank page.

Thanks in advance !

P.S how can i make $u query to two wheres? For example WHERE login = ? AND uid = ?

3 Answers 3

3

First: code looks ok. You could also make use of a little un-documented feature of dynamic where methods, and call it like that:

DB::table('users')->whereLogin($uname)->first();

Second: In order to throw 404 error, you can simply call

App::abort(404);

Third: You can chain the where() methods, e.g.:

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

The query above doesn't make much sense though, as uid and probably username as well are unique and fetching just by one of them should be enough. If you have a Eloquent model for your users, you could also use model's find() or findOrFail() methods to search by primary key. Especially findOrFail() is very useful, as it throws 404 exception if model cannot be found, so no need to wrap it in the if statement.

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

Comments

0

Assumming you have User model. Try:

Route::get('users/{uname}/{uid}', function($uname, $uid)
{
    $u = User::where('login', $uname)->where('uid',$uid)->first();

    if(!$u->isEmpty()) {
          return View::make('users',array('username'  => $u->login,
                                          'userid'    => $u->uid));
    }else{
         App::abort(404);
    }
});

3 Comments

If you're using Eloquent the best way is to use findOrFail. In this case firstOrFail would be more appropriate.
Yes, I know but I use it intentionaly not to mess with caching an exception - to make code cleaner and simplier as OP said he is newbie
Well your way is not simple neither is it cleaner. I was just improving your answer with better ways. Also the OP didn't mention using Eloquent you're making an assumption so you're still likely to be wrong.
0

Yes, you're code is valid, but you can simplified it.

get('users/{uname}/{uid}', function($uname, $uid)
{
    $user = User::where('login', $uname)->where('uid', $uid)->first();

    if (! $user) abort(404);

    return view('users', compact('user'));
});

To use this code you need to use Laravel 5+ and to have a model for your User table.

Also, as you can see, I have write another condition to handle the uid.

Note that I send $user to the view.

2 Comments

It won't throw an exception. It will return null if a matching model is not found. Only eloquent findOrFail() throws exceptions
Correct! Laravel 4 automatism... Thanks, I've edited my answer.

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.