Laravel eloquent hasmany->hasmany - laravel

it is possible to get all details (user, posts, postimages) within one collection / json?
user->hasmany(posts)
post->hasmany(postimage)
user:
id | name
post:
id | user_id | text
postimage:
id | post_id | imgpath
user model:
public function posts() {
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
posts model:
public function images(){
return $this->hasMany('App\postsimages');
}
get all posts from user works fine:
$myposts = users::find(Auth::user()->id)->posts;
i'm able to get all images from a post within a loop
foreach($myposts as $mypost) {
$postimages = $mypost->images;
}
what i want is to get all posts, images without the loop e.g
$myposts = users::find(Auth::user()->id)->posts->images
thanks

Yes, use hasManyThrough relationship.
posts model:
public function images()
{
return $this->hasMany('App\postsimages');
}
user model
public function posts()
{
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
public function images()
{
return $this->hasManyThrough('App\posts', 'App\postsimages');
}
then
$postimages = Auth::user()->images;

Related

Laravel - Eloquent How can make this relationship HasMany -> BelongToThrough

How can I come up with this kind of relationship?
User:
- id
- name
- username
Post
- id
- user_id
- comment_id
Coment
- id
- other_id
Other
- id
- name
- descripton
So the relationship would be:
User - belongsToMany -> Post - belongsTo -> Comment - belongsTo -> Other
At this relationship the user have many post but the comment_id relation is belongs to.
I cant use hasManyThrough because the relationship is different.
Is there a way to make this work?
it should be hasMany -> BelongsToThrough
I could use hasMany then on each item will call the BelongsTo, but the relationship I had is too deep.
It needs a relationship that can use hasMany then belongsToThrough such relationship
UPDATE
There is a belongsToMany instead of hasMany but still not sure if I could work on this
Got an alternative solution
class User {
public function user_post_comments () {
return $this->belongsToMany(Post::class);
}
public function getCommentsAttribute() {
return $this->user_post_comments->map(function ($value, $key) {
$obj = $value->comment;
$obj->pivot=(object) [
// Extra variable for the nested
];
return $obj;
});
}
}
class Post {
public function comment () {
return $this->belongsTo(Comment::class);
}
}
$this->user->comments will return the post comment instead of posts
https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
You can get comments from users like this
class User extends Model
{
/**
* Get all of the Comments for the Post.
*/
public function comments()
{
return $this->hasManyThrough(Comment::class, Post::class);
}
/**
* Get all of the Post.
*/
public function posts()
{
return $this->hasMany(Post::class);
}
}
Get Other's comment like this
class Comment extends Model
{
public function others()
{
return $this->belongsTo(Other::class);
}
}
and get final data like this
$data = User::with(['comments.others'])->get();
Got something that will work if to add other information in the pivot.
use Illuminate\Database\Eloquent\Relations\Pivot;
class ModelPivot extends Pivot
{
public $appends = ['extra_detail'];
public function getExtraDetailAttribute() {
return App\Detail::find($this->attributes['detail_id'];
}
}

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();

Override primary key with foreign key in hasMany relation using laravel

Here are my tables
Posts
id | name | created_At
Comments
id | comment | post_id | created_at
Post Model
function comments(){
return $this->hasMany('App\Models\Comment','id','post_id');
}
PostsController
function index()
{
Post::with('comments')::get();
}
Now how can i get list of all posts along with comments?
I am trying to match post_id in the child table.
In Your Post Model:
public function comments()
{
return $this->hasMany('App\Model\Comment');
}
In Your Post Controller :
use Illuminate\Http\Request;
function index(Request $request)
{
$posts=Post::with('comments')->get();
// in json
return response()->json($posts);
// in your view
//return view('viewname')->with('posts',$posts);
}

Laravel 5 nested relationship

I have 3 tables, with:
User: id | name
Products: id | name | user_id
Tracks: user_id | product_id
Relationships between these models (tables) are:
//User.php*************************************************
public function products() {
return $this->hasMany('App\Product');
}
public function track() {
return $this->hasMany('App\Track');
}
//Product.php*************************************************
public function user() {
return $this->belongsTo('App\User');
}
public function track() {
return $this->belongsTo('App\Track');
}
//Track.php*************************************************
public function products() {
return $this->hasMany('App\Product');
}
public function user() {
return $this->belongsTo('App\User');
}
Track table is used if user want to track the product. Some kind of bookmark.
So, when I'am on product page, $product = App\Product::find(1), if I want to echo the user name, I do $product->user()->name, but if I want to know if the user already tracking the current product, when I do $product->user()->track()->get() I receive this error:
Call to undefined method Illuminate\Database\Query\Builder::track()
If I do $product->user()->with('track')->get(), I don't get an error, but I get User Object which it do not contain info if the user already tracking the product.
How can I know if user is already tracking the product?
First, some of your relationship should be like these
Product.php
public function track() {
return $this->hasMany('App\Track');
}
Track.php
public function product() {
return $this->belongsTo('App\Product');
}
now to check if a user is tracking a product
$product = App\Product::find($id);
$product->user->track()->where('product_id', $product->id)->first();
another way
$user = App\User::find($id);
$user->track()->where('product_id', $product->id)->first();
And to check if logged in user is tracking the product, you can get the logged in user by Auth::user() , so
$user = Auth::user();
$user->track()->where('product_id', $product->id)->first();

Need help laravel 4.2

I want to get the username in table user, of post ID in post table and return View::make('post.index'). I need to view in post.index blade.
Database
users table
id
username
post table
id
message
posts_id
Post.php #models folder
public function posts()
{
return $this->belongsTo('User');
}
User.php #models folder
public function post()
{
return $this->hasMany('Post');
}
PostController.php #controller folder
public function index()
{
$posts = Post::with('username')->get();
return View::make('post.index')->with('posts', $posts);
}
post/index.blade.php
the result here
i need to the result of author id become the username
for the best practices:
your model class names should be User and Post. database table names should be users and posts.
in Post.php file ,
public function user()
{
return $this->belongsTo('User');
}
in User.php file,
public function posts()
{
return $this->hasMany('Post');
}
in your PostController.php
public function index()
{
$user_of_the_post = Post::find(postId)->user; //replace the postId //this will return the user object of the specified post
$user_name_of_the_author = $user_of_the_post->username;
**pass the necessary data to the view and print and return the view**
}

Resources