Laravel Getting Child table values with multiple Parent values - laravel

I need your help to find a solution in laravel 5. So let me explain my problem I have 4 tables lets suppose these are users, table1, table2, table3.
users and table1 have many to many relationship.
then table1 and table2 have many to many relationship.
and then table2 and table3 have many to many relationship.
Now on table3 controller i want to get all values with respect to a user. for example
$table1 = User::find(Auth::user()->id)->table1()->get()->lists('id');
$table2 = table1::whereIn('id',$table1)->table2()->get()->list('id');
$table3 = table2::whereIn('id',$table2)->table3()->get();
On table2 data i am getting the error of badmethodexception. I have googled and read the documentation but unable to get anywhere.
Can anyone help me how can i get the data please?
I have pivot table of table1_user, table1_table2 and table2_table3.
In user model i have function
public function table1(){
$this->blongstomany('App\table1');
}
In Table1 Model i have function
public function table2(){
$this->belongstomany('App\table2');
}
In Table2 Model i have function
public function table3(){
$this->belongstomany('App\table3');
}
I hope now it make sense.

Related

How to count for a substring over two tables in codeigniter

I have two tables
table1
id|name|next_field
table2
id|id_of_table1|whatelse
I do a msql query to get all entries of table1 and the number of entries in table2 who has table2.id_of_table1 = table1.id
This is my query - it works fine.
$select =array('table1.*', 'COUNT(table2.id) AS `my_count_result`',);
$this->db->select($select);
if($id!=false){ $this->db->where('id',$id); }
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id_of_table1');
return $this->db->get()->result_array();
Now I have another field which has coma-separated data
table1.next_field = info1, info2, next,...
Now I want to check in the same way like the first query how often for example "info2" is as a part inside the table1.next_field
Is it possible, how?
After all i decide to change my database structure to make the work and handle much easier.

Crudbooster laravel - How to make different column relationship?

I have two table 1. table_a 2. table_b
And all table have company_id. I want to make relationship among this two table using same company id . Like
select * from table_a left join table_b on table_a.company_id = table_b.company_id;
Please help me out if its really possible. Thank you in advance.
You can use this:
$data = DB::table('table_a')->leftJoin('table_b', 'table_a.company_id', '=', 'table_b.company_id')->get();
Or raw:
$users = DB::table('table_a')
->select(DB::raw('query here'))
->get();
More on:
https://laravel.com/docs/5.7/queries

Laravel Eloquent select function cause empty relation

Following is my query
$user = User::select(['uuid','name','about'])->with(['education','work'])->first();
this returns empty data for relationship education and work,
but if I remove select function from query I am getting data in relationship and it also returns all columns of user table which I don't want.
how can solve this problem
The problem is that relationships (with(...)) execute an additional query to get the related results. Let's say you have one to many relationship where users have many works. User::with('work')->find(1) will then execute these 2 queries:
select user where id = 1 and select works where user_id = 1.
So basically in order to be able to execute the second query (fetch relationship data) you need to include id (or whichever column you're referencing) in you select statement.
Fix:
$user = User::select(['uuid','name','about', 'id'])->with(['education','work'])->first();
Same principle in different forms applies to all relationships. For example in the inverse of hasMany which is belongsTo you would need to select the foreign key (for example user_id).

LINQ Join and performing aggregate functions

I am facing issue in writing LINQ query to perform join on three tables and then performing aggregate functions on the rows. Kindly do provide some help.
I have three tables
Table 1: Students (Id, Name)
Table 2: Subject (SubID, Title, Id)
Table 3: Grade (Id, SubID, marks)
I have to write LINQ query to get the results as following
Count of Students table rows
Count of Grade table rows
Sum of
marks of all rows in Grade table
I am writing query as following but it is not up to the mark as i feel it is not correct.
var _Count = from student in _context.Students
join subject in _context.Subject on student.Id equals subject.Id
join grade in _context.Grade on subject.SubID equals grade.SubID
// How to group them?
select new { //How to take and return the counts?};

Eloquent Subquery

I have a many to many relationship between a student table and a apparatus table (A student performs on many apparatuses and an apparatus has many students). I have a student_results table that has a composite primary key (student_id and apparatus_id) and a 3rd field called results).
I have written a query to find all the apparatuses that a student DOES NOT have a result. The example I give is for student with id = 121.
The sub-query is.
SELECT apparatus_id, strapparatus_name FROM apparatuss
WHERE apparatus_id <> ALL(SELECT apparatus_id FROM student_results
WHERE student_id =' . 121 . ')';
I would like to write this using Eloquent (Laravel 4.1).
Any help greatly appreciated.
Assuming that you already have your Eloquent models Apparatus and StudentResult set, this is a way:
Apparatus::whereNotIn(
'apparatus_id',
StudentResult::where('student_id', 121)->lists('apparatus_id')
)->get('apparatus_id', 'strapparatus_name');
Or, if you don't have a model for your student_results table, you can just:
Apparatus::whereNotIn(
'apparatus_id',
DB::table('student_results')->where('student_id', 121)->lists('apparatus_id');
)->get(array('apparatus_id', 'strapparatus_name'));

Resources