将数据存储在Laravel Controller中的2个不同表中

时间:2017-04-12 10:44:25

标签: php laravel laravel-5 laravel-5.3

请帮助我一点帮助!我正在尝试与可以访问此业务的企业和用户建立一个系统。用户与数据库中的业务相关,其中包含名为“id_business”的列,该列是此用户可以管理的业务的ID。首先我创建用户,然后设置' id_business'默认情况下为0.然后,当我创建新业务时,我设置了业务名称,并从下拉列表中设置(对于值为' id_business = 0'的用户),我选择此业务的用户。现在我的问题是,我如何以一种形式存储新业务并设置' id_business'在users表中,就像我刚刚创建的新业务的id一样。我从中获取用户是列表:

<select name="id_business" class="form-control input-xlg">
 <option value="">Select User </option>
 @foreach($users as $user)
 <option value="{{$user->id}}">{{$user->name}}</option>
 @endforeach
</select>

在我的控制器中,我已经走了这么远,但它无法正常工作:

public function store(Request $request)
{

    $businesses = new Businesses();
    $businesses->name = $request->name;
    $businesses->user_create_id = Auth::user()->id;

    $user= Businesses_Users::first();

    if($businesses->save())
    {
        $request->id_business;
        $user->id = Businesses_Users::find($request->id_business);

        $user->id_business = $businesses->id;


        return redirect()->route('businesses',$businesses->id)->with('success', sprintf('Business successfully created.'));
    }
    else
    {
        return redirect()->back()->with('error', sprintf('An unexpected error occurred.Please fill a report form on this issue.'));
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:1)

缺少<h1>{% now "l d F H:i" %}</h1> 功能

save()