It all boils down to the same thing SQL.
Eloquent is just syntactic sugar for the query builder.
Is it bad to use query builder instead of model in laravel ? i like db.
DB::table('tableName')->get();
Its so easy to use
It all boils down to the same thing SQL.
Eloquent is just syntactic sugar for the query builder.
I use getPdo() quite often, only eloquent for simple relations. But no there's nothing wrong with the query builder.
And @tray2 have you noticed really weird answers from the AI?
I don’t see how using the plain query builder makes anything easier.
Laravel is built on the MVC pattern. If you forego Eloquent, you’re just using VC, making everything more complex. The whole pattern is based on having model classes that represent data structures – a core concept in all object-oriented programming.
I cannot see any logical reason for eschewing Eloquent in a Laravel project. Unless you need to fetch data that doesn’t correspond to a model class (which happens, but is generally fairly rare), there are no advantages to using a plain query builder over Eloquent, but several downsides:
DB::table('models')->find(123) is not as intuitive as Model::find(123)
@kokoshneta you can still have models without eloquent.
@jlrdw Yes, but the question specifically says, “use query builder instead of model in laravel”.
Using the plain query builder instead of the extended Eloquent query builder for model data seems silly to me – it’s just making more work for yourself with no benefit – but not using models at all in a Laravel application is downright bizarre in my view.
Plain query builder it's easy, but quite frankly using the eloquent model it's easier. I don't think that not creating an extra file for the eloquent class really counts as a benefit.
Of course there cases where Eloquent is not a good fit, like when the query is a very complex one but plain query builder won't make it easier either. In those cases I'll use DB::select($sql, $bindigns) and that'll be it.
Like @kokoshneta pointed out, you miss out a lot by using plain query builder instead of eloquent to gain virtually nothing
I like query builder because I don't need to create model files or create any relationship in it . I can make any kind of query sample or complicated straight forward. I can Use join, where, select etc in that way statement. I think 🤔 is more clear and easy. Eg.
DB::table() ->join() ->join() ->where() ->select() -get();
This type of statement do thing fast what ever I want to do
What you people think about it ??
@abdulrehman176617 You create the model file and its relationships once, then call the same class and functions everywhere you need to use them. That’s the purpose of a class.
If you don’t create a model class, you have to repeat your queries and functions every time you need to use them, which has nothing but downsides: it’s repetitive, slower, makes for hard-to-read code, makes it very hard to change or update code, etc.
You can also do Contacts::join()->join()->where()->where()->get() ; or whatever you need.
Everything you can do with plain query builder Eloquent can do it.
I just believe that not writing a file for the eloquent model is such a miniscule benefit compaired to what eloquent brings. But if you think that you don't need any of those features, of course, query builder is enough.
@tray2 @jlrdw @kokoshneta @snapey @psrz
So what you guys think about it ??
@abdulrehman176617 a join and group by type query is different from related data type queries, so learn both is my suggestion.
And you still should have models.
@abdulrehman176617 I agree with @jlrdw , you don't really need models to query your data, if you don't want them. However, they will help you with all the crud operations, and the relationships defined in the model will help you write cleaner code in your controllers, so yes you should learn how to use them, and then use them. The will speed up your development, and when using models it's very easy to test relations between tables., and you would get an Eloquent model from your query, which allows for cleaner code in your views.
Here is an example on how I validate, store and sync with a many-to-many relation between book and authors. The reason I set the foreign key in that way, is because the values are passed in from datalists, so they needs to be converted to ids.
public function __invoke(BookFormRequest $request, ForeignKeyService $foreignKeyService)
{
$valid = $request->validated();
if ($request->series_name === 'Standalone') {
$valid['part'] = null;
}
$book = Book::create(array_merge($valid, [
'genre_id' => $foreignKeyService->getGenreId($request->genre_name, 'book'),
'format_id' => $foreignKeyService->getFormatId($request->format_name, 'book'),
'series_id' => $foreignKeyService->getSeriesId($request->series_name),
'publisher_id' => $foreignKeyService->getPublisherId($request->publisher_name),
]));
$book->authors()->attach($foreignKeyService->getAuthorIds($request->author));
return redirect(route('books.index'));
}
Please or to participate in this conversation.