laravel-query-builder

change default date format laravel sql query

萝らか妹 提交于 2019-12-01 14:38:36
I want to change date out put from following SELECT query to DD/MM/YYY format . pls advice. this is what I tried so far ->select('time_sheets.progress',DATE_FORMAT('time_sheets.date', "%d/%l/%Y") ,'role_users.role_id','roles.role_name') Instead of using ->select you can use selectRaw to allow raw SQL query so you can use: ->selectRaw('time_sheets.progress, DATE_FORMAT(time_sheets.date, "%d/%l/%Y"), role_users.role_id, roles.role_name'); ->select('time_sheets.progress',\DB::raw('DATE_FORMAT(p.created_at,\'%d-%m-%Y\') as data')), role_users.role_id, roles.role_name'); Hope to help someone else !

how to calculate data by category when creating new data with the same category

北慕城南 提交于 2019-12-01 13:44:31
Model public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek(); $nowDate = Carbon::now()->today(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $new_spent_time = SpentTime::find($plan_id); $task_category = $new_spent_time->task_category; $new_spent_time->task_category = (['{task_category}' => $task_category, '{daily_spent_time}' => $new_spent_time->daily_spent_time, '{daily_percentage}' => $new_spent_time->daily_percentage, '{spent_time}' => $new_spent_time->spent

Cannot save calculation data by category when create new data in laravel

半腔热情 提交于 2019-12-01 13:02:36
问题 Model SpentTime I am unable save calculation data by category when create new data in laravel public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString(); $nowDate = Carbon::now()->today()->toDateString(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $task_category = SpentTime::where('task_category', $spent_time->task_category)->get(); $create_spent

how to calculate data by category when creating new data with the same category

这一生的挚爱 提交于 2019-12-01 12:28:10
问题 Model public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek(); $nowDate = Carbon::now()->today(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $new_spent_time = SpentTime::find($plan_id); $task_category = $new_spent_time->task_category; $new_spent_time->task_category = (['{task_category}' => $task_category, '{daily_spent_time}' => $new_spent_time->daily_spent_time,

laravel querybuilder how to use like in wherein function

馋奶兔 提交于 2019-11-29 03:36:30
$book = array('book1','book2'); $book array elements numbers are variable. it might have 2 element or 20 elements I need to make a query like this: select * from book where bookname like %book1% or bookname like %book2% To make this query in laravel 5 there is an option : $name = DB::Table('bookinfo') ->select('*') ->wherein('bookname',$book) ->get(); but it use = operator I need to use like operator Al-Alamin Thanks everyone for helping me but i solved it by doing: $book = array('book2','book3','book5'); $name = DB::Table('bookinfo') ->select('BookName', 'bookId') ->Where(function ($query)

A JOIN With Additional Conditions Using Query Builder or Eloquent

风格不统一 提交于 2019-11-28 16:03:22
I'm trying to add a condition using a JOIN query with Laravel Query Builder. <?php $results = DB::select(' SELECT DISTINCT * FROM rooms LEFT JOIN bookings ON rooms.id = bookings.room_type_id AND ( bookings.arrival between ? and ? OR bookings.departure between ? and ? ) WHERE bookings.room_type_id IS NULL LIMIT 20', array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10') ); I know I can use Raw Expressions but then there will be SQL injection points. I've tried the following with Query Builder but the generated query (and obviously, query results) aren't what I intended: $results = DB:

A.* isn't in GROUP BY with left join on laravel query builder

◇◆丶佛笑我妖孽 提交于 2019-11-28 14:24:16
$search_alls= DB::table('a16s as A') ->select('A.id') // ->select('A.*') ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 ELSE 0 END) as Yshow')) ->leftjoin('a16s_likes as B', function($join) { $join->on('A.id', '=', 'B.p_id'); }) ->groupBy('A.id') ->get(); when I use above select('A.id') is work well. But when I use select('A.*') to select all A cloumn I got the error SQLSTATE[42000]: Syntax error or access violation: 1055 'employee.A.name' isn't in GROUP BY PS:employee is my DB name The column on A table is id name .... 1 john 2 mary 3 susan How can I select all column by the leftjoin? the

How to perform TRIM() and CONCAT() in Laravel Query Builder?

倖福魔咒の 提交于 2019-11-28 11:41:00
问题 I tried to take FirstName, middlename, Lastname used the Query Builder in combination with RAW but failed. is my way wrong? Thanks $student = \DB::table('student') ->select(DB::raw('RTRIM(LTRIM(CONCAT( COALESCE(FirstName + ''), COALESCE(MiddleName + ''), COALESCE(Lastname, '')))) AS Name')) ->get(); 回答1: $student = DB::table('student') ->select( DB::raw("TRIM(CONCAT(FirstName,' ',MiddleName,' ',LastName)) AS Name") )->get(); TRIM Function - Remove leading and trailing spaces from a string See

laravel querybuilder how to use like in wherein function

走远了吗. 提交于 2019-11-27 17:36:43
问题 $book = array('book1','book2'); $book array elements numbers are variable. it might have 2 element or 20 elements I need to make a query like this: select * from book where bookname like %book1% or bookname like %book2% To make this query in laravel 5 there is an option : $name = DB::Table('bookinfo') ->select('*') ->wherein('bookname',$book) ->get(); but it use = operator I need to use like operator 回答1: Thanks everyone for helping me but i solved it by doing: $book = array('book2','book3',

Laravel join query with conditions

◇◆丶佛笑我妖孽 提交于 2019-11-27 09:54:24
I have 4 tables. User table: id col1 col2 CoursesAssigned table: id user_id course_id approved CourseInfo table: id parent_id CourseParents table: id start_date end_date I think the table names and its column names are self explanatory. I have 2 kinds of users - i) assigned ii) unassigned. I show them in 2 different pages. While showing the assigned students I need those students from users table for each of whom there is at least one row in CoursesAssigned table where user_id is his own user id and the approved field is 1 and the course_id in that row has its own parent_id (from CourseInfo )