eloquent

Laravel: How to get last N entries from DB

ぃ、小莉子 提交于 2020-08-20 23:10:43
问题 I have table of dogs in my DB and I want to retrieve N latest added dogs . Only way that I found is something like this: Dogs:all()->where(time, <=, another_time); Is there another way how to do it? For example something like this Dogs:latest(5); Thank you very much for any help :) 回答1: You may try something like this: $dogs = Dogs::orderBy('id', 'desc')->take(5)->get(); Use orderBy with Descending order and take the first n numbers of records. 回答2: My solution for cleanliness is: Dogs:

Laravel: How to get last N entries from DB

孤人 提交于 2020-08-20 23:10:11
问题 I have table of dogs in my DB and I want to retrieve N latest added dogs . Only way that I found is something like this: Dogs:all()->where(time, <=, another_time); Is there another way how to do it? For example something like this Dogs:latest(5); Thank you very much for any help :) 回答1: You may try something like this: $dogs = Dogs::orderBy('id', 'desc')->take(5)->get(); Use orderBy with Descending order and take the first n numbers of records. 回答2: My solution for cleanliness is: Dogs:

Laravel Eloquent: How to count with nested relationship

痞子三分冷 提交于 2020-08-10 18:50:28
问题 Let's say we have four tables: tags, posts, replies, and post_tag tags table: id tag 1 tag A 2 tag B 3 tag C posts table: id post_content 1 some text A 2 some text B 3 some text C post_tag table: (for many-many relationship between posts and tags) post_id tag_id 1 1 1 2 2 3 replies table: id post_id 1 1 2 1 3 2 Now I wish to generate a table which gives me the total replies count for each of the tag. A sample output based on the above tables would be: id tag_id replies_count 1 1 2 2 2 2 3 3 1

Update non-fillable field in Laravel Model

谁都会走 提交于 2020-08-10 05:19:18
问题 I have my User table, the default table laravel comes with when you do the whole php artisan make:auth stuff, and I've added a boolean system_admin column, and a couple other similar columns Now when someone registers to the site, I don't want a malicious person to be able to just give themselves that right, so I haven't put that field in the fillable array in the User.php model file. First question then: is that the right decision for the right reason? But now I'm working on a system admin

SQLSTATE [23000]: Integrity limitation violation: 1062 'mail@mail.com' duplicate entry for 'users_email_unique' key

六月ゝ 毕业季﹏ 提交于 2020-08-09 17:54:26
问题 I have a registration form. When the email is registered in the database it gets an error message SQLSTATE [23000]: Violation of integrity restrictions: 1062 Duplicate entry 'mail@mail.com' for 'users_email_unique' key I want to avoid that mistake and instead get a warning like "registered email" or something similar. Any help is appreciated. This is my code. controller/auth/registercontroller.php <?php namespace VMS\Http\Controllers\Auth; use VMS\User; use VMS\Http\Controllers\Controller;

Laravel Many to Many Sync with additional column

喜你入骨 提交于 2020-08-07 15:58:35
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

南楼画角 提交于 2020-08-07 15:58:10
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

穿精又带淫゛_ 提交于 2020-08-07 15:51:26
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

核能气质少年 提交于 2020-08-07 15:51:05
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

How to use alias column in whereIn with Laravel?

一笑奈何 提交于 2020-08-07 07:48:52
问题 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