Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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/vba/14.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资源创建id为的模型_Php_Laravel_Resources - Fatal编程技术网

Php Laravel资源创建id为的模型

Php Laravel资源创建id为的模型,php,laravel,resources,Php,Laravel,Resources,我有两种型号'a'和'b'和a->hasmally('b')和b->belongsTo('a') 因此,当我创建一个'b'时,它应该恰好属于一个'a' 问题在于通常的Route::resource('b','bController')我只能创建这个'b',不知道它属于哪个'a' 我试图将bController中的create()方法编辑为create($id) Redirect::action('bController@create', $a->id) 仍然重定向到/b/create?2

我有两种型号
'a'
'b'
a->hasmally('b')
b->belongsTo('a')

因此,当我创建一个
'b'
时,它应该恰好属于一个
'a'

问题在于通常的
Route::resource('b','bController')
我只能创建这个
'b'
,不知道它属于哪个
'a'

我试图将
bController
中的
create()
方法编辑为
create($id)

Redirect::action('bController@create', $a->id)
仍然重定向到
/b/create?2
时出错

Missing argument 1 for bController::create()

当我使用
手机
用户
时,可能会更容易解围

每部手机都属于一个用户


如何创建手机?我如何给
create()
用户的参数,并且仍然使用
Route::resource

我认为你走错了方向,因为
user
Phone
是两个模型,因此
用户
有许多
手机
,所以这里
Phone
模型是一个子模型(相关)如果没有
用户
类和
电话
就不可能存在,因此您只需要通过控制器创建
用户
,在创建
用户
时将创建
电话
,例如:

Route::resource('users', 'UserController');
现在假设您有两个模型作为
User
Phone
,并且
User
模型有
phones
方法来建立关系(
phones=User->hasMany('Phone')
),而
Phone
模型有
User
方法来建立关系(
User=Phone->belomgsTo(“用户”)

现在,要创建
用户
,将使用如下存储方法:

// UserController.php
class UserController extends BaseController {

    // Other methods

    // Creates a User
    // URI: /users Method: POST
    public function store()
    {
        // Create a User using User model
        $user = User::create(...);
        if($user) {
            // Initialize/Create a Phone
            $phone = new Phone(array(...));
            if($phone) {
                // Save the Phone and relate with User
                $user->phones()->save($phone);
            }
        }
    }

}
$user = User::with('phones')->find(1);
这是基本思想,最后一点是,手机不需要创建
控制器,因为它是
用户
的一部分,所以当你创建一个新的
用户
时,就创建一个(或多个)创建
用户后,或更新
用户时,从
UserController
更新
电话

因此,如果您想加载一个
用户
及其相关的
手机
型号,您可以这样做:

// UserController.php
class UserController extends BaseController {

    // Other methods

    // Creates a User
    // URI: /users Method: POST
    public function store()
    {
        // Create a User using User model
        $user = User::create(...);
        if($user) {
            // Initialize/Create a Phone
            $phone = new Phone(array(...));
            if($phone) {
                // Save the Phone and relate with User
                $user->phones()->save($phone);
            }
        }
    }

}
$user = User::with('phones')->find(1);
因此,当您加载一个用户时,您可以加载与该用户相关的所有电话,因此在编辑用户时;您只需加载一个具有相关
电话
模型的
用户
,并将该模型传递给视图


要将新的
手机
添加到现有的
用户
中,您需要编辑
用户
模型,这样您就可以用
手机
加载
用户
模型,并将该
用户
模型传递到视图进行编辑。当新的
手机
添加到用户时,您只需要将该
手机
附加到现有的
User
,就是这样。希望它有意义。

您可以在传递
User
s id的地方创建自定义路由,嵌套资源,如
route::resource('users.phones','someController'))
,将其放在您的表单中。有很多方法可以实现这一点。我认为这会引导我走向正确的方向,谢谢!嘿,我认为我仍然存在一些问题。当我使用UserControllers编辑方法添加电话时,我在哪里检查用户是否只是想更改电子邮件?以及我是否仍然需要一个PhoneController,因为我不想这样做它的功能与其他资源相同:create()、store()、show($id)、edit($id)、update($id)、destroy($id)。它完全像一个资源,只是创建我必须更改的东西,但当我将所有这些放在UserController中时,这将结束我的混乱,不是吗?我发现的是: