0

I am very new to Laravel and quite confuse with the model and database thing. I understand that a model represent one table. So I created a model using artisan command without migration and it created the code as follow.

namespace App;

use Illuminate\Database\Eloquent\Model;

class RegCars extends Model
{
    //
}

Since I have a different table name, so I add protected $table = 'regcars'; and assume that this model can now access the table by running query such as RegCars::where('user_id', $user_id); from controller. But I wasn't able to get anything by running it.

So I am wondering, how does this model able to run the query? Is the migration needed in order to do this? Is there still other area I need to set before I can run any query?

1 Answer 1

1

highly recommend to read docs or at least watch a tutorial. what you get is a query build not a model or a collection of models. laravel(eloquent) doesnt exactly know what you want so you query database then get the data by methods that laravel gives you if assume you want a RegCars you need a method like first:

RegCars::where('user_id', $user_id)->first(); // now you have model

more info: https://laravel.com/docs/8.x/eloquent

Sign up to request clarification or add additional context in comments.

1 Comment

I know about this first() but I want to get all rows with the same user_id. By the way I have figured it out. I have to add ->get() behind the code in order to retrieve the data.

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.