Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将透视表连接到laravel中的其他两个表_Php_Laravel_Pivot_Pivot Table - Fatal编程技术网

Php 将透视表连接到laravel中的其他两个表

Php 将透视表连接到laravel中的其他两个表,php,laravel,pivot,pivot-table,Php,Laravel,Pivot,Pivot Table,我有两张桌子。一个有用户,一个有位置。我创建了一个名为user_locations的透视表。用户可以有一个或多个位置。一个位置可以属于一个或多个用户。所以我在这些表上有一个多对多的关系。我从laravel那里读过attach方法,但我不太确定如何使用它 在“我的用户和位置”表中,我有一个id,该id在“用户id”和“位置id”下进入我的透视表 $user = new User; $user->organisation_id = Auth::user()->organisation_i

我有两张桌子。一个有用户,一个有位置。我创建了一个名为user_locations的透视表。用户可以有一个或多个位置。一个位置可以属于一个或多个用户。所以我在这些表上有一个多对多的关系。我从laravel那里读过attach方法,但我不太确定如何使用它

在“我的用户和位置”表中,我有一个id,该id在“用户id”和“位置id”下进入我的透视表

$user = new User;
$user->organisation_id = Auth::user()->organisation_id;
$user->fill(Input::except('locations'));

$input_locations = Input::only('locations');

foreach($input_locations as $input_location){
$location_id = Location::where('name', '=', $input_location)->get();
$location = Location::find($location_id);

$user->User_location()->associate($location);
目前我遇到了两个问题。In Location::where('name…)我应该检查$input\u locations中的id,但我不知道如何获取id?有一个id和一个名称来自input::only('locations')

我的第二个问题是最后一行代码。在这里我应该使用attach。我在laravel文档中发现:

您还可以传递应存储在关系透视表上的属性数组:

 $user->roles()->attach(1, array('expires' => $expires));
所以我认为应该是这样的:

 $user->user_locations()->attach(the new user id, array('location_id' => $location_id));
真的希望有人能帮我,因为我真的不知道从这里走下去

更新:

这是我的用户控制器:

 public function createUserPost()
{
    $user = new User;
    $user->organisation_id = Auth::user()->organisation_id;
    $user->fill(Input::except('locations'));

    // pak het field locations
    $input_locations = Input::only('locations');

    if($user->save())
    {
        // Koppel de id's van de locatie aan de nieuwe user
        $new_user_id = $user->id;
        foreach ($input_locations as $key => $value) {
            $location = Location::find($value);
            User::find($new_user_id)->locations()->attach($location);
        }
        return Redirect::to('users/'.$user->id);
    }
    return $this->createUser($user);
}
在我的用户模型中,我有以下规则:

 'locations' => 'required',
这是必需的,因为它是必填字段。现在,当我点击提交按钮时,它不会保存用户,而是显示字段“位置”填充时是必需的。问题是select的名称是locations。但我不知道如何将其放入我的表中,因为我没有名为locations的列。这只是为了填充透视表并确保表单中需要该字段。

为什么不使用

您需要多对多关系。在这种情况下,请将数据透视表重命名为
location\u user
(默认情况下,数据透视表应按字母顺序排列)

然后定义您的关系:

// In User.php model
protected $fillable = ['first_name', 'last_name']; \\ List of all allowable items to be filled

public function locations()
{
    return $this->hasMany('Location');
}

// In Location.php model
public function users()
{
    return $this->hasMany('User');
}
在此之后,有许多方法可以将
位置
附加到
用户
。最好的方法是:

public function createUserPost()
{
    $user = new User(Input::all); // Use the $fillable in the model to allow mass assign

    // This should be done via a relationship
    $user->organisation_id = Auth::user()->organisation_id;

    // pak het field locations
    $input_locations = Input::only('locations');

    $location_ids = [];
    foreach($input_locations as $key => $id) {
        $location_ids[] = Location::find($value)->id;
    }
    $user->locations()->attach($location_ids);

    if($user->save())
    ...
}

但是一个用户可以被分配到多个位置。这就是为什么我需要一个透视表。我在创建一个用户时正在这样做。我可以这样做吗:$user->locations()->attach($location)?这在每个位置的foreach中创建您的用户firs(即
save()
),然后
attach($location)
OK,但随后我遇到了另一个问题。我使用带有规则的laravel表单验证。因此我的表单是这样打开的:{{form::model($user)}在用户规则中,我知道位置是必需的。但不知怎么的,它没有保存并说位置是必需的。我不明白问题是什么。你能解释一下吗?为什么你的用户模型中甚至有一个
locations
列?你不应该!我不明白该列的用法。不,我没有是的。但是表单使用模型用户的规则。我如何使用位置模型中的规则?如果“位置”=>“必需”应该放在那里,如果我理解正确,则应该为您正在设置规则的模型的列设置规则。因此,用户模型不应该有位置规则,因为模型没有任何位置上。您应该在位置模型中定义位置规则。保存后,在两个模型上执行验证,如果两个密码都无法访问计算机,则继续。我建议您阅读Jeffery Way Laravel验证教程,了解如何有效地使用它们。凯。感谢所有帮助。现在一切正常。唯一的问题是密码word字段。不知怎的,它没有发布密码字段。