laravel - 在laravel中插入数据相关的两个表
问题描述
我在城市中得到空结果但数据保存,当插入时我插入数据city_id
并且在查询中插入city_id
为空,你能帮我吗?谢谢
这是我的模型:
class Company extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function city()
{
return $this->belongsTo(City::class);
}
和城市模型:
class City extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function companies()
{
return $this->hasMany(Company::class);
}
和我的控制器
public function store(Request $request)
{
$company_name = $request->input('name');
$city_name = $request->input('city_id');
$company_address = $request->input('address');
$data = new \App\Models\Company();
$data->name = $company_name;
$data->address = $company_address;
$data->city_id = $city_name;
// $data->save();
$city = new \App\Models\City();
//$data->companies->add($data);
$data->city()->associate($city->city_id);
$data->save();
if($data->save()){
$res['message'] = "Success!";
$res['value'] = "$data";
//return response($res);
return redirect('/companies')->with('success', 'Data has been added');
}else{
$res['message'] = "Not Success!";
$res['value'] = "$data";
return response($res);
}
结果:
{
"id": 6,
"city_id": null,
"name": "aaa",
"address": "bbb",
"created_at": "2019-03-16 23:45:49",
"updated_at": "2019-03-16 23:45:49",
"deleted_at": null,
"city": null
},
and the form:
<form method="post" action="{{ url('/branches') }}">
<div class="form-group">
<label for="cityname" class="col-sm-3 control-label"><i class="fa fa-building"> </i>Company Name</label>
<div class="col-sm-4">
<select class="form-control" name="company_id" id="company_id" style="width: 100%;">
<option selected="selected">Company Name : </option>
@if (count($company) > 0)
@for($i = 0; $i < count($company); $i++)
<option value="{{ $company[$i]->company_id }}">{{ $company[$i]['name'] }}</option>
@endfor
@endif
</select>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Branch Name :</label>
<input type="text" class="form-control" name="name"/>
</div>
<div class="form-group">
<label for="address">Address : </label>
<input type="text" class="form-control" name="address"/>
</div>
<div class="form-group">
<label for="cityname" class="col-sm-3 control-label"><i class="fa fa-building"> </i>City</label>
<div class="col-sm-4">
<select class="form-control" name="city_id" id="city_id" style="width: 100%;">
<option selected="selected">Pilih Kota : </option>
@if (count($city) > 0)
@for($i = 0; $i < count($city); $i++)
<option value="{{ $city[$i]->city_id }}">{{ $city[$i]['name'] }}</option>
@endfor
@endif
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
解决方案
我相信您的选择是错误的,首先,在您的控制器功能中,您应该像这样将城市发送到此页面:
$cities = City::all()
return view('your-view', compact('cities'));
然后你可以在 select 中循环$cities
变量并为每个城市做一个选项:
<select class="form-control" name="company_id" id="company_id" style="width: 100%;">
@foreach($cities as $city)
<option value="{{$city->id}}">{{$city->name}} </option>
@endforeach
</select>
在您的商店功能中,您可以这样做:
$data->city_id = $request->city_id;
$data->save();
推荐阅读
- node.js - 为什么猫鼬会因为这个错误使我的机器人崩溃?
- javascript - 如何在 React 中创建查询字符串
- mongodb - 只显示最近的条目而不是旧的 Mongodb
- c++ - 如何分析在 kubernetes pod 上运行的服务?
- python - pyqt Parent是QTextDocument,parent的线程是,当前线程是Thread2
- java - Spring Boot中的PageRequest未定义
- matlab - 绘制时间序列时如何调整网格密度
- python - 如何使用 tweepy 进行迭代,更正
- mongodb - 如何在 React Native 中进行功能计时器倒计时并与 MongoDB 数据库同步
- c - 当我使用“strcat”函数时,它会导致分段错误......为什么?