I'm building a sort of human resources application where I have to reduce a variable of a mysql table with a variable of another mysql table.
The CMV I'm using is Laravel so I'm trying to use the DB class as much as possible.
This is the first table, users:
users_table
And the second table, aanvraags:
aanvraags_table
I would like reduce the variable vakantie from the first table with the variable duur from the second table. This would happen right after the vacation request is accepted. There is already a function in my controller to change the variable goedgekeurd from 0 to 1. The function looks like this:
public function updateNieuw($id){
//update aanvraag
$update_aanvraag = DB::table('aanvraags')
->where('id', $id)
->update([
'goedgekeurd' => 1 //approved by the boss
]);
Within that same function I would like to reduce the variable vakantie with the variable duur.
At the moment I already tried a few things like:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->get();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $update_vakantiedag
]);
All of this doesn't get me anywhere. Most of the time I get errors or the value from the variable vakantie from the first table.
It is quite important for the application that the reduction takes place. The variable vakantie displays the remaining vacation days per user after accepting a new request and is displayed in the app.
Any tips or solutions are welcome !
Thank you all in advance !
Use this:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->decrement('users.vakantie', $update_vakantiedag->duur);
#JonasStaudenmeir Thanks a lot ! I changed the function and it works !
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$verlof = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->first();`
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $verlof->vakantie - $update_vakantiedag->duur
]);
Thank you so much !
Related
#foreach($users as $user)
<div>
{{ $user->username }}
</div>
#endforeach
public function group($id){
if (!$this->secure($id) && $this->secure($id)) return redirect('/404');
$user = Auth::user();
$hobbies = Hobby::all();
$group = $this->group;
$users = DB::table('users')
->join('user_hobbies', 'users.id', '=', 'user_hobbies.user_id')
->where('user_hobbies.hobby_id' == $group_id)
->select('users.*')->orderBy('first_name', 'ASC');
$users = $users->get();
$wall = [
'new_post_group_id' => $group->id
];
return view('groups.group', compact('user', 'users', 'group','wall', 'hobbies'));
}
Hey guys, complete beginner here.. Im trying to access and return the users information of the members of a group. I am not so sure where I am going wrong and I'm trying to understand the log instructions but I can't quite understand where I'm going wrong. Ive seen "Sql error column doesn't exist in where clause".. tried to change the code and finding undeclared variables too.
The system is that a user creates a hobby and that hobby becomes the group. ie 'my hobby is skiing and then a group of skiing is created'. So I have a user_hobbies model of course and everything is work, but I can't quite work out how to access the information of the user from the DB and display it in the blade.
Any help appreciated.
You have typo on query builder.
$users = DB::table('users')
->join('user_hobbies', 'users.id', '=', 'user_hobbies.user_id')
->where('user_hobbies.hobby_id' == $group_id) // This line
->select('users.*')->orderBy('first_name', 'ASC');
It should be
$users = DB::table('users')
->join('user_hobbies', 'users.id', '=', 'user_hobbies.user_id')
->where('user_hobbies.hobby_id', $group_id)
->select('users.*')->orderBy('first_name', 'ASC');
And make sure you use === instead of == for explicit comparation in PHP. It will cause a Type Juggling. I assume your blade file is correct since i dont know what's inside the $user object
I'm trying to get this MySQL query to work in laravel 5.7 query builder.
It works fine in phpmyadmin
SELECT c.Symbol
, s.SectorName
, cprs.strenght
, s.parentid
, ssbpi.Risklevel
, ssbpi.ColumnType
FROM Companies AS c
JOIN Sectors AS s ON s.SectorID = c.SectorID
JOIN Company_PriceRS AS cprs ON cprs.CompanyID = c.CompanyID
JOIN SubSectorsBPIsData AS ssbpi ON ssbpi.subcategoryid = s.parentid
WHERE cprs.PostDate = '2017-05-08'
AND WHERE CompanyPriceRS.strenght = 'strong'
AND WHERE SubSectorsBPIsData.ColumnType = $ColumnType
ColumnType is a variable that comes from a dropdown and it's already being captured and working properly.
I have tried the normal way according to documentation:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')
->where('CompanyPriceRS.strenght', '=', 'strong')
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType)
->where('CompanyPriceRS.Postdate', '=', '2017-05-08');
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
Error:
Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "syntax error, unexpected '->' (T_OBJECT_OPERATOR)"
Using functions With several nested joins:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', function ($join) use ($ColumnType) {
$join->on('Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', function ($join2) {
$join2->on('CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->join('SubSectorsBPIsData', function ($join3) {
$join3->on('SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')
->where(function ($query1) {
$query1->where('CompanyPriceRS.strenght', '=', 'strong') //filter 1
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType) //filter2
->where('CompanyPriceRS.Postdate', '=', '2017-05-08'); // filter 3
});
});
});
})
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
Error:
ErrorException (E_NOTICE)
Undefined variable: ColumnType
3: Then tried functions with nested WHERE
$Completequerytry1 = DB::table('Companies')
->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid') //ERROR IS GIVEN ON THIS LINE
->where(function ($query1) {
$query1->where('CompanyPriceRS.strenght', '=', 'strong')
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType)
->where('CompanyPriceRS.Postdate', '=', '2017-05-08');
});
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
Error:
Undefined variable: join
Still don't know what i'm missing.
Should i create functions for the JOINs and the WHEREs ?
Running out of ideas. Thanks in advance for your insights :)
It turns out that the query was being sent to a variable directly from the controller, not to the view.
I was doing this only for testing but it was using too much memory with print_r(); function making impossible to get a result. Even when using dd(); i wasn't getting what i wanted (I think it's because some sintax errors i had).
So i passed the final variable to the view and it works fine since laravel could handle the data differently.
Also i used ->limit(10); to split the results and avoid memory overload.
Here is the final code working:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
->join('Company_PriceRS', 'Company_PriceRS.CompanyID', '=', 'Companies.CompanyID')
->join('SubSectorsBPIsData', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')
->select('Sectors.SectorName', 'Companies.Symbol', 'Company_PriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.Risklevel','SubSectorsBPIsData.ColumnType')
->where('Company_PriceRS.strenght', '=', 'strong')
->where('SubSectorsBPIsData.ColumnType', '=','X')
->where('Company_PriceRS.Postdate', '=', '2017-05-08')
->limit(10)
->get();
return view('besttrades2', array('Completequerytry1' => $Completequerytry1)); //sending my query variable to the view
Then in the view only used:
<?= $Completequerytry1; ?>
or show it in whatever way you want.
Sometimes it's better to have another project only for testing separately.
I have two table 1)clients 2)orders. I trying to make leftjoin but I not getting correct result what i want. Please see image below.
Thanks
ClientController.php
$clients = Client::leftjoin('orders','orders.client_id','=','clients.id')
->select(DB::raw("count(orders.id) as orderCount"),
'clients.name as name','clients.email as email',
'clients.phone as phone','clients.country as country',
'orders.total_amount')
->where('orders.status',0)
->get();
$myevents = DB::table('users')
->join('event_model_user', 'users.id', '=', 'event_model_user.user_id')
->rightJoin('events', 'event_model_user.event_model_id', '=', 'events.id')
->where('users.id','=',$user->id)
->orWhere('events.bookable','=',$user->zipcode)
->get();
this is an example of i used a right join to get the events where no user has done any booking!
in your case this would be something like (not tested)
$myevents = DB::table('clients')
->leftJoin('orders','orders.client_id','=','clients.id')
->where('orders.status',0)
->get();
I am having problems building a query which delimit the number of lawyers by branch of law in one particular territory(province) of the country.
I have four tables, one with the branchlaws, one for users, one where lawyers add branches where the practice law and one for territories.
I am able to summarize all this information with the following query:
$lawyersbyprovince = DB::table('branchlawsubareas')
->leftJoin('lawyerbranches', function($join) {
$join->on( 'branchlawsubareas.id', '=', 'lawyerbranches.subarea_id');
})
->leftJoin('users', function($join) {
$join->on( 'lawyerbranches.user_id', '=', 'users.id');
})
->leftJoin('states', function($join) {
$join->on('users.working_province', '=', 'states.id_state');
})
->leftJoin('branchlaws', 'branchlawsubareas.area_id', '=', 'branchlaws.id')
->select('branchlawsubareas.name as subarea',
DB::raw('count(lawyerbranches.subarea_id) as total'),
'branchlawsubareas.id'
)
->where('states.id_state','=', $province)
->groupBy('branchlawsubareas.id')
->get();
This query returns the number of lawyers per branch of law but it does not delimit by province. I have spent enough time trying things but obviously I missing something.
Any help will be much appreciated.
Try not to join instead the states of leftjoin.
$lawyersbyprovince = DB::table('branchlawsubareas')
->leftJoin('lawyerbranches', 'branchlawsubareas.id', '=', 'lawyerbranches.subarea_id')
->leftJoin('users', 'lawyerbranches.user_id', '=', 'users.id')
->leftJoin('branchlaws', 'branchlawsubareas.area_id', '=', 'branchlaws.id')
->join('states', 'users.working_province', '=', 'states.id_state')
->where('states.id_state','=', $province)
->select('branchlawsubareas.name as subarea',
DB::raw('count(lawyerbranches.subarea_id) as total'),
'branchlawsubareas.id'
)
->groupBy('branchlawsubareas.id')
->get();
Now if this does not work, you have another way. Remove where() clause and change your select like this
...
->select(''branchlawsubareas.id', 'states.id as state_id')
...
This will return a collection with ALL the data. Then you can manipulate that collection like this
$newCollection = $resultFromQuery->where('state_id', $province);
$newCollection->count(); //number of laywer in that state
I hope this helps, or simply gives you a hint.
I am trying to pick out Shops with the specified $item_id.
An Item can have multiple Images, but I only want the first one.
The current code gives me only the first image, but it duplicates the item as many times as there are images.
Here's the code:
$shops = Shop::with(array('items' => function($query) use ($item_id) {
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', 'items.id', '=', 'items_images.item_id');
$query->where('items.id', '=', $item_id);
}))
->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
And the current output:
How can I avoid getting all the duplicates?
I think this will work.. I haven't done much at all with this syntax but I think this is how it would work (assuming main is in the items_images table)
$shops = Shop::with(array('items' => function($query) use ($item_id)
{
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', function($join)
{
$join->on('items.id', '=', 'items_images.item_id')
->where('items_images.main', '=', 1);
})
$query->where('items.id', '=', $item_id);
}))->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
Found the DOCS on it... if you want to include it in the JOIN then it looks like you have to add a where to the JOIN