eloquent

Eloquent morphTo()->withTrashed() stopped working

坚强是说给别人听的谎言 提交于 2021-01-06 04:24:07
问题 I have a polymorphic relationship set up in an OrderItem model, where saleable can be a few different models. I've set it up like any other relationship: public function saleable() { return $this->morphTo()->withTrashed(); } This used to work fine, now all of a sudden it doesn't work and it throws the error: Call to undefined method Illuminate\Database\Query\Builder::withTrashed() I don't understand why it would have stopped working, possibly due to a composer update which may have updated

Laravel: Paginating a query on a view MySQL Error 1140 Mixing of GROUP columns

南楼画角 提交于 2021-01-05 08:45:08
问题 I am trying to do a simple pagination of a query that uses a view: DB::table('vw_myview')->paginate(50) And get this error: SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select count(*) as aggregate from vw_myview) Yet when I manually run the query, it works just fine: mysql> select count(*) as aggregate from vw_myview; +-----------+ | aggregate | +-----------+ |

fetch additional data in to array from database

落爺英雄遲暮 提交于 2021-01-05 07:43:05
问题 i have standings array with team info and calculated points. But also i need to get goals count of each team. Need help how to fetch it to current array. This is my LeaguesController: public function standings(League $league, Team $team) { $standings = [ ]; $matches = Match::where('league_id', '=', $league->id)->get(); foreach($matches as $match) { $homeTeamScore = $match->score->home_team_score; $awayTeamScore = $match->score->away_team_score; if ($homeTeamScore === $awayTeamScore) { if

fetch additional data in to array from database

爷,独闯天下 提交于 2021-01-05 07:43:04
问题 i have standings array with team info and calculated points. But also i need to get goals count of each team. Need help how to fetch it to current array. This is my LeaguesController: public function standings(League $league, Team $team) { $standings = [ ]; $matches = Match::where('league_id', '=', $league->id)->get(); foreach($matches as $match) { $homeTeamScore = $match->score->home_team_score; $awayTeamScore = $match->score->away_team_score; if ($homeTeamScore === $awayTeamScore) { if

Laravel withCount() subquery

瘦欲@ 提交于 2021-01-01 07:01:30
问题 How would I run a subquery on withCount()? I have a query I want to run for multiple counts, each with their own subqueries. Here is an example of something that I'm looking for: $date_from = Carbon::parse('1/1/2018'); $date_to = Carbon::parse('1/2/2018'); $models = Model::query() ->withCount('relation1', function (Builder $query) use ($date_from, $date_to) { $query->whereBetween('relation1.date1', [$date_from, $date_to]) ->where('value1', true); }) ->withCount('relation2', function (Builder

How to sort query results by distance in Laravel QueryBuilder / MySQL Spatial package?

点点圈 提交于 2020-12-30 07:45:21
问题 First I want to show you the current database structure. There are three tables: dishes (id, name) locations (id, name, coordinates (POINT)) dish_location (location_id, dish_id) Now I want to implement an API which gets the position (latitude, longitude) of the user and returns a list of dishes sorted by the distance in km. I already have a method which takes two latitudes and two longitudes and gives me the distance. But I am sure you can show me a way, which is a more performant way to do

How to use 'hasManyThrough' with more than 3 tables in Laravel?

让人想犯罪 __ 提交于 2020-12-29 23:37:30
问题 as stated in the docs the current hasManyThrough can be used when u have something like country > users > posts which result in something like Country::whereName('xx')->posts; which is great but what if i have more than that like country > cities > users > posts or even country > cities > towns > users > posts how would you then implement something like that so you can write the same as above Country::whereName('xx')->posts; or Town::whereName('xx')->posts; 回答1: I created a HasManyThrough

How to use 'hasManyThrough' with more than 3 tables in Laravel?

帅比萌擦擦* 提交于 2020-12-29 23:34:58
问题 as stated in the docs the current hasManyThrough can be used when u have something like country > users > posts which result in something like Country::whereName('xx')->posts; which is great but what if i have more than that like country > cities > users > posts or even country > cities > towns > users > posts how would you then implement something like that so you can write the same as above Country::whereName('xx')->posts; or Town::whereName('xx')->posts; 回答1: I created a HasManyThrough

How to use 'hasManyThrough' with more than 3 tables in Laravel?

大城市里の小女人 提交于 2020-12-29 23:33:34
问题 as stated in the docs the current hasManyThrough can be used when u have something like country > users > posts which result in something like Country::whereName('xx')->posts; which is great but what if i have more than that like country > cities > users > posts or even country > cities > towns > users > posts how would you then implement something like that so you can write the same as above Country::whereName('xx')->posts; or Town::whereName('xx')->posts; 回答1: I created a HasManyThrough

Eloquent select rows with empty string or null value

半腔热情 提交于 2020-12-29 09:11:37
问题 I have something like $user->albums()->where('col', NULL) , it works fine then I tried to extend it to empty strings with $user->albums()->where('col', NULL)->or_where('col', '') and it's not working. Also I saw on this post that I could use where_null('col') but it's not working and it's not documented. Any simple method to select where empty or NULL col 回答1: Try using orWhereNull for the second clause: $users = DB::table('users') ->where('col', '=', '') ->orWhereNull('col') ->get(); 回答2: