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

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'];
}
}

Related

Laravel MongoDB 1:n relations

What is the correct way of defining 1:n relations in Laravel using MongoDB between two models Author and Book where one author can have several books while one book has exactly one authors?
Author.php
class Author extends Model
{
public function books()
{
// what to place here?
return $this->...
}
}
Book.php
class Book extends Model
{
public function author()
{
// what to place here?
return $this->...
}
}
Controller.php
class BookController extends Controller
{
public function store (Author $author, Request $request)
{
$book1 Book::create();
$book2 Book::create();
// connect $book1 <-> $author and $book2 <-> $author
// what to place here?
$book1->save();
$book2->save();
}
}
After calling store() I want to be able to do the following request
dump ($book1->author); // return $author
dump ($author->books); // return Collection of $book1 and $book2
assume you have the foreign key author_id in books table
class Author extends Model
{
public function books()
{
return $this->hasMany(Book::class, 'author_id');
}
}
class Book extends Model
{
public function author()
{
return $this->belongsTo(Author::class, 'author_id');
}
}
class BookController extends Controller
{
public function store (Author $author, Request $request)
{
$book1 = new Book();
$book2 = new Book();
$author->books()->saveMany([$book1, $book2]);
}
}
Suppose you have the ff:
users = model a
posts = model b
Case 1: vice versa of Case 2:
Case 2:
// on posts table/document you should have a user_id (as foreign key)
// and on post model you need define the belongsTo relationship
$this->belongsTo(User::class)
// on the User model you have to define hasOne or hasMany
$this->hasOne(Post::class) or $this->hasMany(Post::class)
Case 3:
On case 3, I'm not sure if adding a reference to each other is necessary because creating a single reference that links them can provide the data you require regardless of whether the request comes from model a or model b.
It is possible that it will not solve your problem. However, hopefully it will be of some assistance to you.
For case 1 ( in model_a ):
public function model_b_many()
{
return $this->hasMany( Model_b::class, 'id' );
}
For case 2 ( in model_b ):
public function model_a_many()
{
return $this->hasMany( Model_a::class, 'id' );
}
This will also work for case 3 :)

Laravel - Defining has-many-through relationship

I'm having a problem while creating a Forum with Laravel 5.7
I want to have these three models: User, Category, Thread and Post. The problem is that i don't how to define one of my Thread model relationships.
This model has a creator:
public function creator()
{
return $this->belongsTo('App\User');
}
...it has a series of replies:
public function replies()
{
return $this->hasMany('App\Post');
}
...and, finally, participants:
public function participants()
{
return $this->hasManyThrough('App\User', '???')
}
I should get the thread participants through its replies but it don't know if i should put the post class like this:
public function participants()
{
return $this->hasManyThrough('App\User', 'App\Post')
}
...or if this is an special case with some considerations. Any help?
Since posts acts as a pivot table, a BelongsToMany relationship is the better choice here:
public function participants()
{
return $this->belongsToMany('App\User', 'posts');
}

Laravel how to define 3 relationships with one model?

I have a Model which is called Championship. Championship may have 3 judges which are called Main Judge, Main Secretary and Judge Operator.
All of them linked to User Model and stored in the database as user ID.
My relationships looks like this
class Championship extends Model
{
protected $table = 'championships';
public function mainJudge()
{
return $this->hasOne('App\User', 'id', 'main_judge');
}
public function mainSecretary()
{
return $this->hasOne('App\User', 'id', 'main_secretary');
}
public function judgeOperator()
{
return $this->hasOne('App\User', 'id','judge_operator');
}
}
But I can't undertand how to define inverse relationship in User model
class User extends Authenticatable
{
public function sex()
{
return $this->belongsTo('App\Models\Sex');
}
public function player()
{
return $this->hasOne('App\Models\Player', 'user_id');
}
public function championship()
{
????
}
You just have to add it like you are adding other relations :
public function championship()
{
return $this->belongsTo('App\Championship');
}
When you do :
$championship = Championship::find($id);
$mainJudge = $championship->mainJudge;
$mainSecretary = $championship->mainSecretary;
// All objects will be exactly same
dd($mainJudge->championship,$mainSecretary->championship,$championship);
I assume all the user records have a foreign key to championships table championship_id
When you call the $user->championship relation it will return you the championship wrt to its foreign key championship_id
No need to worry you are just confusing the inverse relations:
See it this way:
Your mainJudge, mainSecretary, judgeOperators are of type App\User and every user have a championship_id when you will call the (App\User)->championship it will always return you its respective championship or null if the championship_id is empty.
Its just matter of perspective.
Just try the above code it will clear out your confusion.

Eloquent hasOne relationship Not working laravel 5.4

I am trying to keep a relationship between two tables in my database.
Users Table:
Id User_id Name
Contacts Table:
Id User_Id contacts
I am saving multiple contacts in the contacts table. Now i want to retrieve all the contacts saved to the User_ID. For that, i am trying to establish a hasOne relationship but i get a error for the following code can someone let me know what i am doing wrong? I am following the official documentation for this.
User model:
class users extends Model
{
protected $table = "users";
public function contacts()
{
return $this->hasOne('App\contacts','User_Id');
}
}
Contacts Model:
class contacts extends Model
{
public $table = "contacts";
public function Users(){
return $this->belongsTo('App\Users','User_id');
}
}
Controller:
public function eloquentget(){
$contacts = Users::find(1)->contacts->first();
return response()->json($contacts,200);
}
When i try this code in postman i get an Exception :
ErrorException
Trying to get property of non-object
What am i doing wrong? Any help will be appreciated.
Since you are saving multiple Contacts for any given User then you are using the wrong relation. You need to setup a One-Many relationship using hasMany().
class users extends Model
{
protected $table = "users";
public function contacts()
{
return $this->hasMany('App\contacts','User_Id','User_Id');
}
}
Try something like this
class users extends Model
{
protected $table = "users";
public function contacts()
{
return $this->hasMany('App\contacts','User_Id','User_Id');
}
}
And on your controller, You can use it like this
$contacts= User::find(1)->contacts; // it will return an array of collection
You don't need to use ->first(); because you already used relation hasOne
just use :
$contacts = Users::find(1)->contacts;
return response()->json($contacts,200);

Laravel query multiple tables using eloquent

hi sorry bit of a newbie here but I am have three tables users, profiles, friends. they all have the user_id fields within them and I want fetch all of the fields in one statement using Eloquent and not DB::statement and doing the table joins.
How can I achieve this?
Try this
use the User class and the with method that laravel has to query model relationships
$user = User::with(['profile', 'friend'])->get();
Ensure your models has the correct relationships as follows:
app/models/User.php
public function friend () {
return $this->hasMany('Friend');
}
public function profile () {
return $this->hasOne('Profile');
}
app/models/Profile.php
public function user() {
return $this->belongsTo('User');
}
app/models/Friend.php
public function user() {
return $this->belongsTo('User');
}
use some thing like this:
You should define relations in your models with hasOne, hasMany.
class Review extends Eloquent {
public function relatedGallery()
{
$this->hasOne('Gallery', 'foreign_id', 'local_id');
}
}
class Gallery extends Eloquent {
public function relatedReviews()
{
$this->hasMany('Review', 'foreign_id', 'local_id');
}
}
$gallery = Gallery::with('relatedReviews')->find($id);
Will bring the object Gallery with
$gallery->id
gallery->name
...
$gallery->relatedReviews // array containing the related Review Objects

Resources