【问题标题】:Laravel 5 Relationship Not Working?Laravel 5 关系不起作用?
【发布时间】:2015-04-09 12:16:33
【问题描述】:

在我的应用中,我有几个模型:用户和个人资料。用户模型仅适用于公司,我的应用程序仅适用于公司。用户注册时,只需填写姓名、电子邮件地址和密码。我的个人资料模型有公司名称、地址等列。我的个人资料表格不起作用;不保存到数据库。这是设置:

表单控制器:

public function update($company_name)
{
  $user = User::whereCompanyName($company_name)->firstOrFail();

      $user->fill(Input::all());

      $user->save();

      flash('You have successfully edited your profile');

      return redirect('/');
}

用户.php:

public function profile()
{
    return $this->hasOne('Profile');
}

Profile.php:

protected $fillable = ['company_name', 'company_logo', 'company_founded'];

public function user()
{
    return $this->belongsTo('App\User', 'user_id','ID');
}

表格:

{!! Form::model($user, array('method' => 'PATCH', 'route' => array('profile.update', $user->company_name), 'files' => true)) !!}
{!! Form::hidden('user_id', Auth::user()->id) !!}

    // more fields

<div class="form-group">
    {!! Form::label('company_name', 'Company Name') !!}
    {!! Form::text('company_name', null, ['class' => 'form-control']) !!}
 </div>

{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}

我是否设置了正确的关系?没有任何内容保存到数据库中。

【问题讨论】:

  • 从您的控制器中向我们提供一些代码。
  • @mininoz 刚刚意识到。更新了
  • 另外不要忘记在 User.php 中更改profile()return $this-&gt;hasOne('App\Profile');

标签: laravel relationship laravel-5


【解决方案1】:

您只更新用户模型和用户模型。您还需要在profile 关系中设置属性:

$user->update(Input::all());
$user->profile->update(Input::all());

您的控制器操作也可以稍微整理一下,通过使用route–model binding 注入您的User 模型实例,并使用服务容器来提供Request 实例,这样您就不用@ 987654326@门面:

public function update(User $user, Request $request)
{
    $user->update($request->all());
    $user->profile->update($request->all());

    flash('You have successfully updated your profile.');

    return redirect('/');
}

【讨论】:

  • 我喜欢你的方式。我会用我的下一个控制器试试。
  • 我收到'PDOException' with message 'SQLSTATE[HY000]: General error: 1 no such column: _method'
  • @Sylar 我的猜测是 - 因为你正在做Input::all() Laravel 尝试更新与名为 _method 的表单中的隐藏字段对应的列。请改用Input::except(),并传递一个包含所有应该更新的表单输入名称的数组。或者您也可以使用Input::only() 并指定更新时必须使用的输入名称。
  • @Sylar 如果您使用的是模型,则不应该发生这种情况。 Laravel 只会在将数组传递给 update() 时尝试填充 $fillable 属性中声明的属性。
  • 但是我在哪里放:$user-&gt;update(Input::all()); $user-&gt;profile-&gt;update(Input::all());
【解决方案2】:

我想评论但没有足够的声誉:(前几天我发现这种方法有一点问题:

$user->update(Input::all());
$user->profile->update(Input::all());

在这种情况下,相关模型(示例中的配置文件)中的 mutators 不会被调用(可能是错误):

public function setLoremIpsumAttribute($attr)
{
    # code
}

controller 中我尝试了另一种方法,它奏效了:

$user->update($request->all());
$user->profile->fill($request->all()['profile'])->push();

【讨论】:

    【解决方案3】:

    在 Laravel 5 中,当您想与关系链接时,您需要例如(与评论相关的帖子)使用评论中的方法。

    Post::find(id)->comment()->where(your where statement)
    

    来自 Laravel 的文档:

    如果您需要为检索 cmets 添加进一步的约束,您可以调用 cmets 方法并继续链接条件:

    $comments = Post::find(1)->comments()->where('title', '=', 'foo')->first();
    

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 2017-04-23
      • 1970-01-01
      • 2015-09-24
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多