eloquent

How to use alias column in whereIn with Laravel?

◇◆丶佛笑我妖孽 提交于 2020-08-07 07:47:51
问题 here is my code. I am trying to get new list of A items in order to loop on. $allowed_a = \App\NewA::select('name')->get()->pluck('name'); $a = App\A::selectRaw("replace(unaccent(trim(name)), ' ', '') AS newname, name") ->whereIn('newname', $allowed_a)->get(); But I am getting Undefined column 'newname' . How can I fix it please? thanks 回答1: You should be able to achieve something equivalent using: $allowed_a = \App\NewA; \App\A::selectRaw('replace(unaccent(trim(name)) as newname') ->whereRaw

Using limit parameter in paginate function

你说的曾经没有我的故事 提交于 2020-08-04 08:55:28
问题 Is it possible to use 'limit' parameter in paginate() function? I'm trying this: $users->where(...)->limit(50)->paginate($page) ...and now, if I have 100 users in the database then the response from paginate function will be all 100 users instead of 50 (or number of users defined with limit parameter). So, my question is: is it possible to implement limit parameter when I use paginate function? 回答1: No, it's not possible to limit the query when using pagination. Query pagination uses skip()

Laravel how to get query with bindings?

家住魔仙堡 提交于 2020-07-17 06:08:22
问题 I have some query that I need to pass to another query using query builder $query = DB::table('table')->whereIn('some_field', [1,2,30])->toSql(); Model::join(DB::raw("({$query}) as table"), function($join) { $join->on('model.id', '=', 'table.id'); }) which should results with Select * from model join (select * from table where some_field in (1,2,30)) as table on model.id = table.id but the bindings are not passed, which force me to do $query = DB::table('table')->whereRaw('some_field in ('.

Laravel eloquent sort by role name on relationship model

痞子三分冷 提交于 2020-07-16 09:57:41
问题 I'm stuck with a problem where I have to sort / order a collection of models by their relationship's data. I've got it setup like this: Models: User , Team , TeamUser , Role The TeamUser model is a pivot model / table (containing user_id and team_id . If it's worth mentioning I am also using spatie/laravel-permissions for the roles. How would I go forth when I want to sort the users in a team by their role.name ? I'm talking about the users() relation in the Team model (see further down for

Deep Compare JavaScript function

断了今生、忘了曾经 提交于 2020-07-09 17:16:58
问题 I'm working my way through the 3rd edition of Eloquent JavaScript and although I've seen one or two various answers on SO that seem almost identical in execution logic to mine that work mine just doesnt seem to no matter how I tweek it. THE GOAL: create a deep comparison function that can compare two objects and determine based on their properties if theyre different instances of the same type of object (same keys and values) regardless of reference... Can anyone spot the bug in my code?

insert `id` autoincrement primary key field explicitly in laravel 5.2

折月煮酒 提交于 2020-07-09 06:27:06
问题 I am migrating an old data into laravel. What i want is to keep the previous ids same in the new table where i have a default $table->increments(id) field. Is there a way in laravel that i can explicitly insert this id field? As i am unable to do it using eloquent model create method : User::create([ 'id' => 123456, 'first_name' => 'john', 'last_name' => 'doe' ]) Is it even possible in there? 回答1: The problem must be in the User model. Open the User.php file and modify the $fillable variable.

Call to a member function addEagerConstraints() on string

房东的猫 提交于 2020-06-29 04:05:14
问题 I want to calculate the average of the column of nested morph relation. Model Function public function trader_ratings() { return $this->morphMany(TraderRatings::class, 'rateable') ->select('rateable_id', 'rateable_type', 'rating') ->avg('rating'); } Controller with lazy loading $user = auth('api')->user(); $user_id = $user->id; $customer_classes = CustomerClassBooking::with([ 'trader_class', 'trader_class.trader_ratings', 'vendor', ]) ->where('customer_id', $user_id) ->where('status',

Call to a member function addEagerConstraints() on string

房东的猫 提交于 2020-06-29 04:04:25
问题 I want to calculate the average of the column of nested morph relation. Model Function public function trader_ratings() { return $this->morphMany(TraderRatings::class, 'rateable') ->select('rateable_id', 'rateable_type', 'rating') ->avg('rating'); } Controller with lazy loading $user = auth('api')->user(); $user_id = $user->id; $customer_classes = CustomerClassBooking::with([ 'trader_class', 'trader_class.trader_ratings', 'vendor', ]) ->where('customer_id', $user_id) ->where('status',

Laravel hasMany association with multiple columns

断了今生、忘了曾经 提交于 2020-06-27 11:25:48
问题 I have two models model_1 model_2 model_1 has many model_2 Now I want to association model_1 hasMany model_2 match with multiple column. Let me give an example in raw query select ...... from model_1 left join model_2 ON (model_1.f1 = model_2.f1 AND model_1.f2 = model_2.f2) How can I do this in hasMany association 回答1: I got it to work by adding an additional parameter to join on in the model: public function my_joined_columns($mySecondJoinColumnValue) { return $this->hasMany(

Retrieving all morphedByMany relations in Laravel Eloquent

我的梦境 提交于 2020-06-24 11:08:54
问题 In the Laravel documentation, there is the following example for retrieving morphedByMany relations, which are many-to-many polymorphic relations. Laravel Many to Many polymorphic relations documentation namespace App; use Illuminate\Database\Eloquent\Model; class Tag extends Model { /** * Get all of the posts that are assigned this tag. */ public function posts() { return $this->morphedByMany('App\Post', 'taggable'); } /** * Get all of the videos that are assigned this tag. */ public