laravel-query-builder

Laravel: change a raw query in a “query-builder” or “eloquent” one

試著忘記壹切 提交于 2019-12-13 11:47:43
问题 I have this Laravel Query Builder snippet that is working fine: $records = DB::table('users') ->select( DB::raw('users.*, activations.id AS activation, (SELECT roles.name FROM roles INNER JOIN role_users ON roles.id = role_users.role_id WHERE users.id = role_users.user_id LIMIT 1) AS role') ) ->leftJoin('activations', 'users.id', '=', 'activations.user_id') ->where('users.id', '<>', 1) ->orderBy('last_name') ->orderBy('first_name') ->paginate(10); Is there a way to avoid use of raw queries

Writing a function in laravel

空扰寡人 提交于 2019-12-13 09:24:04
问题 I have the following function which fetch some data related to jobs from database. The user can search for jobs with job title / keyword, city and/or category. The user can either choose one option, e.g. searching jobs only by title, or by category. or he can use all options for deep search. Below is my function: public function jobsearch(Request $request) { $keyword = htmlspecialchars($request->input('keyword')); $city_id = $request->input('city_id'); $category_id = $request->input('category

can't save calcution data when create new data

房东的猫 提交于 2019-12-13 04:58:34
问题 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::first(); $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

Using REGEXP with Laravel query builder

…衆ロ難τιáo~ 提交于 2019-12-13 03:54:41
问题 I have the following query: $registros = Oportunidade::whereRaw("descricao REGEXP '[[:<:]]". $search ."[[:>:]]'")->get(); SQLSTATE[HY000]: General error: 1 no such function: REGEXP (SQL: select * from "oportunidades" where descricao REGEXP '[[:<:]]java[[:>:]]') Also tried: $registros = Oportunidade::where("descricao", "REGEXP", "'[[:<:]]'" .$search . "'[[:>:]]'")->get(); But for some reason I'm receiving this: SQLSTATE[HY000]: General error: 1 no such function: REGEXP (SQL: select * from

Why can't I delete this from my tables that have relationships?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:40:41
问题 Why can't I delete these items from my database, they only have one relationship each, and I'm detaching and deleting each respectively. I have 3 tables Vendor, Brand and Product. The error mentions the Products table but there are no relationships in the Vendor table to the Products table, the relationship is with the Brand table and it's a one to one relationship. Table structure Schema::create('vendors', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table

Foreign key query laravel - the eloquent way

十年热恋 提交于 2019-12-12 03:55:30
问题 I am trying to find a way to write eloquent way to query my model. I have a Form model every form belongs to a User (Form:User = 1:1). Each User has a State and a City associated with them. Admin reviews a Form and each admin can be assigned to multiple State and City . I want to find the Form that belongs to an Admin . This is the forms function in Admin.php (Model) public function forms() { //cities $cities = $this->cities->pluck('name'); //states $states = $this->states->pluck('name'); /

whereNotBetween giving me error

时光怂恿深爱的人放手 提交于 2019-12-11 17:23:34
问题 I am trying to write whereNotBetween query but I dont where is my mistake. its not giving me result. I know something is mistake where i am writing near the line whereNotBetween . There is not any other example. $values = DB::table('exchanges') ->leftJoin('trades', 'exchanges.id', '=', 'trades.exchange_id') ->select('trades.*') ->where('trades.user_id', $user) ->whereRaw('TIME(trades.tradedate) NOT BETWEEN exchanges.start_time AND exchanges.close_time') ->get(); Unknown column 'trade_date' in

SQLSTATE[42S22]: Column not found: in laravel affte using having clause?

一个人想着一个人 提交于 2019-12-11 17:12:31
问题 I am working on laravel project but firstly it was created in core php. I am stuck in one query that is working fine in sql query but when i convert in to laravel its showing error... Sql query is below:- SELECT *,MAX(UNIX_TIMESTAMP( if(last_recur_date IS NULL, subscription_date,last_recur_date) )) as max, SUM( IF (active = 'y', 1, 0) ) AS acount, m.id AS member_id FROM members m INNER JOIN user_access_old ua ON m.id = ua.member_id GROUP BY m.id having acount=0 ORDER BY max DESC, m.id DESC,

How to calculate age using query builder in laravel?

折月煮酒 提交于 2019-12-11 15:25:45
问题 I have saved the date of birth in MySQL database. table name : **users** field name : **dob** now I want to show age on view. My query is : $user_info = DB::table('users')->select('(year(curdate())-year(dob)) as age')->get(); I'm using Laravel 5.5. but this is creating error. How to calculate age using query builder in laravel? 回答1: I recommend you to make a custom attribute in your app/User.php model. For example: In your User.php public function getAgeAttribute() { return $this->dob-

Laravel query replace new data with old one

佐手、 提交于 2019-12-11 11:23:29
问题 Basically this question is following of [link] As that question got too messy I want to separate the question here. Explanation I have appended rows in my blade where several select box will append and i can choose options of each of them. this data will save in database ( Until here everything is good ) The issue comes when I change one of my selected options and instead of being update it will add all selects as new one. Screenshot 1 Code controller public function spacssendto(Request