Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel query builder returns object or array?

I'm building a very simple web app with Laravel.

I've built two separate Controllers, which each return two separate views, as follows:

ProfileController:

class ProfileController extends BaseController {

    public function user($name)
    {
        $user = User::where('name', '=', $name);

        if ($user->count())
        {
            $user = $user->first();
            $workout = DB::table('workouts')->where('user_id', '=', $user->id)->get();

            Return View::make('profile')
                    ->with('user', $user)
                    ->with('workout', $workout);
        }

        return App::abort(404);
    }
}

WorkoutController:

class WorkoutController extends BaseController {

    public function workout($name)
    {
        $workout = DB::table('workouts')->where('name', '=', $name)->first();

        if ($workout)
        {
            Return View::make('add-exercise')
                    ->with('workout', $workout);
        }

        return App::abort(404);
    }
}

What is confusing me is what I had to do in order to pass a single workout object to each view. As you might have noticed the query builders for workout are different:

$workout = DB::table('workouts')->where('user_id', '=', $user->id)->get();

and

$workout = DB::table('workouts')->where('name', '=', $name)->first();

On the profile view, I get an object using the ->get(); method, but on the add-exercise view, I must use ->first(); or I will otherwise get an array with only one index, where I can then access the object, i.e. $workout[0]->name instead of $workout->name.

Why is this? Shouldn't I be able to use either get and/or first in both controllers and expect the same type of result from both since I want the same thing from the same table?

like image 579
Tiago Avatar asked Jan 27 '15 17:01

Tiago


People also ask

How does Laravel query builder work?

In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.

What is the difference between query builder and eloquent ORM?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.

What does Laravel get method return?

Model::find() returns either an instance of Model or a collection containing one or more instances of Model .

What is query Builder in Laravel with example?

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems.


2 Answers

get() returns a collection of objects every time. That collection may have 0 or more objects in it, depending on the results of the query.

first() calls get() under the hood, but instead of returning the collection of results, it returns the first entry in the collection (if there is one).

Which method you use depends on what you need. Do you need the collection of all the results (use get()), or do you just want the first result in the collection (use first())?

like image 91
patricus Avatar answered Oct 26 '22 16:10

patricus


  • Model::find(numeric); returns a object
  • Model::whereId(numeric)->first(); returns a object
  • Model::whereId(numeric)->get(); - returns a collection
  • Model::whereId(numeric); - returns a builder
like image 38
joseph roxas Avatar answered Oct 26 '22 16:10

joseph roxas