简体   繁体   English

Laravel 5.1如何将对象的ID传递给另一个视图以用作另一个表中的外键?

[英]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.

相关问题 如何将ID从视图传递到控制器以用作另一个表中的外键? - How to pass the id from a view to a controller to be used as the foreign key in another table? 检查一个表的ID是否用作另一个表的外键 - Check if the ID of one table is used as a foreign key in another Laravel将外键保存在另一个表中 - Laravel saving foreign key in another table Laravel 如果手动命名id,外键无法通过约束()找到另一个表的主键 - Laravel Foreign Key can't find Primary Key of another table through constrained() if id is named manually Laravel Eloquent - 将数据透视表的id用作另一个表中的外键 - Laravel Eloquent - Using pivot table's id as a foreign key in another table 如何根据从另一个外键显示的外键显示表格 / Laravel 6 - How to display a table according to a foreign key which is display from another foreign key / Laravel 6 将表的主键存储到另一个表的外键中— Laravel 5.8 - Storing Primary Key of table to Foreign key of another table — Laravel 5.8 如何在laravel 5.1迁移中使用外键 - how to use foreign key in laravel 5.1 migration 检索表的最后插入ID以在另一个表中用作外键 - retrieving last inserted ID of a table for using as Foreign key in another table 如何添加具有外键值的表引用laravel中的另一个表? - How to add table that has foreign key value referring to another table in laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM