Shiva
4 years ago
292
16
General

Adding data to a pivot table

Posted 4 years ago by Shiva

I created a table that refrences both the menu table and content table. It doesn't save in the content_menu table.

contentController

<?php

class ContentController extends \BaseController {

    /**
     * Display a listing of the resource.
     * GET /content
     *
     * @return Response
     */
    public function index()
    {
        $content = Content::all();
        return View::make('content::index', compact('content'));
    }

    /**
     * Show the form for creating a new resource.
     * GET /content/create
     *
     * @return Response
     */
    public function create()
    {
        $content = Content::with('menu')->get();
        $menu_options = Menu::lists('title', 'id');
        return View::make('content::create', compact('content'))->with('menu_options', $menu_options);
    }

    /**
     * Store a newly created resource in storage.
     * POST /content
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Content::$rules);

        if($validation->fails()){
            return Redirect::route('content::index')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        if($validation->passes()){
            Content::create($input);

            $content = Content::all();
            return View::make('content::index', compact('content'));
        }
    }

    /**
     * Display the specified resource.
     * GET /content/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     * GET /content/{id}/edit
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $content = Content::find($id);

        if(is_null($content)){
            return Redirect::route('admin.content.edit');
        }

        return View::make('content::edit', compact('content'));
    }

    /**
     * Update the specified resource in storage.
     * PUT /content/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = Input::all();
        $validation = Validator::make($input, Content::$rules);

        if($validation->fails()){
            return Redirect::route('admin.content.edit')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors.');
        }

        if($validation->passes()){
            $content = Content::FindOrFail($id);
            $content->update($input);
            return Redirect::route('admin.content.index', $id);
        }
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /content/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        Content::find($id)->delete();
        $content = Content::all();
        return View::make('content::index', compact('content'));
    }

}

Content model

<?php

class Content extends \Eloquent {
    protected $fillable = array('title', 'content', 'meta_title', 'meta_description', 'keywords');
    protected $guarded = array('id');

    protected $table = 'content';

    public static $rules = array(
        'title' => 'required',
        'content' => ''
    );

    public function menu(){
        return $this->belongsTo('ContentMenu');
    }
}

ContentMenu model

<?php

class ContentMenu extends \Eloquent {
    protected $fillable = array('content_id', 'menu_id');
    protected $guarded = array('id');

    protected $table = 'content_menu';

    public function menu(){
        return $this->belongsTo('Menu');
    }

    public function content(){
        return $this->belongsTo('Content');
    }
}

My create.blade.php

@extends('templates::admin')
@section('content')


    <div class="row">
        <div class="col-lg-12">
            <p>This is the create page</p>
            {{ Form::open(array('route' => 'admin.content.store', 'class' => 'add-form')) }}
                <div class="form">
                    <div>
                        {{ Form::label('menu_id', 'Menu') }}
                    </div>
                    <div>
                        {{ Form::select('menu_id', $menu_options) }}
                    </div>
                    <div>
                        {{ Form::label('title', 'Title') }}
                    </div>
                    <div>
                        {{ Form::text('title','', array('id' => 'title', "class" => "form-control")) }}
                    </div>
                    <div>
                        {{ Form::label('content', 'Content') }}
                    </div>
                    <div>
                        {{ Form::textarea('content') }}
                    </div>
                    <div>
                        {{ Form::label('meta_title', 'Meta Title') }}
                    </div>
                    <div>
                        {{ Form::text('meta_title') }}
                    </div>
                    <div>
                        {{ Form::label('meta_description', 'Meta Description') }}
                    </div>
                    <div>
                        {{ Form::text('meta_description') }}
                    </div>
                    <div>
                        {{ Form::label('keywords', 'Keywords')}}
                    </div>
                    <div>
                        {{ Form::text('keywords') }}
                    </div>
                    <div>
                        {{ Form::submit('Submit', array("class" => "btn btn-default submit", "role" => "button")) }}
                    </div>
                </div>
            {{ Form::close() }}
        </div>
    </div>
@stop

Please sign in or create an account to participate in this conversation.