eloquent

Laravel hasMany detach

£可爱£侵袭症+ 提交于 2019-12-23 07:56:52
问题 I am having an issue with Eloquent regarding removing child model: When this is executed in process2() I still have the deleted model which is not ok. Model namespace App\Models; use Illuminate\Database\Eloquent\Model; class Model1 extends Model { public function seasons() { return $this->hasMany('App\Models\Seasons', 'series_id', 'id'); } } Service class Process { public function process1($model1Instance) { for($model1Instance->seasons() as $season) { if(//whatever//) { $season->delete(); }

Eloquent taking too long with remote MySQL DB

。_饼干妹妹 提交于 2019-12-23 07:45:48
问题 I'm developing a web (Laravel) and iOS app that both consumes from a REST API that I'm also developing with Laravel. The API queries from a MySQL Database hosted at an AWS RDS instance. When I set up both, the API and the app on localhost, and the API configured to connect my local Database (same machine as the apps and API) it works ok as expected, but the problem is when I setup the API to query from the AWS RDS Database instead of locally. Simple queries made with eloquent, like Product:

Eloquent taking too long with remote MySQL DB

爷,独闯天下 提交于 2019-12-23 07:45:06
问题 I'm developing a web (Laravel) and iOS app that both consumes from a REST API that I'm also developing with Laravel. The API queries from a MySQL Database hosted at an AWS RDS instance. When I set up both, the API and the app on localhost, and the API configured to connect my local Database (same machine as the apps and API) it works ok as expected, but the problem is when I setup the API to query from the AWS RDS Database instead of locally. Simple queries made with eloquent, like Product:

How to get the raw SQL for a Laravel delete/update/insert statement?

六月ゝ 毕业季﹏ 提交于 2019-12-23 07:29:29
问题 I know I can use the toSql method on a query builder to get the raw SQL with binding parameter placeholders for a SELECT statement. App\User::where(['id'=>'1'])->toSql(); "select * from `users` where (`id` = ?)" But how can I get this for a DELETE statement? App\User::where(['id'=>'1'])->delete()->toSql(); PHP error: Call to a member function toSql() on integer on line 1 This executes the statement, but I would like to get the raw, uninterpolated SQL generated with the question marks standing

How to get the raw SQL for a Laravel delete/update/insert statement?

╄→гoц情女王★ 提交于 2019-12-23 07:28:28
问题 I know I can use the toSql method on a query builder to get the raw SQL with binding parameter placeholders for a SELECT statement. App\User::where(['id'=>'1'])->toSql(); "select * from `users` where (`id` = ?)" But how can I get this for a DELETE statement? App\User::where(['id'=>'1'])->delete()->toSql(); PHP error: Call to a member function toSql() on integer on line 1 This executes the statement, but I would like to get the raw, uninterpolated SQL generated with the question marks standing

Laravel Eloquent how to use between operator

自闭症网瘾萝莉.ら 提交于 2019-12-23 06:47:10
问题 I am trying to find an elegant way in Eloquent and Laravel to say select * from UserTable where Age between X and Y Is there a between operator in Eloquent (I can't find it). The closest i have gotten so far is chainging my query like this $query->where(age, '>=', $ageFrom) ->where(age, '<=', $ageTo); I also came across whereRaw that seems to work $query->whereRaw('age BETWEEN ' . $ageFrom . ' AND ' . $ageTo . ''); Is there an actual Eloquent way (not raw) that deals with ranges? 回答1: $query-

In Laravel, is there a way to find out whether `firstOrCreate` created or if it found the row?

别来无恙 提交于 2019-12-23 06:47:09
问题 In Laravel, is there a way to differentiate between firstOrCreate creating the row and if finding out the row exists from before? Or will I have to manually do a get() first, and then create the row? 回答1: If you created the model in the current lifecycle, then the model's wasRecentlyCreated attribute will be set to true . Otherwise, that attribute will be set to false . In other words, lets say you have a user with the email, bob@example . $user = User::firstOrCreate(['email' => 'bob@example

Laravel Return Siblings Results in Non-Object situation

社会主义新天地 提交于 2019-12-23 05:34:12
问题 I have a table of users that are imported to the DB. I keep their original id as reference_id: id | reference_id | parent_id 1 35 null 2 36 35 3 37 35 4 38 null 5 37 38 I am trying to get back the entire family. In laravel I am using this in the model: public function getFamily(){ $parent = User::where('user_id', auth()->id()) ->where('parent_id', $this->parent_id)->get(); $children = User::where('user_id', auth()->id()) ->where('reference_id', $this->parent_id)->get(); return $parent->merge(

Laravel using UNION in query builder

拈花ヽ惹草 提交于 2019-12-23 04:25:05
问题 I have an SQL query that works fine and I'm trying to convert into fluent:: SELECT DISTINCT tags.tag FROM tags, items WHERE tags.taggable_type = 'Item' AND items.item_list_id = '1' UNION SELECT DISTINCT tags.tag FROM tags, itemlists WHERE tags.taggable_type = 'ItemList' AND itemlists.id = '1' This is what I have so far in fluent, it all seems right as far as I can tell from the docs and the individual queries both work on their own, it's just when I UNION them it throws an error: $itemTags =

How to encode an array of data and save to a pivot table laravel

做~自己de王妃 提交于 2019-12-23 04:22:11
问题 All I want here is to be able to save all the ID's of services into the pivot table associated with the given keywords/tags but at the moment all it does is that it takes the last ID of the created object and saves into the pivot table with different keywords. let's say for example I enter [id1 => service1, id2 => service2] and [id1 = > keyword1, id2 => keyword2, id3 => keyword3] instead of it saving only id2 of service2 and all the keywords I want it to save all the Ids of all of the