Support the ongoing development of Laravel.io →
Input Database Forms

Hi

I have two pages, the first one is menu and the other one is content.

In my menu view its just a normal blade template where I can add and edit my menu items.

Then I have the content page, which has 2 textboxes and one dropdown and a submit. In my dropdown is a list of all the menus that are in my database.

In my database the table content has a column called menu_id which refrences the id in the menu table.

What I would like to do is to add the data that I entered into the content form to be saved.

Here is my ContentController

public function store()
	{
		//

		
		$input = \Input::all(); //puts the get and post variables into an array
		$validation = \Validator::make($input, \Content::$rules); //checks the validation rules

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

		if($validation->passes()){
			\Content::create($input); //inserts the data into the table
			//return \Redirect::route('admin.content.index');
			$content = \Content::all();
			return \View::make('admin.content.partials.form-list', compact('content'));
		}

		/*return \Redirect::route('admin.content.create')
			->withInput()
			->withErrors($validation)
			->with('message', 'There were validation errors.');

		$result = $validation->passes();
		echo $result;	*/

		//print_r($input);
	}

My Content model

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Content extends \Eloquent implements UserInterface, RemindableInterface{
	use UserTrait, RemindableTrait;
	
	protected $fillable = array('menu_id', 'title', 'description'); //allows the data to be updated
	protected $guarded = array('id'); //prevents people from changing the value

	protected $table = 'content';

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

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

my view with the form

@extends('layouts.admin')
@section('content')

{{ Form::open(array('route' => 'admin.content.store', 'class' => 'add-form')) }}
	<ul>
		<li>
			{{ Form::label('title', 'Title') }}
			{{ Form::text('title', '', array('id' => 'title')) }}
		</li>

		<li>
			{{ Form::label('description', 'Description') }}
			{{ Form::text('description', '', array('id' => 'description')) }}
		</li>

		<li>
			{{ Form::label('menu', 'Menu') }}
			{{ Form::select('menu', $menu_options) }}
		</li>

		<li>
			{{ Form::submit('Submit', array("class"=>"btn btn-default", "role"=>"button")) }}
		</li>
	</ul>
{{ Form::close() }}

@if($errors->any())
	<ul>
		{{ implode('', $errors->all('<li class="error">:message</li>')) }}
	</ul>
@endif	
@stop

If there is anything else you need please let me know.

Last updated 2 years ago.
0

I have done some changes to my controller I replaced

$input = \Input::all();

with

$input = \Input::with('menu')->all();

but now I get this error in my firebug

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to undefined method Illuminate\\Http\\Request::with()","file":"\/Applications\/MAMP\/htdocs\/laravel-auth\/vendor\/laravel\/framework\/src\/Illuminate\/Support\/Facades\/Facade.php","line":208}}
Last updated 2 years ago.
0
Solution

I managed to fix it. My select box didn't have the menu_id on it

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiva shiva Joined 24 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.