【问题标题】:Laravel 5.4 search relationship with pivot in bladeLaravel 5.4 与刀片中枢轴的搜索关系
【发布时间】:2017-06-01 17:25:42
【问题描述】:

我正在构建一个搜索功能,但我遇到了一个看似简单但显然很难的功能。

在搜索结果中,我已成功显示所有具有给定技能的用户。 (在这种情况下,它是belongsToMany()relationship),以及它们的first_namelast_nameemail。我希望条目的最后一部分显示用户在相关技能中的能力(在代码中称为skill_level)。目前,skill_level 是技能/用户关系的枢纽;它们由以下四个字符串之一表示:“Wants to Learn”、“Novice”、“Intermediate”、“Expert”,每个字符串的 id 为 1-4(含)。

以下是相关代码:

控制器(部分)

    public function results(Request $request){

    $theskillName = $request->input('theskillName');
    $users = User::whereHas('skills', function ($q) use($theskillName) {
        $q->where('name', '=', $theskillName);
    })->get();
    $skills = Skill::orderBy('name', 'ASC')->get();
    if(count($users) > 0){
        return view('search.search', compact('users', 'skills', 'theskillName'));
    }
    else { 
        return view ('search.search', compact('skills'));
    }
  }

search.blade.php(部分)

@if(isset($users))
<p>The following people have {{ $theskillName }} as one of their skills:</p>
<h2>People</h2>
<table class="table table-striped">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Skill Level</th>
        </tr>
    </thead>
    <tbody>
        @foreach($users as $user)
        <tr>
            <td>{{$user->first_name}}</td>
            <td>{{$user->last_name}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->???->skill_level }}</td> /* <- What replaces ??? here? */
        </tr>
        @endforeach
    </tbody>
</table>
@endif

User.php(部分)

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id', 'first_name', 'last_name', 'middle', 'email', 'status', 'role', 'password'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'remember_token'
];

// Gets all of the skills the user knows
public function skills()
{
    return $this->belongsToMany('App\Skill')->withPivot('skill_level');
}

到目前为止,我尝试的所有内容要么为条目的最后一部分返回null,要么吐出错误Trying to get property of non-object。谁能提供一些关于在这里做什么的见解?

更新:我尝试了明显的答案$user-&gt;pivot-&gt;skill_level,它给了我Trying to get property of non-object.

【问题讨论】:

  • 搜索时添加pivot_column_name
  • 所以我认为在你的情况下它会是 pivot_skill_level

标签: php laravel search pivot-table


【解决方案1】:

一个用户有很多技能。所以你有很多技能水平。你可以这样访问它。

@foreach($user->skills as $skill)
    {{ $skill->pivot->skill_level }}
@endforeach

{{ $user->skills()->first()->pivot->skill_level }}

编辑:根据 cmets,您可以执行此操作以获取查询技能的查询级别。

{{ $user->skills()->where('skills.name', $theskillName)->first()->pivot->skill_level }}

【讨论】:

  • 第一个答案不起作用,因为我正在搜索具有查询技能的 USER。我需要有@foreach ($users as user) 否则它将无法正常工作。我的同事(向我提供了我发布的答案)说你的第二个答案不起作用,因为它只会抓住用户的第一个技能,而不是被查询的那个。
  • 两个答案都行。第一个将获取所有设置的技能级别。 $user 模型在 $users 的循环中,所以没有问题。第二个是如何直接获取的示例。您从未在问题中提到您只想显示查询的技能水平。
【解决方案2】:

一位同事给出了这样的答案:

&lt;td&gt;{{ $user-&gt;skills-&gt;first(function($skill) use($theskillName) { return $skill-&gt;name === $theskillName;})-&gt;pivot-&gt;skill_level}}&lt;/td&gt;

但不确定这有多安全。

【讨论】:

    猜你喜欢
    • 2020-08-23
    • 2019-08-20
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多