Comments (13)
In complex queries with with() previous solution doesn't work. Better to overwrite base query 'from' value instead:
$query = SomeModel::where()->with()...->orderBy();
$query->getQuery()->from(\DB::raw($query->getQuery()->from . ' FORCE INDEX (index_name)'));
// or just
$query->getQuery()->from(\DB::raw('`table` FORCE INDEX (index_name)'));
$results = $query->get();
from ideas.
My solution:
Add following to the model class:
Class SomeModel extends Model {
public static function IndexRaw($index_raw)
{
$model = new static();
$model->setTable(\DB::raw($model->getTable() . ' ' . $index_raw));
return $model;
}
...
}
Now you can use simply something like this:
SomeModel::IndexRaw('FORCE INDEX (some_index_name)')->where('condition','=',true)->get();
from ideas.
I needed the FORCE INDEX
for a join, so I used the following solution...
DB::table('table1 as t1')
->join(
DB::raw('table2 as t2 FORCE INDEX (index_name)'),
't1.some_field', '=', 't2.some_field'
)
...
from ideas.
so you need something like Model::forceIndex('index_name')->get(); ?
from ideas.
Yes. This would save needing to use DB::raw and hard code the table name.
from ideas.
i will make a PR for it , hoping taylor merge it :D
from ideas.
Make sure to link back to this discussion so he can see the reasoning behind it. Seems reasonable to me 👍
from ideas.
if I am not mistaking, you can achieve this performing using models relations, as so:
$users = Loan::has('installments')->with(['user' => function ($query) use ($q){
$query->select(['id', 'first_name', 'last_name']);
$query->where('verified', 'yes');
$query->where('status', 'active');
$query->where('role', '!=', 'admin');
if ($q != '') {
$query->whereRaw("CONCAT (first_name, ' ', last_name) like '%" . $q . "%'");
}
return $query;
}])->where('status', '!=', 'paid')->groupBy('user_id')->get();
this is a relly specific example, but I thought it could illustrate my thoughts
from ideas.
@ahmedash95 did you ever make a PR? Would like to see this missing feature.
from ideas.
I don't believe this has become a feature. It would be important that it worked on all possible database drivers and I am not sure if they all support specifying an index by name?
from ideas.
i'm agree with @danielbaylis , any idea about what should we do ( let it as a DB::row() solution or try to make a PR for that ) ?
from ideas.
What about a beforeWhere()?
from ideas.
I also want to user FORCE INDEX with laravel Eloquent so Please message here if you implement that.
from ideas.
Related Issues (20)
- [Proposal] Easy way to cancel notifications at time of sending
- Rendering Blade Components Outside of Views HOT 1
- Assert that a component has rendered on a page
- Service Container | Contextual Binding of the Methods
- [REQUEST] Enable flush method from dynamoDB cache HOT 3
- [Feature] Enable flush method from dynamoDB
- [Proposal] Support AOP with php8 Attribute
- laravel join select max of joined column
- Ability to get event name which fires in Eloquent booted method HOT 1
- Make `php artisan migrate:reset` work with *-schema.dump
- Size File Validation only deals with Kilobytes
- Suggesting that ".env.testing" file be added to .gitignore HOT 2
- add native support for dev containers HOT 3
- [9.x] Rename firstOrNew() to firstOrMake()
- Set property to a config value via an Attribute HOT 1
- Add support to get more information on the job class
- Allow pausing batches
- afterCommit on Queueable Anonymous Event Listeners
- Upsert for related models
- Command option to run scheduler in a particular timezone
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ideas.