[英]Laravel 5.1 How to pass the id of an object to another view to be used as the foreign key in another table?
I have a projects table and a quotes table with the following structure: 我有一个项目表和一个具有以下结构的引号表:
Schema::create('quotes', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->index()->unsigned();
$table->decimal('quote', 10,0);
$table->timestamps();
On the project page I need a link which refers to the page for creating a new quote. 在项目页面上,我需要一个链接,该链接指向用于创建新报价的页面。 How can I pass the id of a project to the create view of a quote?
如何将项目的ID传递到报价的创建视图?
UPDATE UPDATE
I tried flashing the project id to the session an then getting it in the quote controller but by the time I'm trying to get it there the session is emty! 我尝试将项目ID刷新到会话中,然后在报价控制器中获取它,但是到我试图将其获取时,会话是空的!
//flashing project_id to the session
$request->session()->flash('project_id', $project->id);
or
$request->session()->flash('project_id', $project->id);
$request->session()->reflash();
or
$request->session()->flash('project_id', $project->id);
$request->session()->keep(['project_id']);;
//getting it in quote controller
$project_id=$request->session()->get('project_id');
what is the problem? 问题是什么?
Suppose that U have a Project model and $project
is an instance of it. 假设U有一个Project模型,而
$project
是它的一个实例。 Get Id of the project and pass it to the quote view like this: 获取项目的ID,并将其传递给报价视图,如下所示:
$project_id = $project->id;
return redirect()->intended('your_view')->with('project_id' , $project_id);
the $project_id
is stored in the sessions. $project_id
存储在会话中。 So you can easily retrieve it like this in the blade: 因此,您可以在刀片中像这样轻松地检索它:
if(session()->has('project_id')){
$project_id = session('project_id');
// do whatever u want with the $project_id and set it in quote table
}
First, you can define resourceful routes like this: 首先,您可以像这样定义资源丰富的路由:
Route::resource('projects.quotes', 'QuotesController');
Now, in your project page place this code where you want to display the link to create quotes: 现在,在您的项目页面中,将以下代码放置在要显示创建引号的链接的位置:
{!! link_to_route('projects.quotes.create', 'Create Quote', $project->id) !!}
Now, modify your QuotesController's create function like this: 现在,像这样修改QuotesController的create函数:
public function create($project_id)
{
$project = App\Project::find($project_id);
return view('quotes.create', compact('project'));
}
Now, you can use $project data in your create.php or create.blade.php view file 现在,您可以在create.php或create.blade.php视图文件中使用$ project数据
Laravel allows you to share variables between views. Laravel允许您在视图之间共享变量。 You can add it to the existing
AppServiceProvider
or create your own. 您可以将其添加到现有的
AppServiceProvider
或创建自己的AppServiceProvider
。
In the selected service provider: 在选定的服务提供商中:
public function boot()
{
view()->share('username','Joe');
}
The variable $username
is available to all views. 变量
$username
可用于所有视图。 You can also call share
method from the controller or anywhere before you render the view. 您也可以在呈现视图之前从控制器或任何地方调用
share
方法。
View::share('key','value');
Hope this help 希望这个帮助
I found a solution which worked.In the show method of projects controller I used the put()
method to flash the project->id to the session: 我找到了一个可行的解决方案。在项目控制器的show方法中,我使用了
put()
方法将project-> id刷新到会话中:
$request->session()->put('project_id', $project->id);
then in the create method of quotes controller I used the pull()
method to get the project_id out of session. 然后在报价控制器的create方法中,我使用了
pull()
方法来使project_id脱离会话。
$id=$request->session()->pull('project_id');
But there was a little problem with this approach, and in case the user decides to refresh the form page there will be no project_id key in session. 但是这种方法存在一点问题,并且如果用户决定刷新表单页面,则会话中将没有project_id键。 To tackle this I decided to redirect the user to the list of projects to select one again.
为了解决这个问题,我决定将用户重定向到项目列表,以再次选择一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.