Laravel存储来自href的数据

时间:2018-05-28 08:13:36

标签: laravel navbar

我想制作一个选项,您可以在导航栏中创建项目,只需单击导航栏中的按钮即可创建项目。这是我的代码

这是导航栏刀片

<ul class="dropdown-menu">
      <li><a href="{{ action('InventoryController@store') }}">Create 
Inventory</a></li> 
</ul>

这是我在控制器中的商店方法

    public function store(Request $request)
{
    $inventory = new Inventory();
    $inventory->company_id = $request->get('company_id');
    $inventory->save();

    return redirect('inventories')->with('success', 'Inventory has been added');
}

点击该按钮后,它只会转到页面库存,但不会创建新库存。

更新

我的路线是:

Route::get('/create-inventory/{id}', 'InventoryController@store')->middleware('auth');

我的商店功能现在看起来像这样:

    public function store(Request $request, $id)
{
    $inventory = new Inventory();
    $inventory->company_id = $id;
    $inventory->save();

    return redirect('inventories')->with('success', 'Inventory has been added');
}

这是我的inventory.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Inventory extends Model
{
    **protected $fillable = ['company_id'];**
}

这是我的数据库迁移文件

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateInventoriesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('inventories', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('company_id');
        $table->timestamps();
        $table->dateTime('finished_at'); 
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('inventories');
}

}

更新2

这是我的inventory.php现在

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Inventory extends Model
{
    protected $fillable = ['company_id'];
}

它仍无效。

1 个答案:

答案 0 :(得分:0)

我认为问题可能出在生成的链接上,也许这有效:

 <a href="/create-inventory/{{Auth::user()->company_id]) }}">Create Inventory</a>