How to write this query with Laravel ORM - laravel

I am trying to write this query with laravel orm to use the eager loading but not getting it.
SELECT p.* FROM tbl_posts as p,tbl_posts_requests as pr WHERE p.post_status='active' AND (p.post_id=pr.request_post_id OR p.post_private='no' )
I have a comment table to eager load with it but i am not able to create a laravel ORM version of this query

Eloquent ORM eager loading happens for Eloquent models with specific relationships.
So assuming you have a tbl_posts_requests relationship in your Posts model, it could be something like this:
Posts::where(['post_status' => 'active', 'post_private'=>'no'])->with('tbl_posts_requests')->get();
I am assuming you called your relationship function tbl_posts_requests there.

Related

How to use eager loading with custom query builder in Laravel

I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:
$view = DB::table('blocks')
->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');
I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.
Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.
But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?
Block dates and modules are conditional, Sometime I dont want get data of this table with block.
Please help me on this.
You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.
But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.

OrderBy using withCount on Polymorphic relationship not working on Laravel 5.2 / 5.3

I love the withCount method on Eloquent to orderBy a relationship count. Let's say we'd want to get some posts ordered by the number of favorites they have, in a "normal" one-to-many relationship:
$posts = $query->withCount('favorites')->orderBy('favorites_count', 'DESC')->get();
That works perfectly, as I said, in normal relationship. BUT when is used with a polymorphic relationship, it seems to take a huge amount of time so for more than 100 - 1000 elements it fails, in both Laravel 5.2 and 5.3.
Is there anything I'm missing? I've used polymorphic relationships quite often, I don't think that there is a problem with the relationship itself but just with the withCount method.
Note: I can't use sortBy, it must be orderBy. And I know I could do it with Fluent query builder, but I'm interested on using Eloquent this way.
Thanks!

How to create models for dynamically generated tables (on the fly)?

I stumbled upon this problem today. My project has a few tables, for which I have created their dedicated Models. However, my project allows user to create dynamic tables.
How do I create relationships or models for/between these dynamically generated tables?
I would not recommend to create Eloquent Models on dynamically generated tables, because Eloquent Models are here to help developers with already created database tables (features like relationships, scopes, attributes casting are usually defined for existing database tables).
What would be my recommendation? Use the Query Builder instead. You will have almost the same functionalities of Eloquent Models, except without model.
Instead of having something like:
DynTable::where('foo', 123)
->where('bar', 456)
->first();
You will have the following:
DB::table('dyn_table')
->where('foo', 123)
->where('bar', 456)
->first();
Basically, the same. But, with the benefit that you can specify any table name to the Query Builder, but you can't change the table name to an Eloquent Model (in a running script).

Translate query to Laravel Eloquent Model

How would I translate the following query into a format that Laravel can parse?
select posts.id,title from posts,albums where posts.id != albums.post_id
Now it has this ORM relations
a Post hasOne Album
an Album Belongs to a Post
So with this statement I want to know which Posts dont have Albums binded.
so far I have
Post::with('album')->select('id', 'title')->get();
I don't know how to declare the condition
Please do read docs again, I think you may look for polymorphic relationship or One to Many. If you use Eloquent ORM do use relationships its the biggest why "YES".
As for the query you can do http://laravel.com/docs/4.2/queries#raw-expressions
There is an example in a link just fill in your query and you are done.

Data processing using eloquent orm methods

I am very beginner to PHP and Laravel.
I am using laravel 5 eloquent ORM. I have an array $caseIDs and want to fetch the data from db where caseID (column name) matches from one of the $caseIDs elements. Can I use where() method of eloquent ORM? or How can i do it?
You need to use whereIn which simulates a WHERE IN('comma','separated','values')
YourModel::whereIn('caseID', $caseIDs)

Resources