Eloquent Relationships Laravel connecting multiple tables together - php

i am attempting to rewrite all my joins into Elequent model relationships.
Here is what i have so far:
class SectionAndUser
{
public function sections()
{
return $this->belongsTo('App\Models\Section');
}
public function users()
{
return $this->belongsTo('App\Models\User');
}
...
class User
{
public function sectionAndUser()
{
return $this->hasMany('App\Models\SectionAndUser');
}
...
class Section
{
public function sectionAndUsers()
{
return $this->hasMany('App\Models\SectionAndUser');
}
...
With the select:
$sections = User::find($userId)->sectionAndUser()->get();
I get the result:
{
"id": 1,
"section_id": 1,
"user_id": 133
}
How do i now attach the 3 model section that carries all the data about section_id 1?
This is the join that i am hoping to achieve:
$id=Auth::id();
$results = DB::table('sections')
->join('section_and_users', function ($join) use ($id) {
$join->on('sections.id', '=', 'section_and_users.section_id')
->where('section_and_users.user_id','=', $id);
})
->get();
The expected result:
{
"id": 1,
"section_id": 1,
"section_name": 'sectionName'
"user_id": 133
}

I think the solution is to create only models Section and User, and add the relationship as BelongsToMany.
class User
{
public function sections()
{
return $this->BelongsToMany('App\Models\Section');
}
...
And
<?
class Section
{
public function users()
{
return $this->BelongsToMany('App\Models\User');
}
...
And of course, you need to create the pivot table. You can consult BelongsToMany documentation.
If you use this way, you can simple get the result with this query:
$section = Section::find(1); // This will return all your Section data
$section_related_users = $section->users; // This will return a collection of Users

You could do it this way
$id=Auth::id();
$results = SectionAndUser::where('user_id', $id)->with('users', 'sections')->get();
then you could map it to get your desired output
$sections = collect($results)->map(function ($section){
return [
'id' => $section->id,
'section_id' => $section->id,
'section_name' => $section->sections->name,
'user_id' => $section->user_id
];
});

You can create a many-to-many realtionship without the SectionAndUser-model.
With the belongsToMany-method, you can pass the name of the pivot table as a second argument. You can view Illuminate\Database\Eloquent\Concerns\HasRelationships#belongsToMany if you want to know what other arguments you can pass.
Section:
class Section extends Model
{
...
public function users()
{
return $this->belongsToMany(User::class, 'section_and_users');
}
...
}
User:
class User extends Model
{
...
public function sections()
{
return $this->belongsToMany(Section::class, 'section_and_users');
}
...
}
Then use it as this:
$user->sections->where(...

// Post Model
public function user()
{
return $this->belongsTo('App\User');
}
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
// Role Model
public function users()
{
return $this->hasMany('App\User');
}
// User Model
public function role()
{
return $this->belongsTo('App\Role');
}
public function posts()
{
return $this->hasMany('App\Post');
}
//Tag Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
// Catgory Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
// controller
public function index()
{
$posts = Post::latest()->get();
return view('admin.post.index',compact('posts'));
}
// posts tabel to user result get
foreach($posts as $post){
$post->user->name
}

Related

How can I resolve two foreign key and three table relation with eloquent

I can't resolve this relation with eloquent. I don't know, maybe it is illegal relation. But I can't think of another relationship. I need one CustomerCodeReferences row from SaleProduct row.
Why not:
class CustomerCodeReference extends Model
{
public function saleProducts()
{
return $this->hasMany(\App\Models\SaleProduct::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->hasMany(\App\Models\Document::class, 'customer_id', 'customer_id');
}
public function scopeCustomer($query, int $customer)
{
$query->where('customer_id', $customer);
}
}
class Document extends Model
{
public function saleProducts()
{
return $this->hasMany(\App\Models\SaleProduct::class);
}
public function customerCodeReferences()
{
return $this->hasMany(\App\Models\CustomerCodeReference::class, 'customer_id', 'customer_id');
}
}
class SaleProduct extends Model
{
public function customerCodeReferences()
{
return $this->hasMany(\App\Models\CustomerCodeReference::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->belongsTo(\App\Models\Document::class);
}
}
The Project's CustomerCodeReferences can be accessed like this:
$project->customerCodeReference()->customer(1)->get();

How to use Multiple relationship in laravel 5.4

In my app i have define relationship (profile, user, level) but when I fetch data it is showing an error (Trying to get property 'email' of non-object) how can i solve this thank in advance.
this is User Model
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
public function user()
{
return $this->belongsTo(User::class, 'id');
}
public function level()
{
return $this->belongsTo(Level::class, 'id');
}
Level Model
public function profile()
{
return $this->hasOne(Profile::class, 'level_id');
}
This is Controller ProfileController
$users = Profile::with(['user', 'level'])->where('is_bd_partner', 'Yes')->get();
foreach ($users as $key => $value)
{
echo $value->first_name.'<br>';
echo $value->last_name.'<br>';
echo $value->user->email.'<br>';
echo $value->level->level.'<br>';
}
Note that belongsTo takes foreign_key as the first parameter.
So you should change the Profile Model as
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function level()
{
return $this->belongsTo(Level::class, 'level_id');
}
Read more here

How to paginate for hasMany relationship in laravel (App\News::user must return a relationship instance)

I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));

How can I filter this with eloquent

I try to filter SurveyQuestionnaire which have Answer = 'poor' and their Questionnaire have step = 'rating'.
I've tried to look through the documentation of Eloquent and I've found nothing help.
This is my models.
class Questionnaire extends Model {
...
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id');
}
public function answers() {
return $this->hasMany(Answer::class, 'question_id');
}
public function questionnaires() {
return $this->hasMany(QuestionnaireTranslation::class, 'question_id' );
}
}
class SurveyQuestionnaire extends Model {
public function survey() {
return $this->belongsTo(Survey::class ,'survey_id');
}
public function questionnaires() {
return $this->belongsTo(Questionnaire::class, 'question_id');
}
public function answer() {
return $this->belongsTo(Answer::class, 'answer_id');
}
}
Well, the hasMany method returns query builder, so you can simply add some conditions:
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id')->where('Answer', 'poor');
}
Also, you can read this link Constraining Eager Loads and add your conditions manually after taking an instance of your model:
$items = App\Questionnaire::with(['surveysQuestionnaires' => function ($query) {
$query->where('Answer', 'poor');
}])->get();

Laravel return property from third table

I have three tables:
products
product_types
product_categories
products belongs to product_types and product_types belongs to product_categories.
How can I access a column from product_categories from products?:
ProductTypes model:
class ProductTypes extends Eloquent {
public function category() {
return $this->belongsTo('ProductCategories');
}
}
Products model:
class Product extends Eloquent {
protected $table = 'products';
public function brands() {
return $this->belongsTo('ProductBrands', 'brand_id', 'id');
}
public function ages() {
return $this->belongsTo('ProductAges', 'age_id', 'id');
}
public function types() {
return $this->belongsTo('ProductTypes', 'type_id', 'id');
}
public function images() {
return $this->hasMany('ProductImages');
}
public function reviews() {
return $this->hasMany('ProductReviews');
}
public function toArray() {
$ar = $this->attributes;
$ar['brand'] = $this->brand;
$ar['age'] = $this->age;
$ar['type'] = $this->type;
return $ar;
}
public function getBrandAttribute() {
$brands = $this->brands()->first();
return (isset($brands->brand) ? $brands->brand : '');
}
public function getAgeAttribute() {
$ages = $this->ages()->first();
return (isset($ages->age) ? $ages->age : '');
}
public function getTypeAttribute() {
$types = $this->types()->first();
return (isset($types->type) ? $types->type : '');
}
}
I have tried:
$productData->types()->category()->category
But this gives an error saying the method doesn't exist.
Sorry about the title, couldn't think of one.
The problem is, that you're not executing the query when doing types() and therefore you're calling category() on the query builder instance and not the ProductTypes model.
You can use the dynamic properties for accessing the result of the relationship.
$productData->types->category->category
Also consider renaming your relationships. Currently you have "types" for example but it only returns one type, because its a one-to-many relation. "type" would make more sense.

Categories