Php 多多拉威尔
我有三张桌子Php 多多拉威尔,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我有三张桌子 职位 类别 职类职位 我想在我的category_post表中添加category_id和post_id,以创建包含类别的帖子。我有类别的复选框 主要主题是,我想创建多个类别的职位。所以我想在category_post表中添加category_id和post_id 拉威尔有可能吗 我看过很多教程,现在我累了。请帮帮我 请参阅下面的代码 我的迁移表: <?php use Illuminate\Database\Migrations\Migration; use Illumi
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateCategoriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('category_slug');
$table->timestamps();
});
}
public function up()
{
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->string('meta');
$table->text('body');
$table->string('image');
$table->timestamps();
});
}
public function up()
{
Schema::create('category_post', function(Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->integer('post_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('categories');
}
public function down()
{
Schema::drop('posts');
}
public function down()
{
Schema::drop('category_post');
}
}
首先,您需要一个类别ID数组:
// view
@foreach ($categories as $category)
{{ Form::checkbox('categories[]', $category->id, $post->categories->contains($category->id)) }}
{{ Form::label($category->name) }}
@endforeach
如果在编辑时使用模型绑定,则将类别[]
重命名为其他名称以避免字段名冲突
然后,您需要将帖子与这些类别同步:
// posts controller @store
... // validate everything, categories too
$categories = Input::get('categories');
$post = Post::create($data);
$post->categories()->sync($categories);
return Redirect::route('admin.posts.index');
标记为正确答案,如果它让其他人知道。你好deczo,现在我可以创建帖子。但是当我试图编辑这篇文章时,类别没有被选中。但这是需要的。如何从透视表中设置复选框类别的值?这意味着,我想显示我在帖子中选择的类别。请帮助我。请检查我的编辑-3rd参数,以便Form::checkbox()
将完成此工作。如果给定的类别在文章的类别集合中,它将检查该字段。谢谢回答。我加了这个。但当我试图编辑文章时,只能选中第一个复选框。其他人不。。怎么办。?请1检查我的编辑,应该有contains
nothas
方法,2确保类别
字段名中没有冲突
<?php
class CategoriesController extends \BaseController {
public function __construct()
{
$this->beforeFilter('csrf',['on'=>'post']);
}
/**
* Display a listing of categories
*
* @return Response
*/
public function index()
{
$categories = Category::all();
return View::make('admin.categories.index', compact('categories'));
}
/**
* Show the form for creating a new category
*
* @return Response
*/
public function create()
{
return View::make('admin.categories.create');
}
/**
* Store a newly created category in storage.
*
* @return Response
*/
public function store()
{
$validator = Validator::make($data = Input::all(), Category::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
Category::create($data);
return Redirect::route('admin.categories.index');
}
/**
* Display the specified category.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$category = Category::findOrFail($id);
return View::make('admin.categories.show', compact('category'));
}
/**
* Show the form for editing the specified category.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$category = Category::find($id);
return View::make('admin.categories.edit', compact('category'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$category = Category::findOrFail($id);
$validator = Validator::make($data = Input::all(), Category::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$category->update($data);
return Redirect::route('admin.categories.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Category::destroy($id);
return Redirect::route('admin.categories.index');
}
}
<?php
class Category extends \Eloquent {
protected $fillable = ['name','category_slug'];
public static $rules = ['name'=>'required|min:3','category_slug'=>'required|min:3'];
public function posts()
{
return $this->belongsToMany('Post');
}
}
@extends('admin.layouts.main')
@section('content')
<h2>Create Post</h2>
@if ($errors->has())
<div class="error">
<p>The Following errors have</p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{ Form::open(array('action' => 'PostsController@store')) }}
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('meta') }}
{{ Form::text('meta') }}
</p>
<p>
{{ Form::label('slug') }}
{{ Form::text('slug') }}
</p>
<p>
{{ Form::label('body') }}
{{ Form::text('body') }}
</p>
<p>
@foreach ($categories as $category)
{{ Form::checkbox('category_id', $category->id) }}
{{ Form::label($category->name) }}
@endforeach
</p>
<p>
{{ Form::label('image') }}
{{ Form::text('image') }}
</p>
{{ Form::submit('Crate Post') }}
{{ Form::close() }}
@stop
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::get('/', function()
{
return View::make('hello');
});
Route::resource('admin/categories', 'CategoriesController');
Route::resource('admin/posts', 'PostsController');
// view
@foreach ($categories as $category)
{{ Form::checkbox('categories[]', $category->id, $post->categories->contains($category->id)) }}
{{ Form::label($category->name) }}
@endforeach
// posts controller @store
... // validate everything, categories too
$categories = Input::get('categories');
$post = Post::create($data);
$post->categories()->sync($categories);
return Redirect::route('admin.posts.index');