J
J
Justique2016-02-07 16:05:02
Laravel
Justique, 2016-02-07 16:05:02

How to call a function with Request from under the controller?

Good afternoon, there is a function with which I get a list through Ajax

public function get(Request $request)
  {
    $lists = DB::table('lists')->where('user_id', $this->__user_id)->get();
    
    return json_encode($lists);
  }

How can I call this function from under the controller itself?
public function index()
  {
    $lists = $this->get();
    return view('lists', ['lists' => $lists]);
  }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
IceJOKER, 2016-02-07
@IceJOKER

pass null as an argument.
Better yet, move the shared code into another method, and then call it via get(); and through index();

E
Eugene, 2016-02-07
@Nc_Soft

Make a service (repository) User and pull it from anywhere

A
Andrzej Wielski, 2016-02-09
@wielski

public function get(Request $request)
Remove Request $request , because you don't use it anywhere.
If used, take it in index() and pass the instance to the get function.
If it is not possible to receive it in index(), but it is required in get() - create a new instance of the Request class, fill it with the necessary or dummy data, and pass it.
A bunch of options. It all depends on the implementation and your code.
If the parts of the code in the question are not fictitious, but real, the first option will suit you. Remove Request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question