我试图从类别表中获取ID(即文章表中的外键),然后在创建文章时将其放在视图中的隐藏字段中,然后将其传递给文章控制器,我已经尝试过,但是不确定如何执行此操作。显示的错误是“函数App \ Http \ Controllers \ ArticleController :: create()的参数太少,传递了0个且恰好期望1个”
答案 0 :(得分:1)
不要在create方法中传递任何内容,而是在文章视图中显示类别的下拉列表,以便您可以选择任何类别,然后从该下拉列表中将类别ID获取到存储方法。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::pluck('title', 'id')
return view('article.create', ['categories' => $categories]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->all();
Article::create($input);
return redirect('article');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
}
}
在您的视图中
{!! Form::open(array('action' => 'ArticleController@store', 'id' => 'createArticle')) !!}
@csrf
<div class="row large-12 columns">
{!! Form::label('', 'Category:') !!}
{!! Form::select('category_id', $categories, null, ) !!}
{!! Form::label('', 'Title:') !!}
{!! Form::text('title', null, ['class' => 'large-8 columns']) !!}
</div>
{!! Form::close() !!}