user2901304
user2901304

Reputation: 713

Limit Eloquent database results

I am trying create a query to output all my users but limit thier last name to one character for privacy reasons. I can seem to find a clean way to do this through Laravals Eloquent model.

public function getAll(){
    $users = User::get(array('id', 'fname', 'lname'));
    return $users
}

Desired response: { id: 1, fname: "Luca", lname: "D" }

Upvotes: 0

Views: 63

Answers (2)

Rapthera
Rapthera

Reputation: 43

Can't you just do:

public function index()
{
    $mymodel = MyModel::all();

    return View::make('MyModel.index')->with('MyModel', $mymodel);
}

And then have a view which just foreach between them, if this is what you're trying todo

@foreach($mymodel as $key => $model)
{{ $model->username }}
@endforeach

isn't this it what you're trying todo render all users on a page?

Upvotes: 0

scx
scx

Reputation: 2789

You can try using Laravels raw expresions with LEFT SQL function.

 $users = DB::table('users')->select(DB::raw('id, fname, LEFT(lname,1) as last_name'))->get();

Upvotes: 1

Related Questions