Laravel Eager Loading - Load (Does weird things)

0

I'm trying to load the polls of a group, which they do show, but I also want to retrieve the name of who did that survey.

This is how I have my controller:

  public function index(Grupo $grupo)
    {   

        $grupo->load('Encuestas.users');

        return view('Encuesta.index',compact('grupo'));
    }

Well, for the surveys to be shown with respect to the user who created it.

in the model Encuesta

    public function users()
    {
    	return $this->belongsTo(User::class,'user_id');
    }

I do not understand why I should put user_id. but what I put does not work. "I do not know why" I did not have to do it like that in other things I had done ..

Well then, I make a return. And it shows me all the information, but when I show it in a blade view.

I show each survey- But at the moment of showing the name of the user who created that survey. I get a problem from

n + 1

which is a problem, then. I do not know how to solve that, since depending on whether it loads the information of the user who created that survey.

Then, I need to page it. Which also does not leave me

Then I decided to do this.

   public function index(Grupo $grupo)
    {   

        $grupo = Grupo::with(['encuestas.users'])
        ->where('id',$grupo->id)->first();

        return view('Encuesta.index',compact('grupo'));
    }

Now I do not have the n + 1 problem

but, I double consult, which also the Paginate does not work for me  It is clear that if I remove the first put paginate. but it does not work for me

Possibly because it only shows me a group which group has polls. I need to page the surveys.

It could make everything simpler, making the query to the table surveys putting as where the group to which belongs.

but I want to try to do it with the load. that the group is going through modelBinding. Thanks

    
asked by Alex Burke Cooper 27.12.2018 в 08:03
source

0 answers