$this->db->select('movie_details.movie_id','movie_details.title','movie_details.producer','movie_details.director'.'movie_details.writer','GROUP_CONCAT(movie_genre.genre) As genre','movie_genre.movie_id');
$this->db->from('movie_details');
$this->db->join('movie_genre','movie_genre.movie_id=movie_details.movie_id');
$result=$this->db->get();
return $result->result_array();
I give one query in normal mysql. but i want this query in codeigniter form because i want to learn codeigniter.
$query="select movie_details.movie_id,movie_details.title,movie_details.producer,movie_details.director,movie_details.writer,
GROUP_CONCAT(movie_genre.genre) As genre ,
movie_genre.movie_id
FROM
movie_details,
movie_genre
WHERE
movie_genre.movie_id=movie_details.movie_id
GROUP BY
movie_genre.movie_id limit 110,15";
$this->db->select('movie_details.movie_id','movie_details.title','movie_details.producer','movie_details.director'.'movie_details.writer','GROUP_CONCAT(movie_genre.genre) As genre','movie_genre.movie_id');
//This is not right way to CI select.It will select only movie_id
use this
$this->db->select('movie_details.movie_id , movie_details.title , movie_details.producer , movie_details.director , movie_details.writer , GROUP_CONCAT(movie_genre.genre) genre , movie_genre.movie_id');
Related
Laravel Version: 8.0
PHP Version: 7.3.0
Database Driver & Version: MySQL 5.7.34
Describe the Bug
When I use paginate for pagination the data it call the count( * ) query every time even I pass the column name like count(['id']), and the count( * ) query scan all row in the table.
Table name is users and it has 45 column
Route
Route::get("users", "UsersController#index");
Controller
namespace App\Http\Controllers\Api\V1;
class UsersController extends Controller
{
public function index()
{
return User::paginate(10 , ['id']);
}
}
Call users route
Telescope showing me that two queries
Actual result
Expected result
Steps To Solution:
The following image shows that I had done changes as per our functionality, It will take the first column name from the passed in the paginate method array of $columns params and that query does not scan all columns of the users tables.
Final Results:
I have tired to solving this issue any other way or idea then please let me know
Its not recommended to ever touch the vendor files, you can always just override the functionality inside of your model, you can also pass in the columns to override the getCountForPagination() and you can also pass the columns to simplePaginate() that doesn't invoke any counting!
In order to optimize the query to count and paginate, you can do it like this:
//We will call the query on the model
$program = Program::query()->getQuery();
//count the query by specific columns
$thePaginationCount = $program->getCountForPagination(['id']);
//paginate the results using simplePaginate and select the columns we want:
$thePaginatedProgram = $program->simplePaginate(10, ['id', 'name']);
return 'test: '.$thePaginatedProgram;
Will result like this:
select count(`id`) as aggregate from `programs`
select `id`, `name` from `programs` limit 11 offset 0
As you can see it will only load what we specify and its very efficient!
Final Note:
If you just want to paginate without the count, you can always call Model::simplePaginate($amount, [$columns...])
https://laravel.com/docs/9.x/pagination#simple-pagination
Like I have a page where data are showing like first name, middle name, last name, address, city, state, country, age, salary.
There are a filter implemented that have 4 fields like city, age, salary, state. now I have to made a controller in Spring boot that takes all 4 fields as input params and find data from database using Spring Data JPA.
But my problem is that I want to filter Data sometime by salary only, sometime by city, state, sometime with all 4 params.
So what will be controller code and JPA Repository query to do this filter process.
Please Help me
Thanks in Advance
test if your parameters are null :
"where (:name is null or name = :name) and (:city is null or city = :city)"
I would suggest to use JPA Specification instead,
to know more on specification code here is my thread on stackoverflow
If you want to stick with JPA then here's the query which can help you
where (:field1 is null or table.field1 = :field1)
and (:field2 is null or table.field2 = :field2)
...
...
...
Explanation: The above code will check if the parameter is null, then return true otherwise compare with the column.
You can modify Query with below clause Where you can write query in Repository and in
where clause you can add conditions. So this will work as expected.
#Query(value = "SELECT user from User user WHERE ((:name IS NULL) OR (user.name = :name)) AND ((:city IS NULL) OR (user.city = :city)))
I have a Laravel Controller method with the following code:
$listings = LikedListing::where('user_id', '=', $user->id)
->with('Listing.Photos')
->get();
This should return a collection of LikedListing records with Photos attached to each likedlisting record.
I have this SQL Query I need to inject into each record as well:
select u.id, l.address, u.first_name, u.last_name, ll.score
from listings l
left join liked_listings ll on ll.listing_id = l.id
left join users u on u.id = ll.user_id
where u.id in (
select secondary_user
from user_relationships
where primary_user = $primaryUser
)
and ll.listing_id = $listingId
Where $primaryUser = $user->id And $listingId is equal to the listingid inside each record in the collection.
I have absolutely no idea how to do this.
Maybe theres a model way of performing this? UserRelationship model has a primary_user column, which connects to a $user->id, and there is a secondary_user column, which acts like a follower userid, which is what we need in the final result (a list of all related users per listing)`
Can someone who has much far superior knowledge with Laravel please assist
My goal is to have the current collection of listing records with associated photos as well as following users (secondary_user) from the user_relationship table related to the primary_user (the logged in user) who have a record using user_id with the secondary_user value for that listing in the likedlisting table (obv assoicated with the listing_id). I already provided a raw sql query if thats the only option.
So in simple terms all related users who have liked a listing that the primary user has liked as well should be added to each listing record
What is the equivalent query of this in Laravel 5.4
select id,name
from friends
order by case when id in (5,15,25) then -1 else id end,id
//Controller namespace
use DB;
use App\Friend;
//In controller method
$friends = Friend::select('id', 'name')
->orderBy(DB::raw('case when id in (5,15,25) then -1 else id end,id'))
->get();
You can use raw() to make a raw SQL query, instead of using Eloquent's query builder. Here is the documentation for it.
If it helps, here is a similar question asked in Laravel 4; old but still relevant.
I hope it helps!
There are multiple ways to achieve this.
If you want to use Laravel Query Builder:
$result = DB::select('
select id, name
from friends
order by case when id in (5,15,25) then -1 else id end, id
');
If you want to use Laravel Eloquent, in conjunction with Query Builder:
$result = \App\Friend::select('id', 'name')
->orderBy(DB::raw('case when id in (5,15,25) then -1 else id end'), 'id')->get();
You can pass a raw portion of the query instead of building the whole thing like that. So doing:
DB::table('your_table')
->orderBy(
DB::raw('case when id in (5,15,25) then -1 else id end,id')
)->get()
Will return first the records with an id of 5,15 and 25 (if they exists), then the rest ordered by id. Not sure if it's what you were after.
hi so im trying to create a dropdown that is based form a sql joined query so far here is what i have (got a help from fellow stackoverflow-ers)
$cats = DB::table('nsa_subcategory')
->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')
->lists(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname)'),'nsa_subcategory.subcategoryid');
what im trying to do was join 2 tables display the subcategory and maic category but the value i would get is the subcategory id the code above produces this kind of error
any ideas what im doing wrong or any ideas on how to improve my code? thanks so much in advance!
Instead of putting the DB::raw in the ->lists, put it in your select and give it a name as in the code below category, then you retrieve it using ->lists
$cats = DB::table('nsa_subcategory')
->select(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname) AS category'),'nsa_subcategory.subcategoryid')
->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')
->lists(category, subcategoryid);