Insert data into pivot table in laravel

I have 3 tables: posts, tags, post_tag.

Each Posthas a lot of tags, so I use the method hasManyfor them. But when I select, for example, 3 tags from the drop-down list, I cannot add them to post_tagand as a result I cannot select and show tags for each message.

My Model Post:

 class Post extends Eloquent{
 public function tag()
         {
           return  $this->hasMany('Tag');
         }
    }

My Tagmodel:

class Tag extends Eloquent{
 public function post()
         {
           return  $this->belongsToMany('Post');
         }

}

And mine postController:

class postController extends BaseController{

public function addPost(){

    $post=new Post;

    $post_title=Input::get('post_title');
    $post_content=Input::get('post_content');
    $tag_id=Input::get('tag');

    $post->tag()->sync($tag_id);
    $post->save();

I expect to save this post_idsave to a table post_tagwith its tag identifiers, but this does not work. Thank you for your time.

+6
source share
3 answers

, . , - .

, belongsTomany ( ), belongsTomany ( hasMany - , ). , Laravel .

( ) , ( ->tag()->sync(), ). ( laravel , post_id), . , , .

, "", , , "-", , . , tag post shoudl tags posts .

, :

class Post extends Eloquent
{
    public function tags()
    {
        return $this->belongsToMany('Tag');
    }
}

class Tag extends Eloquent
{
    public function posts()
    {
        return $this->belongsToMany('Post');
    }
}

class PostController extends BaseController
{
    public function addPost()
    {
        // assume it won't work
        $success = false;

        DB::beginTransaction();

        try {
            $post = new Post;

            // maybe some validation here...

            $post->title = Input::get('post_title');
            $post->content = Input::get('post_content');

            if ($post->save()) {
                $tag_ids = Input::get('tags');
                $post->tags()->sync($tag_ids);
                $success = true;
            }
        } catch (\Exception $e) {
            // maybe log this exception, but basically it just here so we can rollback if we get a surprise
        }

        if ($success) {
            DB::commit();
            return Redirect::back()->withSuccessMessage('Post saved');
        } else {
            DB::rollback();
            return Redirect::back()->withErrorMessage('Something went wrong');
        }
    }
}

- , . - , , .

+6

sync() . , , :

$post->tag()->sync([$tag_id]);
+4

diplome_user, : :

enter image description here

//this is Diplome Model

    class Diplome extends Model
    {
    public function users()
        {
            return $this->belongsToMany('App\User','diplome_user')->withPivot('etablissement', 'annee', 'mention');;
        }
    }

DiplomeController, :

$user = Auth::user(); 

, , , :

$diplome = new Diplome();
$diplome->libelle = "the name";
$diplome->decription= "description of the ...";
$diplome->save();

:

   $diplome->users()->attach($user, ['etablissement'=> 'bib',
                                            'annee'=>'2015',
                                            'mention'=>'AB',
                                            ]);

:

enter image description here

+3
source

Source: https://habr.com/ru/post/1546957/


All Articles