首页 > 解决方案 > 在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"> &nbsp;</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"> &nbsp;</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>

标签: laravel

解决方案


我相信您的选择是错误的,首先,在您的控制器功能中,您应该像这样将城市发送到此页面:

$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();

推荐阅读