[英]Laravel: How to get foreign key and pass it to controller
I'm trying to get a the id
from the category table
which is the foreign key
in the article table
and placing it in a hidden
field in a view when creating an article, I then want to pass it to the article controller
, i've had a go at trying but unsure how to do this. 我试图让该
id
从category table
这是foreign key
的article table
,并把它放在一个hidden
创建文章时场的看法,我再想它传递给article controller
,我我曾经尝试过但不确定如何做到这一点。 The error that is displaying is 显示的错误是
Too few arguments to function App\\Http\\Controllers\\ArticleController::create(), 0 passed and exactly 1 expected
函数App \\ Http \\ Controllers \\ ArticleController :: create()的参数太少,传递了0个且恰好期望1个
Don't pass anything in create method, instead display dropdown list of category in your article view so that you can select any category, then get Category id from that dropdown to your store method. 不要在create方法中传递任何内容,而是在文章视图中显示category的下拉列表,以便您可以选择任何类别,然后从该下拉列表中将Category 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)
{
}
}
And in your View 在您看来
{!! 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() !!}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.