This question already has answers here:
Difference between Laravel DB::insert() and DB::table()->insert()
(3 answers)
Closed 1 year ago.
what is the differences of following two:
DB::update(
'update users set votes = 0 where votes < 0'
);
DB::transaction(function () {
DB::table('users')->where('votes', '<' , 0)->update(['votes' => 0]);
});
Official Laravel doc says
The update method should be used to update existing records in the database
But transaction seems more handful that can react to exceptions.
So in what scenarios one is better than the other?
The DB::update() is for raw sql queries on the other hand DB::table()->update is for query builder. Query builder is much more efficient as it prepares and binds the statements itself.
The question has been answered here
Related
This question already has answers here:
How to select specific columns in laravel eloquent
(23 answers)
Closed 6 months ago.
This code fetches all the data in the table in the database, but I want to set a maximum limit, for example, it fetches only 4 columns from the table so that it does not load all the columns in the table
'productes' => productModel::orderBy('id', 'DESC')->get()
'productes' => productModel::orderBy('id', 'DESC')->limit(20)->get(['id',...])
Try this. But I'm highly recommended you read laravel documentation
limit is the count of rows that will be fetched from DB and
get will fetch columns that you passed in the array
This question already has answers here:
Oracle DB: How can I write query ignoring case?
(9 answers)
Closed 4 years ago.
I am working with database where i need to retrieve distinct values. Example [English enGLish english ....] all this should be displayed as English only. Can anyone tell me how to achieve this.
Thanks in advance
Looks like you need function initcap():
demo
select distinct initcap(column_name) from your_table
This question already has answers here:
Oracle: function based index selective uniqueness
(2 answers)
Closed 8 years ago.
I have a table with a column IS_LOGGED which takes only two values 'Y' or 'N'
I want to have a Function based index on the column IS_LOGGED where value is 'Y'.
The purpose of index is to list all entries in an easy and fast to access manner.
Suppose you get a 1500 pages thick book to read with an index listing only specific chapters. What is the use of such an index?
I don't think you need a function based index here. Simply create an index and modify your queries to include IS_LOGGED = 'Y' clause.
This question already has answers here:
LINQ : Dynamic select
(12 answers)
Closed 8 years ago.
I'm working with dynamic queries using LINQ on Entity Framework.
To query some tables by user input filters, we are using PredicateBuilder to create conditional WHERE sections. That works really great, but the number of columns returned are fixed.
Now, if we need the user to select which columns he needs in their report, besides their filters, we are in trouble, as we don't know how to do dynamic myQuery.Select( x => new { ... }) as we do for Where clause.
How can we achieve something like this?
A bit of searching reveals that this is tricky. Anonymous types are created at compile time, so it is not easy to create one dynamically. This answer contains a solution using Reflection.emit.
If possible, I would recommend just returning something like a IDictionary<,> instead.
This question already has an answer here:
How to check if a columns value was explicitly specified in a PL/SQL BEFORE UPDATE trigger?
(1 answer)
Closed 8 years ago.
Is there an efficient method to identify which column has changed in a table in Oracle using a trigger? How to check if only one column (of interest) or all other columns also changed?
As the post Alex Pole pointed in your comments states, you could use the UPDATING function.
But you can also develop different triggers for updating specific columns with "BEFORE UPDATE OF" clause, witch is the optimal option if there is no code to share among the actions for the involved columns.