英文:
method does not exist laravel
问题
I can help you translate the code-related content:
嘿,各位,我在我的代码中遇到了这个问题,我不知道为什么会显示这个错误:
> Illuminate\\Database\\Eloquent\\Collection::students 方法不存在。
我尝试创建多对多关系,这是我的控制器函数:
```php
public function attach_skill(Request $request) {
foreach($request->skills as $id){
dd(Skill::find($request->skills)->students()->attach($request->students));
return "done";
}
}
这是我的学生模型代码:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Task;
use App\Models\Skill;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Students extends Authenticatable
{
use HasFactory, HasApiTokens;
protected $fillable = [
'name',
'student_id',
'phone',
'password',
'email',
'major',
'GPA',
'Department',
'address',
];
protected $hidden = [
'password',
'remember_token',
'email',
'natioal_id'
];
public $table = "students";
protected $primaryKey = 'student_id';
public function tasks() {
return $this->belongsToMany(Task::class,'student_task','task_id','student_id');
}
public function taskk() {
return $this->belongsToMany(Task::class,'student_task','student_id','task_id');
}
public function skill() {
return $this->belongsToMany(Skill::class, 'student_skill', 'skill_id', 'student_id');
}
public function skilll() {
return $this->belongsToMany(Skill::class, 'student_skill', 'student_id','skill_id');
}
}
这是我的技能模型代码:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Students;
class Skill extends Model
{
use HasFactory;
protected $fillable = [
'skill_type',
];
protected $primaryKey = 'skill_id';
public function students() {
return $this->belongsToMany(Students::class,'student_skill','skill_id','student_id');
}
public function skilll() {
return $this->belongsToMany(Skill::class, 'student_skill', 'student_id','skill_id');
}
public $timestamps = false;
}
我希望学生和技能的请求以数组形式返回,因为我有多个技能和学生。
<details>
<summary>英文:</summary>
hey guys i have this problem in my code , i dont know why is showing this error:
> Method Illuminate\\Database\\Eloquent\\Collection::students does not exist.
i tried to make many to many relation and this is my controller function:
public function attach_skill(Request $request) {
foreach($request->skills as $id){
dd(Skill::find($request->skills)->students()->attach($request->students));
return "done";
}
}
and this is my student model code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Task;
use App\Models\Skill;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Students extends Authenticatable
{
use HasFactory, HasApiTokens;
protected $fillable = [
'name',
'student_id',
'phone',
'password',
'email',
'major',
'GPA',
'Department',
'address',
];
protected $hidden = [
'password',
'remember_token',
'email',
'natioal_id'
];
public $table = "students";
protected $primaryKey = 'student_id';
public function tasks() {
return $this->belongsToMany(Task::class,'student_task','task_id','student_id');
}
public function taskk() {
return $this->belongsToMany(Task::class,'student_task','student_id','task_id');
}
public function skill() {
return $this->belongsToMany(Skill::class, 'student_skill', 'skill_id', 'student_id');
}
public function skilll() {
return $this->belongsToMany(Skill::class, 'student_skill', 'student_id','skill_id');
}
}
and this is my skill model code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Students;
class Skill extends Model
{
use HasFactory;
protected $fillable = [
'skill_type',
];
protected $primaryKey = 'skill_id';
public function students() {
return $this->belongsToMany(Students::class,'student_skill','skill_id','student_id');
}
public function skilll() {
return $this->belongsToMany(Skill::class, 'student_skill', 'student_id','skill_id');
}
public $timestamps = false;
}
i want students and skills request in array becuse i have my skills and many students.
</details>
# 答案1
**得分**: 1
因为您正在将一个数组传递给 `find()` 方法,这是错误的,请传递 `$id` 而不是 `$request->skills`,如果您正在循环中进行迭代,请执行以下操作:
```php
public function attach_skill(Request $request) {
foreach($request->skills as $id){
dd(Skill::find($id)->students()->attach($request->students));
return "done";
}
}
英文:
Because you're passing an array to find()
method that is wrong, pass $id
instead of $request->skills
, please do this if you're iterating in loop,
public function attach_skill(Request $request) {
foreach($request->skills as $id){
dd(Skill::find($id)->students()->attach($request->students));
return "done";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论