0

我有 2 张桌子:制造商和品牌。

制造商:字段:id 和制造商

Brands: fields : 许多其他带有“manufacturer_id”字段的字段,这些字段索引“Manufacturers”表的“id”。

我有一个选择框,它应该从制造商表中加载制造商字段。在数据库中存储时,我需要将制造商的“id”存储在 Brands 表中。

这是一对一的关系,但我不知道如何使用 Laravel 资源控制器模型.. CRUD

我也设置了外键

这是 BrandsContoller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Brands;
use App\Manufacturer;
use App\Contents;
use App\Usage;
use App\Type;
use App\SideEffect;

class BrandsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $brand_names = Brands::latest()->paginate(5);
        return view('brands.index', compact('brand_names'))->with('i',(request()->input('page',1)-1)*5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        // $manufacturerList = Manufacturer::find(2);
        //$brand = Manufacturer::find(3)->brand;
        return view('brands.create', compact('brand'));
    }

    public function test($id){

        $manufacturerss = Manufacturer::find($id);
        return view('test', compact('manufacturer'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // request()->validate([
        //     'brand_name' => 'required',
        //     'short_description' => 'required',
        //     'manufacturers_id' => 'required',
        //     'contents_id' => 'required',
        //     'usages_id' => 'required',
        //     'types_id' => 'required',
        //     'photo' => 'required',
        //     'side_effects_id' => 'required',
        //     'common_use' => 'required',
        //     'dosage_directions' => 'required',
        //     'precautions' => 'required',
        //     'contradictions' => 'required',
        //     'possible_side_effects' => 'required',
        //     'drug_interactions' => 'required',
        //     'missed_dose' => 'required',
        //     'overdose' => 'required',
        //     'storage' => 'required',
        //     'discalimer' => 'required',
        // ]);
        // $manufacturer->brands()->save($manufacturer);
        // $manufacturer->manufacturer()->associate($manufacturer)->save();

        Brands::create($request->all());
        // $brand = new Brands;
        // $brand -> manufacturer_id = $data-> manufacturer_id;
        // $brand -> save();
        // @foreach($manufacturerList as $item)
        // {

        //     $brand->manufacturer()->create(['manufacturer_id' => $item]);

        // }
        // @endforeach

        return redirect()->route('brands.index')->with('success', 'Brand created succesfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $brand_name = Brands::find($id);
        return view('brands.show',compact('brand_name'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $brand_name = Brands::find($id);
        return view('brands.edit', compact('brand_name'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        request()->validate([
            'brand_name' => 'required',
            'short_description' => 'required',
            'manufacturers_id' => 'required',
            'contents_id' => 'required',
            'usages_id' => 'required',
            'types_id' => 'required',
            'photo' => 'required',
            'side_effects_id' => 'required',
            'common_use' => 'required',
            'dosage_directions' => 'required',
            'precautions' => 'required',
            'contradictions' => 'required',
            'possible_side_effects' => 'required',
            'drug_interactions' => 'required',
            'missed_dose' => 'required',
            'overdose' => 'required',
            'storage' => 'required',
            'discalimer' => 'required',
        ]);
        Brands::find($id)->update($request->all());
        return redirect()->route('brands.index')->with('success', 'Brand updated succesfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Brands::find($id)->delete();
        return redirect()->route('brands.index')->with('success', 'Brand sucessfully deleted');
    }
}

这是品牌模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Brands extends Model
{
    protected $table = 'brands';
    protected $fillable = ['brand_name','short_description','content_id','usage_id','type_id','photo','side_effect_id','common_use','dosage_directions','precautions','contradictions','possible_side_effects','drug_interactions','missed_dose','overdose','storage','discalimer'];

    public function manufacturer()
    {
    return $this->belongsTo('App\Manufacturer');
    }
}

这是制造商型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Manufacturer extends Model
{
    protected $table = 'manufacturers';
    protected $fillable = ['manufacturer'];

    public function brand()
    {
    return $this->hasOne('App\Brands','manufacturer_id');
    }
}

这是我的前端,它有选择框:

<strong>MANUFACTURER : </strong>

      <br>
      <select id="manufacturers" name="manufacturers" class="form-control item_view" placeholder="Please choose" onchange="">
      <option>Select Manufacturer</option>
      <option value="0">Add New</option>


      <option value="" >test</option>



      </select>

我在 tinker 中得到了正确的值,但我不知道如何保存在 db..

非常感谢任何帮助....

4

1 回答 1

0

使用 Model::create() 和 Model::update() 方法时,将使用模型上的可填充或受保护属性过滤数据。

由于您的 Brand 模型在可填充项中没有“manufacturers_id”,因此该字段不会保存在数据库中。

此外,您需要将您的选择输入命名为“manufacturers_id”,以将正确的属性发送到后端。

小费:

  • 我建议在您的模型、数据库和关系中使用Eloquent 模型约定。例如:使用“品牌”模型,而不是“品牌”模型,以及制造商 ID,而不是制造商 ID。使用这些约定,Laravel Eloquent ORM 可以轻松管理您的查询,并且代码可以更加清晰和简洁。
于 2018-10-30T15:05:41.537 回答