Translate query to Laravel Eloquent Model - laravel

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.

Related

Laravel - How to relate two collections like Eloquent method "belongsToMany" and "with"

How can you combine two collections, one being the collection of parent items together combined with your collection of child items?
I would like something like using the method with and belongsToMany,but in this scenario I cannot use both methods correctly because one table is in another schema and the pivot table is in another schema.
Area::with('permissoes')
->where('sistema', '<>', 'S')
->get()
Most Eloquent eagerloads are done with separate queries that just use an IN statement on keys from the previous one. Pivot tables are the exception. It sounds like you need to explicitly tell the Model relation what database your pivot table is in. See my answer here: belongsToMany relationship in Laravel across multiple databases

how do I make relationships for 4 tables including pivot table

I have many to many relationships. Imagine I have 3 tables. Something like this :
users.
roles.
role_user.
(This example is also provided in laravel's docs).
Now I'm doing this : $user->roles() which returns roles with Pivot attributes . but what I actually want to do is move forward and also get the appropriate data from the 4th table. something like this $user->roles()->types(); and the difficult thing is that this types() belongs to pivot table.
Do you know how to do this kind of thing ? where Do I write types() function?
Assuming your "Roles" model has the relationship set you may try
$user->roles()->with("types")
Source docs: https://laravel.com/docs/5.7/eloquent-relationships#eager-loading

Multi categories posts

I'm using laravel 5.6 and i have two tables categories and posts
I have created category_id in posts table which looks like
{id, category_id, title, description, created_at, updated_at}
I created a drop-down on the post create and edit form to select the category which works fine.
Now I am looking for something more advanced where a post can have multiple categories. I have changed belongTo to HasMany categories in post model.
I feel I am doing it the wrong way. Do i need to create another table i.e.,
post_categories
{id, category_id, post_id}
The reason i want to do this is because i have multiple posts which belong to multiple categories and my route is like this
site.com/categoryname/post-slug
So few posts appear in multiple categories.
You probably will need to use the pivot table. That way you can data mine. Even if you don't have a multi-select, you'll easily be able to collect posts linked to categories and vice versa. laraveldaily-good example. They use the sync method, one of my favs. when you save the form data you can just do something like App\Post::find($id)->categories()->sync(request()->input(categories')) and laravel will handle the rest for you.
Your relationships look like they are thought out. to me, it looks like your on the right track.
Just use the belongsToMany relationship instead of HasMany
Laravel has great documentation on this: laravel many to many

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!

Conditional Relationship in laravel Eloquent

Let I have 3 table named user, admin, post
My post table structure is
-id
-poster_id
-poster_type //if value is 1 then poster_id will be releted with user table. if value is 2 then poster_id releted with admin table.
Now How writte belongsTo relationship with two table based on poster_type value
I want to do in Post model like this
public function Author(){
return $this->belongsTo('User', 'poster_id')->where('poster_type', '1') //poster_type is the field of post table.
}
First of all, you are talking about a Polymorphic relationship, supported by eloquent. You should take a look at the documentation.
http://laravel.com/docs/eloquent#polymorphic-relations
Second, you are mixing Eloquent relationships with special data recovery functions, and that's something you should avoid. I suggest you split the relationship itself from the data recovery function.
Also, if you want to go one step further, keep the relationship in the model, and split the data recovery functions into a repository object.

Resources