laravel,显示从另一个表下拉的数据

时间:2017-10-26 02:17:27

标签: php laravel laravel-blade

大家好我有一张桌子制造商(列 ' manufacturer_id' &# 39;制造商' )并希望显示"选择"此表格基于不同但链接的视图' asset.blade.php' 动态生成的列表。

模型 - Asset.php

namespace App;

class Asset extends Model
{
    protected $primaryKey = 'asset_id';

}

控制器 - AssetController

namespace App\Http\Controllers;

use App\Asset;
use App\Manufacturer;

use Illuminate\Http\Request;

class AssetController extends Controller
{
    public function asset(){

        $assets = Asset::all();
        return view('viewAsset', ['assets' => $assets]);
    }

    public function getmanufacturerlist() {
        $Manufacturer = Manafacturers::all();
        return view('manufacturer')->with('data', $Manufacturer);
    }

    public function add(Request $request){
        $this->validate($request, [
            'asset_id' => '',
            'asset_category_id' => 'required',
            'manufacturer_id' => 'required',
            'department_id' => 'required',
        ]);
        $assets =  new Asset;
        $assets  ->asset_id  = $request->input('asset_id');
        $assets  ->asset_category_id = $request->input('asset_category_id');
        $assets  ->manufacturer_id = $request->input('manufacturer_id');
        $assets  ->department_id = $request->input('department_id');
        $assets  ->save();
        return redirect('/viewAsset') ->with('info', 'New Asset Saved Successfully!');
    }
}

manufacturer.blade.php

<select name="manufacturer_id">
<option>Select a Manufacturer</option>
@foreach( $manufacturer as $manufacturers )
<option value=" <?php echo $manufacturers->manufacturer_id; ?>" > <?php echo $manufacturer->manufacturer?> </option>
 @endforeach
</select>

当我执行我的项目时 - 它工作正常,我看到下拉列表。但如果我在表格中加入manufacturer.blade.php

 @include('forms.manufacturer')

我收到错误

Undefined variable: manufacturer.

如何修复错误?我的Laravel版本是5.4。

1 个答案:

答案 0 :(得分:0)

您将$manufacturer作为$data传递给您的观点。

更新它以正确传递名为$manufacturer的变量。

return view('manufacturer')->with('manufacturer', $Manufacturer);

或者使用compact来实现更清洁的方法:

return view('manufacturer', compact('manufacturer'));