我有2张桌子:制造商和品牌。
制造商: 字段:ID和制造商
品牌: 字段:具有“ manufacturer_id”字段的许多其他字段,这些字段索引“ Manufacturers”表的“ id”。
我有一个选择框,该选择框应该从制造商表中加载制造商字段。 当存储在db中时,我需要在Brands表中存储制造商的'id'。
这是一对一的关系,但我不知道如何使用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>
我在修补程序中得到了正确的值,但是我不知道如何保存在数据库中。
非常感谢任何帮助。...
答案 0 :(得分:0)
使用Model :: create()和Model :: update()方法时,将使用模型上的fillable或guarded属性过滤数据。
由于您的品牌模型在可填充内容中没有'manufacturers_id',因此该字段不会保存在数据库中。
此外,您需要将选择的输入命名为“ manufacturers_id”,以将正确的属性发送到后端。
提示: