Laravel clain Eloquent Query cannot return an array of results

I am trying to make a chained Eloquent request to Laravel and bring users from the user table with a conditional, namely, if they belong to a certain house. I'm struggling a bit to find a way to make this work. Can anybody help me?

This is my controller method:

public function create(){

        if(Auth::user()->role->id == 1){

            $house = House::findOrFail(Auth::user()->house->id);

            $jobs = Job::pluck('name', 'id')->all();
            $categories = Category::pluck('name', 'id')->all();
            $users = User::pluck('name', 'id')->where('house_id', $house)->get();

            return view('admin.tasks.create', compact('jobs', 'categories', 'users'));

        }else{

            return redirect('home');

        }

    }

The error was passed to me when using get ():

Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /Applications/MAMP/htdocs/Housing_around/app/Http/Controllers/AdminTasksController.php on line 55 and at least 1 expected

And all()at the end , they thought nothing returned to me in the returned array

Does anyone have a clue on how to proceed?

Thank!

+4
source share
3 answers

, . , , .

( ) ()

Laravel.

pluck() - , get(), . all() vs get() .

, $users = User::where('house_id', $house)->pluck('name', 'id');

+2

, :

$jobs = Job::pluck('name', 'id');
$categories = Category::pluck('name', 'id');
$users = User::where('house_id', $house)->pluck('name', 'id');
+3

get() pluck()

$jobs = Job::pluck('name', 'id');
$categories = Category::pluck('name', 'id');
$users = User::where('house_id', $house)->pluck('name', 'id');
+2

Source: https://habr.com/ru/post/1693076/


All Articles