using join with list in controller laravel 4.2 - laravel

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);

Related

Laravel Eloquent - Getting data from related models and apply union doesn't work

Faced a really strange error and can't understand why it's happening. Please see the explanation below to understand the issue!
What I have:
3 models: Bucket, Template and DesignPack.
Bucket has Many-To-Many(Polymorphic) relationship with Template and DesignPack(It means we have pivot table bucketables).
In essence Bucket can have(be related with) both: Template and DesignPack.
Laravel 6.*
What I want to do:
I want to get all Bucket's Template and DesignPack models merged(using union) with specific columns only. Want to get it with pagination also off course as we can have too many items :)
What I did:
I tried a few solutions but none of the worked, had the same errors. Please see example below what I've tried:
$templates = Bucket::find($bucket_id)->templates()->select('id', 'file_name as name', 'size', 'preview');
$design_packs = Bucket::find($bucket_id)->dps()->select('id', 'name', 'size', 'preview');
$bt_uploads = $templates->union($design_packs)->paginate(20);
return UserUploadResource::collection($bt_uploads);
The error I get constantly is:
"SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT
statements have a different number of columns (SQL: select count() as
aggregate from ((select id, file_name as name, size,
preview, templates., bucketables.bucket_id as
pivot_bucket_id, bucketables.bucketable_id as
pivot_bucketable_id, bucketables.bucketable_type as
pivot_bucketable_type from templates inner join bucketables on
templates.id = bucketables.bucketable_id where
bucketables.bucket_id = 3 and bucketables.bucketable_type =
App\Template and templates.deleted_at is null) union (select id,
name, size, preview from design_packs inner join bucketables
on design_packs.id = bucketables.bucketable_id where
bucketables.bucket_id = 3 and bucketables.bucketable_type =
App\DesignPack and design_packs.deleted_at is null)) as
temp_table)"
Many thanks guys for any help and ideas!

laravel multiple read data

Iam learning laravel.I don't understand how to read multiple data in laravel.
In my Data-table , my value is 1,2,3
my column name is hobby & value is 1,2 . 1 & 2 are related other table, where i stored my hobby name .
suppose ,
id || name
1 || gardening
2 || playing
I want to display
gardening,playing
My controller's Code :-
$interests = DB::table('tbl_interests') ->join('tbl_interest_masters','tbl_interest_masters.interest','=','tbl_interests.id')
->select()
->get();
return view('profile')
->with('interest',$interests);
Don't understand how display data in my view ?
is my code right for read multiple data !!
Note that, i already learn how to read single data with relationship in Laravel.
just remove the select() method from the chain.
$interests = DB::table('tbl_interests')
->join('tbl_interest_masters','tbl_interest_masters.interest','=','tbl_interests.id')
->first();
return view('profile')->with('interest',$interests);
I don't fully understand your question, do you want to return multiple variables?
If so, try this:
return view('YOUR_VIEW', compact('VARIABLE1', 'VARIABLE2'));
If you are using blade, you can use this to display the returned variables:
{{$VARIABLE1}}
Loop through an array:
#foreach($interests as $interest)
{{$interest->name}}
#endforeach

Laravel 5.3 Method distinct does not exist

I am trying to write a query that returns the count of distinct links. However, I keep on getting
Method distinct does not exist
Here is my code
$sample = Sample::all();
$uniqueResumes = $sample->distinct()->select('link')->count();
Please try this :
$uniqueResumes = Sample::select('link')->distinct()->count();

proper usage of queries (Aggregates) in laravel 4.2

im trying to get the total count of records that are less than a value in my table so im using this query in laravel 4.2
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft','<','ModuleCriticalLevel')
->count();
and passes it like this
return View::make('ssims.board' ,compact('title','mTotal','critical'));
//please don't mind the others
then receives it in the view page like this
<div>Modules at critical level <span><strong><?= $critical ?></strong></span></div>
unfortunately, im getting zero whereas in my database, i have 2 records where ModuleCountLeft is less than ModuleCriticalLevel
any ideas?
thanks
Hi turns out that i needed to pluck out the value of ModuleCriticalLevel first here is the code
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
-thumbs up here-

i want normal query to codeigniter query

$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');

Resources