Select count from three tables Laravel 5 - laravel-5

I am building a job website and one of my challenge is like this. I have three tables:
locationlist (field: id, title)
jobpost (field: id, job_title, job_desc,......)
jobpostlocation (field: id, locationlist_id, jobpost_id)
Notice:
locationlist table store list of locations (eg. location 1, location 2, location 3,...)
jobpost table is to post job announcement, which employers can tick more than one locations stored in jobpostlocation table.
Could you tell me how to list all items from locationlist table with count next to each location item like below?
JOBS BY LOCATION:
Location 1 (89)
Location 2 (27)
Location 3 (0)
Location 4 (48)
Location 5 (0)
Location 6 (0)
Location N (....)
My current code:
$q = DB::table('locationlists')
->leftJoin('jobpostlocations', 'locationlists.id', '=', 'jobpostlocations.location_id')
->leftJoin('jobposts', 'jobposts.id', '=', 'jobpostlocations.jobpost_id')
->get();

You can do this setting a user defined variable using SET keyword
For Example :
SET #count=0; #create a variable for counter
SELECT location #count:=#count+1, * FROM location_list;
#Increment variable by adding 1 to it.
Let me know if this works for you.

Related

Complex count row etl requirement

I have a requirement related as below
1-If there is employee record then count the number of rows
a-if there are four rows then follow the layout 1,
and populate the column1 and column 2 with values in report and ltrimrtrim
b- if there are three rows, then follow the layout 2,
and hardcode the column 1 and column 2 with NULL
Otherwise, look for the employee record.
Couldn't get the logic, I used the router with as if column 1 and two null the send to layout two else 1. But the requirement is different.
router transformation, if null, layout one else 2
Step 1 - Use SRT>AGG>JNR to calculate count. create new column as count_all and set to COUNT(*). Please group by proper key columns.
Step 2 - Use RTR next to split data based on your condition.
group 1- count_all =4 then follow the layout 1 and...
group 2- count_all =3 then follow the layout 2 and...
group 3 - if count <3 then do employee record.

Laravel: get an item that exists a given number of times on database?

I have a table named contact. In that table, there is a column named phone number.
What I really want to do is get a phone number that exists 2 or 3 times on the table. Is there any way to do that using eloquent?
You could use having count:
$numbers = DB::table('contact')
->select('phone_number')
->groupBy('phone_number')
->havingRaw('COUNT(phone_number) > 1')
->get();

Laravel Relation Many to Many

I'm using laravel and sentinel (Sentry) that default using users, groups, and users_groups database table
users
-----------------
id name
1 user
2 user
3 admin
groups
-------------------
id name
1 Users
2 Admins
users_groups
--------------------
user_id group_id
1 1
2 1
3 1
3 2
i define the relation on laravel model and it's work fine when i try to print
$users = Group::find($id)->user;
but how to print just user only?
$users = Group::where(array("name"=>"Users"))->get()->user;
it's still show all data include the admins, i just want to show user only, how to do this?
i know the problem because at users_groups table admin have Users id too..
Please someone help me. It's verry confusing
You can use with to get the data of specified relation as below :
$users = Group::with('user')->find($id);
This will return data of group with user.
If you just want a data of only user table you have to change the query and rather than using model Group you have need to query on User model something like below:
$users = User::where('group_id', $groupId);
http://laravel.com/docs/5.1/eloquent-relationships
http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Model.html#method_with

Count Length and then Count those records.

I am trying to create a view that displays size (char) of LastName and the total number of records whose last name has that size. So far I have:
SELECT LENGTH(LastName) AS Name_Size
FROM Table
ORDER BY Name_Size;
I need to add something like
COUNT(LENGTH(LastName)) AS Students
This is giving me an error. Do I need to add a GROUP BY command? I need the view:
Name_Size Students
3 11
4 24
5 42
SELECT LENGTH(LastName) as Name_Size, COUNT(*) as Students
FROM Table
GROUP BY Name_Size
ORDER BY Name_Size;
You may have to change the group by and order by to LENGTH(LastName) as not all SQL engines let you reference an alias from the select statement in a clause on that same statement.
HTH,
Eric

Laravel 4 - save order in pivot table

I have two tables actividadsand fichas... and a pivot table actividad_ficha. I have created another column in my pivot table: orden_actividad where I would like to save the order of the 'activities' in every 'ficha'.
My main problem is that I have no idea how to create an order in my pivot table. I mean, for example, ficha 4 is related in my pivot table with actividad 1, actividad 6 and actividad 9, and I want to create the order of the activities:
ficha - actividad - orden_actividad
4 1 1
4 6 2
4 9 3
My purpose is, after that, order the activities by 'orden_actividad' and allow the user to reorder them.
Any idea how to do that?
You can add values to custom columns in your pivot table like this:
$actividad_id = /* fetch $actividad_id here */;
$orden_actividad = $ficha->actividad->count() + 1;
$ficha->actividad()->attach($actividad_id, array('orden_actividad', $orden_actividad));

Resources