Php Laravel:如何获取外键并将其传递给控制器
我试图从category表中获取id,它是article表中的外键,并在创建文章时将其放置在视图中的隐藏字段中,然后我想将其传递给文章控制器,我尝试过,但不确定如何执行此操作。显示的错误为 参数太少,无法正常工作 App\Http\Controllers\ArticleController::create,已传递0,并且 1预期Php Laravel:如何获取外键并将其传递给控制器,php,html,laravel,model,key,Php,Html,Laravel,Model,Key,我试图从category表中获取id,它是article表中的外键,并在创建文章时将其放置在视图中的隐藏字段中,然后我想将其传递给文章控制器,我尝试过,但不确定如何执行此操作。显示的错误为 参数太少,无法正常工作 App\Http\Controllers\ArticleController::create,已传递0,并且 1预期 不要在create方法中传递任何内容,而是在您的文章视图中显示类别的下拉列表,以便您可以选择任何类别,然后从该下拉列表中获取类别id到您的store方法 <?ph
不要在create方法中传递任何内容,而是在您的文章视图中显示类别的下拉列表,以便您可以选择任何类别,然后从该下拉列表中获取类别id到您的store方法
<?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() !!}
传递给文章管理员意味着??请编辑您的帖子并显示您的文章和类别迁移我正在创建一篇文章,因此需要将其传递给文章管理员以存储文章显示您的迁移。问题是Laravel没有将$id传递给ArticleController::create。您的路由配置中可能缺少一个参数。你能把文章发出去吗?非常感谢你的努力!!!有没有其他方法可以做到这一点,因为我在其他表中有外键,我需要将它们隐藏在一个表单中,我认为这种方法可能对它们不起作用。当然你可以做到。但在这种情况下,每次都需要显式地将category id传递给create函数。例如,您需要创建一篇新文章,但必须指定要为哪个类别创建文章。为此,在一个页面中显示类别列表,然后提供超链接以创建文章的方法,并将category_id作为参数传递。