R
R
Rooly2021-11-08 13:32:13
Laravel
Rooly, 2021-11-08 13:32:13

Is it possible to define a function in the table property of a Laravel Model?

Colleagues, help out. In my database (the database is built on a separate schema that is not tied to migrations) there is a function lst_books_hist (?) (which we pass the ID and it returns a line from the table of obsolete books)

class BooksListHist extends Model
{
    protected $connection = 'db_main';
    protected $table = 'lst_books_hist(?)'; --> могу ли я вот так сделать?
}


How then can I build a request by passing the ID there? Using AddBinding? How can I work with the BooksListHist model in this case?
BooksListHist::addBinding("id_book", "select")->select() ? But in this case it doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-11-08
@ZetIndex_Ram

I do not think that it will be possible to implement this in the form of a model.
Make a repository that will implement the query logic and DTO that will hold the values ​​of the table row fields.
However, you can try this:

(new BooksListHist)
    ->setTable("lst_books_hist({$IdFilter['value']})")
    ->first();

You can even override the getTable() method of the model and pull query parameters there.
But, IMHO, these are all crooked crutches and it is better to make a repository that will clearly indicate what can and cannot be done with this data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question