Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 两个store()方法,一个不使用';t work(调用未定义的方法save())_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Laravel 两个store()方法,一个不使用';t work(调用未定义的方法save())

Laravel 两个store()方法,一个不使用';t work(调用未定义的方法save()),laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,两种类似的存储方法中的一种不起作用。你能帮我澄清一下吗 关系 一个团队有许多用户一个用户属于一个团队 一个用户有许多字符,其中一个字符属于一个用户 工作代码(字符控制器) 不工作(TeamController) 问题 为什么没有相同的方法?是关系的东西吗 create方法更好吗?我应该试着使用它吗 我以为我知道我在做什么,现在却发现我不。。。 谢谢您的帮助。team()是属于关系的,您的用户表中可能有一个想要与团队关联的团队id列 public function store() {

两种类似的存储方法中的一种不起作用。你能帮我澄清一下吗

关系
  • 一个团队有许多用户一个用户属于一个团队
  • 一个用户有许多字符,其中一个字符属于一个用户
工作代码(字符控制器) 不工作(TeamController) 问题
  • 为什么没有相同的方法?是关系的东西吗
  • create方法更好吗?我应该试着使用它吗
  • 我以为我知道我在做什么,现在却发现我不。。。 谢谢您的帮助。

    team()
    是属于关系的
    ,您的用户表中可能有一个想要与团队关联的团队id列

    public function store()
    {
        $this->validate(request(), [
            'name' => 'required|min:3|max:25|alpha_num|unique:teams',
        ]);
    
        // create and save team
        $team = new Team([
            'name' => request('name'),
            'fame' => 0,
        ]);
        $team->save();
    
        // associate current authenticated user with team (set foreign key) and save user
        auth()->user()->team()->associate($team)->save();
    
        return redirect()->route('team.index');
    }
    

    伟大的让我们祈祷我也能把这个保存到我的脑子里!看来你知道发生了什么。CharacterController在所有链接中看起来足够好吗?它基本上是1 chain$user->characters()->doStuff()。它是为了允许链接而创建的,请尝试保持代码可读性。
    public function store()
    {
        $this->validate(request(), [
            'name' => 'required|min:3|max:25|alpha_num|unique:teams',
        ]);
        auth()->user()->team()->save(new Team([
            'name' => request('name'),
            'fame' => 0,
        ]));
        return redirect()->route('team.index');
    }
    
    public function store()
    {
        $this->validate(request(), [
            'name' => 'required|min:3|max:25|alpha_num|unique:teams',
        ]);
    
        // create and save team
        $team = new Team([
            'name' => request('name'),
            'fame' => 0,
        ]);
        $team->save();
    
        // associate current authenticated user with team (set foreign key) and save user
        auth()->user()->team()->associate($team)->save();
    
        return redirect()->route('team.index');
    }