My current situation involves working with two tables:
In the quotes table, we have columns such as first_name
, email
, estimate
, and more.
Then, there's the quote_notes table with columns like id
, note
, and quote_id
.
The relationship between these tables is that quotes has_many quote_notes. I'm attempting to incorporate this join in my query within the quote model.
function getAllNotes() {
return db('quote_notes')
.select('quote_notes.id', 'note', 'quote_notes.created_at', 'quote_id')
.join('quotes', 'quote_notes.quote_id', 'quotes.id')
.orderBy('id')
}
Upon sending a GET request to the specified path, the response includes all quote_notes associated with any quote being viewed.
router.get('/:id/notes', (req, res) => {
const {id} = req.params;
Quotes.getAllNotes(id).then((users) => {
res.status(200).json(users);
});
});
For clarification purposes, here's the request:
const getQuoteNotes = async () => {
let quoteNotes = await axiosWithAuth().get('/quotes/' + quoteId + '/notes');
setQuoteNotes(quoteNotes.data);
}
Lastly, here's an overview of the quote_notes table structure:
table.bigInteger('quote_id')
.unsigned()
.index()
.references('id')
.inTable('quotes')