hi im fairly new in laravel 4.2 so i have this table (table 1) which gets data from a related table (table 2) but table 2 also gets from another related table (table 3) here is a visualization
im using laravel 4.2 query builder for this here is my sample code on how im connecting table 1 and table 2
$records = DB::table('table1')
->join('table2', 'table1.someID', '=', 'table2.someID')
->select('select something')
->get();
my problem is i don't know how to get the values from table 3 is there a way for this?
any help would be appreciated
Umm.. I don't really mind but, here you go. You just have to put another join to get the values to the third table from the table 2 judging from the visual above.
$records = DB::table('table1')
->join('table2', 'table1.someID', '=', 'table2.someID')
->join('table3', 'table3.someID', '=', 'table2.someID')
->select('select something')
->get();
Related
Laravel code:
$posts = array();
$allPosts = DB::table('post_categories')
->Join('posts', 'posts.id', '=', 'post_categories.posts_id')
->select('posts.title','posts.id','posts.body','posts.created_at')
->where('post_categories.categories_id','!=',5)
->orderBy('posts.created_at','desc')
->get();
foreach ($allPosts as $post){
$categories = DB::table('post_categories')
->Join('categories', 'categories.id', '=', 'post_categories.categories_id')
->select('categories.name','categories.id')
->where('post_categories.posts_id','=',$post->id)
->get();
$post->categories = $categories;
array_push($posts,$post);
}
Model relations:
Posts 1 - m post_categories m - 1 categories
first query is used fetch posts without category number 5
second is used to fetch categories in a post.
problem is i have a n+1 query due to the for loop.
so i was wondering if there was a better way to do this without the for loop
debugbar result:
screenshot from debugbar
post_categories looks like a many:many mapping table. If so follow the indexing advice in http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table
Also, posts needs INDEX(created_at).
For further discussion, please provide SHOW CREATE TABLE and EXPLAIN SELECT ...
I'm using Laravel Query Builder and my join statement working perfectly.
User table columns:
name|email|phone|gender
School_Abouts table columns:
courses|boards|contact|location|teachers
Currently I do the select query as below:
$school=User::join('school_abouts', 'users.id', '=', 'school_abouts.school_id')
->where('users.id',$id)
->select('users.name',
'users.email',
'users.phone',
'school_abouts.courses',
'school_abouts.boards',
'school_abouts.contact',
'school_abouts.location',
'school_abouts.teachers')
->first();
To select the columns from school_about table I have to write table name multiple times. But is there any way to pass an array of columns instead? I tried this but failed:
->select('users.name',
'users.email',
'users.phone',
'school_abouts'.[courses,boards,location,contact,teachers],
)
You can safely remove table name from columns as there is no column name common in both tables but else, as I see, you are trying to get almost all columns from both tables which could be simplified using *:
$school = User::join('school_abouts', 'users.id', '=', 'school_abouts.school_id')
->where('users.id', $id)
->select('users.*', 'school_abouts.*')
->first();
However, if you want to get some columns and their names could make an ambiguity then prefixing column names with table name is a must. To make it shorter you could use aliasing:
$school = User::join('school_abouts AS sa', 'users.id', '=', 'sa.school_id')
->where('users.id', $id)
->select('users.name',
'sa.courses',
'sa.boards',
'sa.contact',
'sa.location')
->first();
I have a question about Laravel queries, so I have a upload Model and database table which stores all my images. Then there is an Activity Model and database table which stores all my activities. For each activity I want a image.
So my Activity model has a 'uploads_id' column. I wrote the query like this:
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->where('uploads.id', '=', 'activities.upload_id')
->get();
It cannot find the right image what am I doing wrong?
You are "joining" twice in your join and where clause. You connect uploads.id and activities.upload_id one time in the join and again in the where clause.
If you want to query for a special upload.id, your query should look like this:
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->where('uploads.id', '=', '<yourUploadID>')
->get();
If you want all images, you can delete the where statement.
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->get();
If you use join an inner join will be used.
You could also use leftJoin if you want all entries from the table uploads.
$activity_images = DB::table('uploads')
->leftJoin('activities', 'uploads.id', '=', 'activities.upload_id')
->get();
Does it solve your issue?
I am new to laravel. I'm working on this laravel 5 app but got stuck on trying to perform a query like below.
update applications
set application_status = 'sa'
where id in (select application_id from application_cart where cart_id = 12);
Appreciate help on how to translate this query.
The query below does the trick
return Application::whereIn('id', function($query) use ($id){
$query->from('application_cart')
->select('application_id')->where('cart_id', $id);
})->update(['application_status' => 'sa']);
Hi,
I need a help related to Codeigniter very badly.
I have some problems with a database query, i'am new to Codeigniter.
I have 3 tables:
1) film(film_id, filmname) PK (film_id)
2) category(category_id, categoryname) PK (category_id)
3) film_category(contains both primary keys (film_id) and (category_id))
The problem is that I want to select all filmname from film tables where category_id = 3.
How to do this with active record class?
Please make a suggestion.
I'am new to Codeigniter and I love it.
Thank you in advance from your friend.
Something like that:
$this->db->select('f.filmname');
$this->db->join('film_category fc', 'fc.film_id = f.film_id');
$this->db->where('fc.category_id', 3);
$query = $this->db->get('film f');
I find the ActiveRecord one of the most useful and elegant parts of CodeIgniter.
select data from the database in descending order
$this->db->order_by("id","desc");
$query = $this->db->get('table_name');
return $query->result();