0

Im new to laravel, i am trying to query a specific table in my DB. I only have 1 data table and the standard user auth tables. I am getting a error: BadMethodCallException Call to undefined method App\Figures::table().

Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Figures extends Model
{
   
}

controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Figures;

class figuresController extends Controller
    public function figurespag2() {
        $dummyDetails = Figures::table('figures')->where('name', 'batman');
        return view ( 'pagination2.index' )->withUsers($dummyDetails);
       }

route

Route::get ( '/pagination2', 'figuresController@figurespag2' );

I know it's going to be something obvious, but I am new to this.

3 Answers 3

1

this is wrong

$dummyDetails = Figures::table('figures')->where('name', 'batman');

Method 1---------- laravel eloquent

Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Figures extends Model
{
   protected $table = 'figures';
}

Controller

  $dummyDetails = Figures::where('name', 'batman')->get();

and Method 2 ---------- laravel Query Builder

  $dummyDetails = \DB::table('figures')->where('name', 'batman')->get();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked. I need to look at models more as I thought the model defines the DB level interaction which I can see is wrong.
1

Use this you not need to define table name

 public function figurespag2() {
        $dummyDetails = Figures::where('name', 'batman')->get();
        return view ( 'pagination2.index' )->withUsers($dummyDetails);
       }

1 Comment

Thank you for the explanation, it helped me to understand the relationship of the model to the query and controller. Thank you.
1

First you may need to know laravel model rules.

If you create a table name like "figures" (plural) you need to create its model by Figure (singular).

if you create a table other then this rule then you have to mentioned table name in model like this.
protected $table = "table_name";

you can access table with where condition in controller like this.

  public function figurespag2() {
    $dummyDetails = Figure::where('name', 'batman')->get();
    return view ( 'pagination2.index' )->withUsers($dummyDetails);
   }

Hope this may help you.

1 Comment

Thank you for the additional clarification. I am new to Laravel and its getting used to the MVC approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.