0

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" }

user2901304
  • 713
  • 1
  • 7
  • 15
  • 2
    I don't know laravel and so I can't help you with that, but can't you just limit their last name on output? I mean the query itself doesn't have to actually pull only the one character if you only display one character. Also, a quick google found this: http://stackoverflow.com/questions/16626702/how-to-make-query-with-substr-in-eloquent-laravel-4 – Jonathan Kuhn Jul 10 '14 at 21:48

2 Answers2

1

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();
scx
  • 2,749
  • 2
  • 25
  • 39
0

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?

Rapthera
  • 43
  • 1
  • 3