【问题标题】:Relationship data to view - Laravel 5.2要查看的关系数据 - Laravel 5.2
【发布时间】:2017-03-06 00:38:49
【问题描述】:

在输出要查看的关系数据时遇到问题。 错误消息是:“尝试获取非对象视图的属性”。

为了解释所有的任务都依赖于一个工作。 所以 Task 属于 Job 并且 Job 有很多任务。 我的模型中有所有关系,并在 Tinker 中对其进行测试,一切正常。

在我看来,我输出每个任务名称和作业名称

  @foreach ($tasks as $task)
    <div class="list-item">
         <span class="item-name">{{ $task->job->name }}
         <span class="item-name">{{ $task->name}} </span>
     </div>
  @endforeach

我的TaskController的索引函数:

public function index(Request $request)
{
    $label = Label::all();
    $user = User::all();
    $task = Task::orderBy('duedate')->get();
    $team = Team::all();
    $customer = Customer::all();
    $status = Status::all();
    $job = Job::all();
    return view('tasks.index')->with([
        'tasks' => $task,
        'teams' => $team,
        'customers' => $customer,
        'labels' => $label,
        'users' => $user,
        'jobs' => $job,
        'statuses' => $status,
    ]);
}

表模式/修补程序的输出

 id: 1,
 assigned_user_id: 1,
 team_id: 4,
 name: "Label many to many ",
 duration: 2,
 created_at: "2016-06-16 14:50:57",
 updated_at: "2016-07-05 09:10:34",
 job_id: 1,
 duedate: "0000-00-00 00:00:00",
 status_id: 3,
 job: App\Job {#702
   id: 1,
   name: "quia",
   jobnumber: "8076",
   customer_id: 2,
   status_id: 0,
   created_at: null,
   updated_at: null,
 },
 user: null,

关系

**工作模式**

class Job extends Model
{
protected $fillable = ['name', 'jobnumber', 'customer_id', 'status_id'];

/**
 * Get all Task for Job
 */
public function task()
{
    return $this->hasMany(Task::class);
}

任务模型

public function job()
{
    return $this->belongsTo(Job::class);
}

希望你能帮助我,谢谢!

【问题讨论】:

  • 请分享您的表架构和关系代码
  • 如果Job 有多个Task,则该关系应称为tasks 而不是task。它可能无法解决问题。

标签: laravel laravel-5.2


【解决方案1】:

参考你的回答:

在数据库中,一些 Task 条目的 job_id 指向一个不存在的 Job

您可以使用has 方法根据关系的存在来限制您的结果。见文档querying relationship absence

$task=Task::orderBy('duedate')->has('job')->get();

【讨论】:

    【解决方案2】:

    当您尝试打印不存在的模型值时会发生这种错误。尝试使用{{isset($task-&gt;job-&gt;name)?$task-&gt;job-&gt;name:'Task without Job'}} 打印并检查它的输出。

    【讨论】:

      【解决方案3】:

      在尝试访问它的值之前,您不会在 $task 上加载“工作”关系:

      public function index(Request $request)
      {
          $label = Label::all();
          $user = User::all();
          $task = Task::with('job')->orderBy('duedate')->get(); // loading the relationship
          $team = Team::all();
          $customer = Customer::all();
          $status = Status::all();
          $job = Job::all();
          return view('tasks.index')->with([
              'tasks' => $task,
              'teams' => $team,
              'customers' => $customer,
              'labels' => $label,
              'users' => $user,
              'jobs' => $job,
              'statuses' => $status,
          ]);
      }
      

      【讨论】:

      • 感谢您的回答。
      • 然后我调用未定义的方法 Illuminate\Database\Query\Builder::jobs()
      • 谢谢,但仍然得到“尝试获取非对象的属性”错误。 :(
      • "aae6d87ae7f8586ad5afa38cfe88fb5c0cdbb89f.php 第 18 行中的 ErrorException:尝试在 aae6d87ae7f8586ad5afa38cfe88fb5c0cdbb8f 中获取非对象的属性(查看:/Users.../resources/views/tasks/index.blade.php)第 18 行 CompilerEngine->handleViewException(object(ErrorException), '0') 在 PhpEngine.php 第 44 行..."
      • 请在您的 OP 中添加以下错误和 aae6d87ae7f8586ad5afa38cfe88fb5c0cdbb89f.php 第 18 行的代码。
      【解决方案4】:

      找到了解决方案。

      在数据库中,一些任务条目的 job_id 指向一个不存在的作业,就是这样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        • 2023-04-06
        • 2016-11-01
        • 2016-06-23
        相关资源
        最近更新 更多