gpt4 book ai didi

Laravel 方法同步不存在

转载 作者:行者123 更新时间:2023-12-05 08:54:48 24 4
gpt4 key购买 nike

后模型

<?php

namespace App\Model\User;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
public function tags(){
return $this->belongsToMany('App\Model\User\Tag','post__tags', 'post_id', 'tag_id');
}

public function categories() {
return $this->belongsToMany('App\Model\User\Category','category__posts','post_id', 'category_id');
}
}

类别模型

<?php

namespace App\Model\User;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
public function posts(){
return $this->belongsToMany('App\Model\User\Category','category__posts', 'post_id', 'category_id');
}

}

标签模型

<?php

namespace App\Model\User;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
public function posts() {
return $this->belongsToMany('App\Model\User\Post','post__tags','post_id','tag_id');
}
}

后 Controller .php

public funcion store()
{
// do validation here
try{
$post = new Post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = 1;
$post->save(); // This works correct
$post->tags->sync($request->tags); // Not working
$post->categories->sync($request->categories); // Not working
}
catch(\Exception $e){
return redirect(route('post.index'))->with('message', 'Error Adding Post into system!'.$e->getMessage()); // getting Message "Method sync does not exist. "
}
}

最佳答案

Sync 是构建器实例上的一个方法,您在集合上使用它。

 // change your code like this
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);

关于Laravel 方法同步不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com