Ad

Non-static Method Illuminate\Database\Eloquent\Model::newQuery() Should Not Be Called Statically

- 1 answer

Receiving the error (in title) that happens whenever i run the following code:

SearchController.php

public function index(Request $request)
   {
   $distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
   $prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');

   $post = Post::newQuery();

    if ($request->has('price')) {
    $post->where('price', $request->price);
    }

  if ($request->has('distance')) {
   $post->where('distance', $request->distance);
   }

   return view('Pages.search', [
    'distances' => $distances,
    'prices' => $prices,
    'posts' => $post->get()
   ]);
Ad

Answer

Use

query();

 $post = Post::query();
Ad
source: stackoverflow.com
Ad