I have a category name A with path: 1-2-
and some children with path like:
B: 1-2-3-
C: 1-2-4-
D: 1-2-5-
I want to move category A from 1 to 6 with path: 6-2-
How can I update categories B,C,D to 6-2-%
I found this code for php:
UPDATE category SET path = REPLACE(path, '1-2-', '6-2-') WHERE path LIKE '1-2-%'
but I don't know how to use it for laravel.
You can do it like this:
DB::table("category")
->where("path", "LIKE", "1-2-%")
->update(["path" => DB::raw("REPLACE(path, '1-2-', '6-2-')")]);
Or, If you have Model for category table:
Category::query()
->where("path", "LIKE", "1-2-%")
->update(["path" => DB::raw("REPLACE(path, '1-2-', '6-2-')")]);
if this code works you can use
DB::raw('UPDATE category SET path = REPLACE(path, '1-2-', '6-2-') WHERE path LIKE '1-2-%');
Related
I have created an author user in surreal DB like this:
CREATE author:shivam SET
name.first = 'Shivam',
name.last = 'Sahil',
name.full = string::join(' ', name.first, name.last),
age = 23,
admin = true,
signup_at = time::now()
;
Now I want to query all the authors, but when I do:
SELECT * FROM author;
I get 0 results.
Is it supposed to be queried in some other way?
What you did should work well as you can see here
Don't forget to start your server with something like this for example
surreal start --log info --user root --pass root memory
and log-in as in my above screenshot with:
surreal sql --conn http://localhost:8000 --ns yoloswag --db compabie
Then, your create query and select should work well!
Alternatively, you can give a try to that video or documentation's quick start.
Creating another author like this is properly fetched afterwards.
CREATE author:123 SET
name.first = 'bob',
name.last = 'hoh',
name.full = string::join(' ', name.first, name.last),
age = 30,
admin = false,
signup_at = time::now()
;
Don't forget that 123 is supposed to be a unique ID (a string should be fine tho).
I need to get names from destinations table for each from_destination_id and to_destination_id
as fromDestinationName and toDestinationName
$bookingTransfersData = DB::table('transfers as t')
->select('t.periodStart as periodStart', 't.periodEnd as periodEnd','t.days','t.transfer_id','t.cost_round_trip',
't.cost_one_way','t.status','d.destination_id as destinationId','d.name as destinationName', 't.type',
'tf.name as officeName', 'ag.name as agencyName', 'u.name as userName', 'v.name as vehicleName')
->join('destinations as d', function ($join){
$join->on('t.from_destination_id','=','d.destination_id')
->orOn('t.to_destination_id','=','d.destination_id');
})->join('vehicles as v','t.vehicle_id','=','v.vehicle_id')
->join('transfer_offices as tf','t.office_id','=','tf.transfer_office_id')
->join('agencies as ag','t.forAgency_id','=','ag.agency_id')
->join('users as u','t.addedBy_user_id','=','u.id')
->get();
i want to get the names of each id after this results
$searchResults = $bookingTransfersData
->where('periodStart','between', $periodStart && $periodEnd)
->where('periodEnd','between', $periodStart && $periodEnd)
->where('destinationName','=',$from_destination_name && $to_destination_name)->where('type','like', $type);
like:
$fromDestinationName = $searchResults->pluck('from_destination_id','destinationName')
->where('from_destination_id','=','destinationId');
but $fromDestinationName return an empty collection
please help :)
I solved it by removing this join:
->join('destinations as d', function ($join){
$join->on('t.from_destination_id','=','d.destination_id')
->orOn('t.to_destination_id','=','d.destination_id');
})
and add for each destionation_id a join to retrive each name
and this will not work if I don't add for table name that i joined two times as to name it new name like
'destinations as d1' and 'destinations as d2'
$bookingTransfersData = DB::table('transfers as t')
->select('t.periodStart as periodStart', 't.periodEnd as periodEnd','t.days','t.transfer_id','t.cost_round_trip',
't.cost_one_way','t.status','d1.destination_id as fromDestinationId','d1.name as fromDestinationName', 't.type',
't.to_destination_id','tf.name as officeName', 'ag.name as agencyName', 'u.name as userName', 'v.name as vehicleName',
't.from_destination_id', 'd2.destination_id as toDestinationId','d2.name as toDestinationName')
->join('destinations as d1','t.from_destination_id','=','d1.destination_id')
->join('destinations as d2','t.to_destination_id','=','d2.destination_id')
->join('vehicles as v','t.vehicle_id','=','v.vehicle_id')
->join('transfer_offices as tf','t.office_id','=','tf.transfer_office_id')
->join('agencies as ag','t.forAgency_id','=','ag.agency_id')
->join('users as u','t.addedBy_user_id','=','u.id')->get();
the problem solved :)
I want to combined the duplicate row to one row and show the result in front end but i face some error please any one help .
time_session | day
------------ ----
1 saturday
2 friday
2 friday
3 wednessday
this is my controller code:
public function classSchedule()
{
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*')
->groupBy('applications.time_session')
->get();
return view('backEnd.classes.manageClass1',['duplicates'=>$duplicates]);}
this is my front end code:
<td> {{$duplicate->time_session }} </td>
time_session | day taking day
------------ ---- ----------
1 saturday 1
2 friday 2
3 wednessday 1
SQLSTATE[42000]: Syntax error or access violation: 1055 'pixelmetro.applications.id' isn't in GROUP BY (SQL: select `applications`.*, `batches`.* from `applications` inner join `batches` on `applications`.`time_session` = `batches`.`id` group by `applications`.`time_session`)
Actual out put :
https://www.facebook.com/photo.php?fbid=2317443465016247&set=pcb.442972496253366&type=3&theater&ifg=1
This 1055 error is a MySQL issue that can be resolved changing Laravel config. Go to config/database.php file and change mysql property strict from true to false. 'strict' => false,
If you don't want to change it, then you can add a new property modes
'modes' => [
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
]
And yeah don't forget to run the command php artisan config:cache after making changes.
To get desired result you need to use count.
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*', DB::raw('COUNT(applications.time_session) as counter')
->groupBy('applications.time_session')
->get();
In blade:
#foreach($duplicates as $duplicate)
<td>$duplicate->counter</td>
#endforeach
i think you have some miss conception on group by.if you group by with a certain column then you have to get others column with aggregate function.
in your case you made group by with (time_session) and you try to get all from the table that's why you got the error.
solution: you can make group by with both (time_session and day) then your problem will be solve.
sample code:
public function classSchedule()
{
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*')
->groupBy('applications.time_session')
->groupBy('applications.day')
->get();
return view('backEnd.classes.manageClass1',['duplicates'=>$duplicates]);}
Im using codeigniter to pull blog posts from my database and I want to display how much time has elapsed since the post is posted. By default codeigniters timespan() function is displaying for example:
1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
but i want to translate the words Year,Months,Weeks,Days,Hours,Minutes in other language, for example Bosnian,Croatian or Serbian.
How can I translate it?
I found the answer. You need to go to the application/language/english folder and create a file called date_lang.php. In my case I just want to have one language on my website, so i made the file in the default (english) folder. If you have more languages you can change the language in the application/config/config.php and change the $config['language'] = 'english' to whatever language you made in the application/language/YOURLANGUAGE.php file.
I added this piece of code in my date_lang.php file and it works well.
<?php
$lang['date_year'] = 'Godinu';
$lang['date_years'] = 'Godina';
$lang['date_month'] = 'Mjesec';
$lang['date_months'] = 'Mjeseci';
$lang['date_week'] = 'Sedmica';
$lang['date_weeks'] = 'Sedmice';
$lang['date_day'] = 'Dan';
$lang['date_days'] = 'Dana';
$lang['date_hour'] = 'Sat';
$lang['date_hours'] = 'Sat';
$lang['date_minute'] = 'Minute';
$lang['date_minutes'] = 'Minuta';
$lang['date_second'] = 'Sekundu';
$lang['date_seconds'] = 'Sekunde';
?>
ccs.each do |cd|
relative_model = main_model.relative_model.where(start_date: XYZ, end_date: XYZ).first_or_initialize
relative_model.capacity = cd['capacity'].to_f
relative_model.save!
end
As per above code, first_or_initialize is not working for relative model and creating new record each time.
Here is the query run at background for both inr:
SELECT `capacity_commitments`.* FROM `capacity_commitments` WHERE `capacity_commitments`.`participants_subscription_id` = 1 AND `capacity_commitments`.`start_date` = '2016-11-16' AND `capacity_commitments`.`end_date` = '2016-11-21'
SELECT `capacity_commitments`.* FROM `capacity_commitments` WHERE `capacity_commitments`.`participants_subscription_id` = 1 AND `capacity_commitments`.`start_date` = '2016-11-16' AND `capacity_commitments`.`end_date` = '2016-11-21'
NEED INITIAL HELP OR POINT OUT WHAT IS WRONG IN ABOVE CODE?
Try to use find_or_initialize_by method
relative_model = main_model.relative_model.find_or_initialize_by(start_date: XYZ, end_date: XYZ)