eloquent

Retrieving all morphedByMany relations in Laravel Eloquent

依然范特西╮ 提交于 2020-06-24 11:08:24
问题 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

Laravel Eloquent Eager Loading : Join same table twice

人走茶凉 提交于 2020-06-24 08:36:05
问题 I have a users table and an appointments table. In appointment table I have two user ID's (customer_id, staff_id). I want to retrieve all the appointments with customer name and the staff name. users table id name appointments table id staff_id(user_id) customer_id(user_id) datetime As you can see, I have to join the users table twice with the appointments table. Usually I do this with inner joins . Can we do the same thing with Laravel eloquent eager loading using with()? Can we do something

Can't json_encode() an array or Laravel collection: “Type is not supported”

那年仲夏 提交于 2020-06-23 08:18:33
问题 I don't know what I'm doing wrong since it works with all the other models in the app. I refreshed and reseeded the database multiple times. The models extend the same abstract methods. This is the code in the controller: $substrates = $this->substrates->all()->toArray(); $temp = json_encode($substrates); dd($temp, json_last_error(), json_last_error_msg(), $substrates); This is the dd() output: false 8 "Type is not supported" array:119 [▼ 0 => array:21 [▼ "id" => 1 "name" => "Wood Free"

Laravel Eloquent - where relationship field does not equal

做~自己de王妃 提交于 2020-06-17 23:07:52
问题 I thought this would be fairly simple but it's not playing ball currently. I have 2 tables for this question, 'applications' & 'application_call_logs'. This query needs to return all from the applications table where the latest call log doesn't have a status of X. Here's the current query: $query = Application::query(); $query->where(function($query) { $query->whereDoesntHave('call_logs'); $query->orWhereHas('latest_call_log', function($q) { $q->where('status', '!=', 'not interested'); }); })

Laravel Eloquent - where relationship field does not equal

蹲街弑〆低调 提交于 2020-06-17 23:06:44
问题 I thought this would be fairly simple but it's not playing ball currently. I have 2 tables for this question, 'applications' & 'application_call_logs'. This query needs to return all from the applications table where the latest call log doesn't have a status of X. Here's the current query: $query = Application::query(); $query->where(function($query) { $query->whereDoesntHave('call_logs'); $query->orWhereHas('latest_call_log', function($q) { $q->where('status', '!=', 'not interested'); }); })

Laravel Eloquent - where relationship field does not equal

风流意气都作罢 提交于 2020-06-17 23:05:31
问题 I thought this would be fairly simple but it's not playing ball currently. I have 2 tables for this question, 'applications' & 'application_call_logs'. This query needs to return all from the applications table where the latest call log doesn't have a status of X. Here's the current query: $query = Application::query(); $query->where(function($query) { $query->whereDoesntHave('call_logs'); $query->orWhereHas('latest_call_log', function($q) { $q->where('status', '!=', 'not interested'); }); })

Laravel Where Count > N

十年热恋 提交于 2020-06-17 09:54:36
问题 I have 2 models in my app: 1. Customer.php 2. Car.php Now I would like to run a query that returns all customers that have less than 2 cars. Where 2 is a number that can be changed by the user. I have tried this but it didn't work, it just returns all customer records: $customers = Customer::whereHas("cars", function($query) { $query->selectRaw("count(*) < ?", [2]); }) ->get(); Edit: The two models are linked in a pivot table, meaning A customer can have more than 1 car and a Car can belong

Laravel Eloquent: My Pivot table has relation

淺唱寂寞╮ 提交于 2020-06-17 09:44:58
问题 Here some information about the table User table -id -name UserProduct table -id -user_id -product_id Product table -id -name Contribution -id -user_product_id -contribution User Model public function products() { return $this->belongsToMany('App\Product'); } Product Model public function users() { return $this->belongsToMany('App\User'); } UserProduct Pivot Model use Illuminate\Database\Eloquent\Relations\Pivot; class UserProduct extends Pivot { public function contribution() { return $this-

How to use Postgres jsonb '?' operator in Laravel with index support?

早过忘川 提交于 2020-06-17 09:17:04
问题 I'm trying to use the jsonb exists operator '?' in Laravel's query builder, and have it use an index, but I've run into some issues. Sample query DB::table('table_name')->whereRaw("jsonb_column ? 'key'")->get(); Sample Index CREATE INDEX ON table_name USING GIN(jsonb_column jsonb_ops) The main issue seems to be that '?' is reserved for parameter replacement, so this query returns a syntax error. I've found a couple ways around this, but each are incomplete solutions. use '??' (a way to escape

filtering a paginated eloquent collection

ぃ、小莉子 提交于 2020-06-14 09:39:08
问题 I am trying to filter a paginated eloquent collection, but whenever I use any of the collection methods, I lose the pagination. $models = User::orderBy('first_name','asc')->paginate(20); $models = $models->each(function($model) use ($filters) { if(!is_null($filters['type'])) { if($model->type == $filters['type']) return $model; } if(!is_null($filters['state_id'])) { if($model->profile->state_id == $filters['state_id']) return $model; } if(!is_null($filters['city_id'])) { if($model->profile-