在laravel中插入与数据相关的两个表

时间:2019-03-16 23:54:57

标签: 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>

1 个答案:

答案 0 :(得分:0)

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

$cities = City::all()
return view('your-view', compact('cities'));

然后,您可以在选择中循环$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();