“method does not exist laravel” 可以翻译为 “Laravel中方法不存在”。

huangapple go评论47阅读模式
英文:

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:
&gt; 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-&gt;skills as $id){
dd(Skill::find($request-&gt;skills)-&gt;students()-&gt;attach($request-&gt;students));
return &quot;done&quot;;
}
}

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 = [
&#39;password&#39;,
&#39;remember_token&#39;,
&#39;email&#39;,
&#39;natioal_id&#39;
];
public $table = &quot;students&quot;;
protected $primaryKey = &#39;student_id&#39;;
public function tasks() {
return $this-&gt;belongsToMany(Task::class,&#39;student_task&#39;,&#39;task_id&#39;,&#39;student_id&#39;);
}
public function taskk() {
return $this-&gt;belongsToMany(Task::class,&#39;student_task&#39;,&#39;student_id&#39;,&#39;task_id&#39;);
}
public function skill() {
return $this-&gt;belongsToMany(Skill::class, &#39;student_skill&#39;, &#39;skill_id&#39;, &#39;student_id&#39;);
}
public function skilll() {
return $this-&gt;belongsToMany(Skill::class, &#39;student_skill&#39;, &#39;student_id&#39;,&#39;skill_id&#39;);
}

}


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 = [
&#39;skill_type&#39;,
];
protected $primaryKey = &#39;skill_id&#39;;
public function students() {
return $this-&gt;belongsToMany(Students::class,&#39;student_skill&#39;,&#39;skill_id&#39;,&#39;student_id&#39;);
}
public function skilll() {
return $this-&gt;belongsToMany(Skill::class, &#39;student_skill&#39;, &#39;student_id&#39;,&#39;skill_id&#39;);
}
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-&gt;skills, please do this if you're iterating in loop,

public function attach_skill(Request $request) {
foreach($request-&gt;skills as $id){
dd(Skill::find($id)-&gt;students()-&gt;attach($request-&gt;students));
return &quot;done&quot;;
}
}

huangapple
  • 本文由 发表于 2023年5月25日 18:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定