eloquent

Laravel 5.5 Custom Soft Deletes on Model

筅森魡賤 提交于 2020-01-04 03:51:32
问题 My posts are defined as soft deleted or not via the value in the status column, where: 0 = unavailable, 1 = available, and 77 = soft deleted. Currently, I add a global scope in the model to make sure I don't return soft-deleted posts: protected static function boot() { parent::boot(); static::addGlobalScope('status', function (Builder $builder) { $builder->where('status', '!=', '77'); }); } How would I modify softDelete (laravels built-in functionality) of a model from its default of

Laravel / Eloquent WHERE NOT SUBQUERY

混江龙づ霸主 提交于 2020-01-04 01:31:12
问题 I`m having trouble to setup a negative condition like such: WHERE NOT( "last_day" "<=" $first_day OR "first_day" "<=" $last_day) My query builder looks like this atm: $query = $query->where(function ($query) use ($first_day, $last_day) { $query->where('last_day', '<=', $first_day); $query->orWhere('first_day', '<=', $last_day); }); I would like it to be as such : $query = $query->whereNot(function ($query) use ($first_day, $last_day) { $query->where('last_day', '<=', $first_day); $query-

Laravel / Eloquent hasMany relationship sum()

Deadly 提交于 2020-01-04 01:20:34
问题 I can't figure out how to eager load a sum of a relationships columns. Database (simplified) is as follows; TABLES PRODUCT PRODUCT_VARIATIONS *ID* *ID* *NAME* *NAME* *AVAILABLE_STOCK* I have my relationships set up as follows; public function variations() { return $this->hasMany('Product_variation'); } When loading all Products I want to be able to see the SUM of all stock for that product attached to the product object itself. A product may have many variations. I can return the entire

Using Auth to get the role of user in a pivot table

只愿长相守 提交于 2020-01-04 00:28:07
问题 I have the following database structure a users & roles table joined using a pivot table: The users table contains all the info such as email and password, while the roles table defines the different roles : roles table 1 -> admin 2 -> encoder 3 -> salesman 4 -> tech Now the roles are defined many-to-many because, there are some encoders that are assigned also as an admin. Others may have an salesman+admin role, or even encoder+salesman. Basically it multiple roles can be assigned to someone.

Order by pivot table created_at in Laravel

扶醉桌前 提交于 2020-01-04 00:06:24
问题 In my database i have the following tables: courses (id, name, created_at, updated_at) students (id, name, created_at, updated_at) course_student (id, course_id, student_id, created_at, updated_at) I'm trying to retrieve all the courses in which a student has enrolled order decrementally by the created_at value in the pivot table course_student This is how i defined my models: Student model: public function students () { return $this->belongsToMany(Student::class, 'course_student', 'course_id

Can Eloquent models retrieve metadata on their rows?

柔情痞子 提交于 2020-01-03 20:04:17
问题 Is it possible for an Eloquent-based model in Laravel to introspect/reflect on the underlying table and retrieve information (type, size, unsigned, default values, etc) about the columns? Or is this intentionally left out? I feel like it would be useful to be able to determine the column type (to do things like intelligently assign some validations, or help infer a form input type and/or populate a default). 回答1: I don't think you can do this "out of the box" in Laravel, though it would be

Can Eloquent models retrieve metadata on their rows?

﹥>﹥吖頭↗ 提交于 2020-01-03 20:02:01
问题 Is it possible for an Eloquent-based model in Laravel to introspect/reflect on the underlying table and retrieve information (type, size, unsigned, default values, etc) about the columns? Or is this intentionally left out? I feel like it would be useful to be able to determine the column type (to do things like intelligently assign some validations, or help infer a form input type and/or populate a default). 回答1: I don't think you can do this "out of the box" in Laravel, though it would be

Eloquent conditional where filter

北城以北 提交于 2020-01-03 09:00:08
问题 If I have a variable that is optional, and I only want to have it search on it if it is defined, I know that I can do this, but is there a way to define the function elsewhere, so I don't need to rewrite the same anonymous function over and over again? Or are the any other good alternatives to going about solving this problem? .......->where(function($query) use ($gender){ if ($gender) { $query->where('gender', '=', $gender); } })->get(); 回答1: You could use a dynamic scope class User extends

#attributes vs #original in Laravel when dd()

喜欢而已 提交于 2020-01-03 08:56:29
问题 In my Laravel code, when I am processing the records somewhere in the code I am adding new attributes to the array (ex. $obj->newAttr=someContent), later when I dd() the object it shows me two arrays: #attributes and #original, but the newly created attribute show up only in #attributes array. Here is an example when I dd() the object, I get the #attributes array: #attributes: array:21 [▼ "field_name" => "229" "company_name" => "Maya Dairy" "seeding_rate" => 115 "id" => 11 "property_id" => 71

Laravel belongsToMany where doesn't have one of

 ̄綄美尐妖づ 提交于 2020-01-03 08:23:12
问题 I have two tables: categories and videos , I then have a pivot table for these as it's a belongsToMany relationship. What I'm trying to do is get all of the videos where there isn't a single instance of the video being in one of many categories. e.g. Video 1 is in category 1, 2 and 3. Video 2 is in category 1 and 3. Video 3 is in category 1. I want to get the video which is NOT in category 2 or 3, meaning this will return Video 3. What I've tried so far, which doesn't give the intended result