Laravel get count from model relationship - laravel

I have this 3 model.
Author
|id|name|
Post
|id|author_id|
Likes
|id|post_id|
The relationship.
Author
public function post()
{
return $this->hasMany(Post::class, 'author_id', 'id');
}
Post
public function author(){
return $this->belongsTo(Author::class, 'id', 'author_id');
}
public function likes(){
return $this->hasMany(Likes::class, 'post_id', 'id');
}
Likes
public function post()
{
return $this->belongsTo(Post::class, 'id', 'post_id');
}
My problem is, how to list all Authors with total likes from all of his posts.
Using call $authors->posts->likes results an error because posts returns a collection.

Author hasMany posts, and Post hasMany likes.
To achieve all likes from the Author model, you can define a hasManyThrough() relationship :
public function likes()
{
return $this->hasManyThrough(
'App\Likes', // Likes model
'App\Post', // Post model
'author_id', // Foreign key on posts table...
'post_id', // Foreign key on likes table...
'id', // Local key on authors table...
'id' // Local key on posts table...
);
}

Related

How to access a relationship through another relationship

I am working on a survey system and I have a problem accessing one relationship through another.
I have these models and these relationships
Post
id
survey_id
public function survey()
{
return $this->belongsTo(Survey::class);
}
Surveys
id
survey_type_id
public function surveyOptions()
{
return $this->belongsToMany(Survey_options::class);
}
public function surveyType()
{
return $this->belongsTo(Survey_type::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
Survey_types
id
public function surveyOptions()
{
return $this->belongsToMany(Survey_options::class);
}
Survey_options
id
survey_type_id
public function values()
{
return $this->hasMany(Option_value::class);
}
options_values
survey_options_id
survey_id
public function surveyOptions()
{
return $this->belongsTo(Survey_options::class);
}
Survey_Survey_options (pivot)
survey_id
survey_options_id
I need to do a query with eadger loading to bring me all the posts with survey, surveyOptions, withCount comments y options_values of each surveyOptions. Something like this:
$post->survey->surveyOptions->options_values_count
I have managed to create this query that works for me everything except fetching the option_values count from each SurveyOption. How can I do it?
$posts = Post::with([
'survey' => function ($query) {
$query->withCount('totalSurveys');
},
'survey.surveyOptions',
'image',
'categories'
])
->withCount('comments')
->get();

Laravel search based on foreign key from a different table

I have three tables post,user and country.
Post is saved by user who is from a country.
Now I want to search all posts of all users from a country.
so user will filter by country and I get country code from search box to controller $request->input($country);
Here are my model relationships:
POST MODEL:
class Post extends Model
{
protected $table = 'posts';
protected $dates = ['status_change'];
public function photos()
{
return $this->hasMany(Photo::class,'post');
}
public function make_rel()
{
return $this->belongsTo(Make::class, 'make_id' ,'id','make_logo');
}
public function user_rel()
{
return $this->belongsTo(User::class, 'created_by' ,'id');
}
}
COUNTRY MODEL:
class Country extends Model
{
public function users(){
return $this->hasMany('App\User');
}
}
USER MODEL:
class User extends Authenticatable
{
public function country_rel()
{
return $this->belongsTo(Country::class, 'country' ,'country_code');
}
}
SEARCH FUNCTION
public function search(Request $request)
{
$this->validate($request, [
'country' => 'required',
]);
$country = Country::where('country_name',$request->input('country'))->get();
$data = Post::where('created_by',$country->user_rel->name)
->get();
dd($data);
}
This is not working. Could anyone advise what am I doing wrong?
I would use hasManyThrugh. The documentation even uses your exact use case:
class Country extends Model
{
public function users(){
return $this->hasMany('App\User');
}
public function posts() {
return $this->hasManyThrough(
'App\Post',
'App\User',
'country_id', // Foreign key on users table...
'user_id', // Foreign key on posts table...
'id', // Local key on countries table...
'id' // Local key on users table...
);
}
}

Laravel relations pivot table name trouble

in my app I have 2 conversations types: normal private conversations between users and conversations for people which are interested of user product.
In my User model I declared relations like:
public function conversations()
{
return $this->belongsToMany('App\Conversation', 'conversation_user');
}
public function conversationsProduct()
{
return $this->belongsToMany('App\ConversationProduct', 'conversation_product_user', 'user_id', 'product_id');
}
Where 'conversation_user' and 'conversation_product_user' are pivot tables between 'users'-'conversations' and 'users'-'conversations_product' tables.
My conversation_user pivot table has conversation_id and user_id table properties, but conversation_product_user pivot table has additional property product_id.
In my Conversation Model I have:
public function users()
{
return $this->belongsToMany('App\User');
}
public function messages()
{
return $this->hasMany('App\Message');
}
In ConversationProduct Model I wrote:
protected $table = 'conversations_product';
public function users()
{
return $this->belongsToMany('App\User', 'conversation_product_user');
}
public function messages()
{
return $this->hasMany('App\MessageProduct');
}
In my ConversationProductController I have method to find user conversations:
public function showUserConversationsProduct(Request $request){
$user_id = $request->user_id;
//var_dump($user_id);
$userData = User::where('id', $user_id)->with('conversationsProduct')->first();
}
And there is my problem: In controller ->with('conversationsProduct') don't take relation for conversation_product_user, but for conversation_user pivot table. I can't handle it why its happen if I add second parameter as 'conversation_product_user' in my relation:
public function conversationsProduct()
{
return $this->belongsToMany('App\ConversationProduct', 'conversation_product_user', 'user_id', 'product_id');
}
I need protected $table = 'conversations_product'; to point my ConversationsProductController#store where to save conversation, but I think that can be problem with recognise proper relation.
I really appreciate any help. I attach photo of my db relations.

Laravel - Get articles from feeds for one user

I have three tables:
articles :
id
feed_id
title
feeds :
id
user_id
name
users :
id
name
I want get all articles from feeds for one user, I search the Eloquent Relationships...
Could you help me please?
First, define the relation on User model. Best option for you situation is to use a Has Many Through relation:
public function articles()
{
return $this->hasManyThrough(
'App\Article',
'App\Feed',
'user_id', // Foreign key on feeds table...
'feed_id', // Foreign key on articles table...
'id', // Local key on users table...
'id' // Local key on feeds table...
);
}
Then make a user object and get his articles:
$user = /App/User::with('articles')->find($id);
foreach($user->articles as $article) {
// do whatever you need
}
laravel documentation is always a good start for researching, read about hasManyThrough
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
don't mind if it is in 5.5, there are no significant differences between them
in model
in Articles.php
public function Feed()
{
return $this->belongsTo('App\Feed');
}
in Feed.php
public function User()
{
return $this->belongsTo('App\User');
}
public function Articles()
{
return $this->hasMany('App\Articles');
}
in User.php
public function Feed()
{
return $this->hasMany('App\Feed');
}
in controller
// To get all articles of user with id = $id
$reports2 = Articles::with(array('Feed'=>function($query)use($id){
$query->with('User');
$query->where('user_id',$id);
}))->orderBy('id', 'desc')
->get();

belongsTo does not work in laravel

In my app I have Users and Tickets.
In the table 'tickets' I have one foreign key, like this:
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
In the model 'Ticket' I have this relationship:
public function owner()
{
return $this->belongsTo('App\User');
}
User model:
public function createdTickets()
{
return $this->hasMany('App\Ticket');
}
By doing this:
dd(\Auth::user()->createdTickets()->get()->toArray());
I get all tickets, no problem here. But when I try to get the owner from the Ticket, it gives me null:
$ticket = \Auth::user()->createdTickets()->first();
dd($ticket->owner); //if I try dd($ticket) it juts fine.
PS: For the record, I have another type of relationship between both model (many to many)
Ticket:
public function followers()
{
return $this->belongsToMany('App\User')->withTimeStamps();
}
User:
public function followingTickets()
{
return $this->belongsToMany('App\Ticket')->withTimeStamps();
}
I got my answer on laracasts forums:
Laracasts
for "belongsTo" relation eloquent uses the method name on the foreign key. I am using owner(), so eloquent was looking for owner_id field.
So I had to do this:
public function owner()
{
return $this->belongsTo('App\User', 'user_id');
}

Resources