如何从laravel中的相关表中获取数据

时间:2019-10-30 21:39:09

标签: laravel

我正在尝试通过与用户表的关系来获取动物的数据

这是我的控制人

<?php

namespace App\Http\Controllers;

use App\Animal;
use App\Clinic;
use App\Role;
use App\Slaughter;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class ClinicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()

    {

        $farms = User::where('role_id', 3)->get();
        $user = Auth::user();
        $animal = Animal::all();
        return view('clinic.index', compact('user', 'animal', 'farms'));
    }
 public function show($id)
    {
        $farm = User::query()->findOrFail($id);
        return view('clinic.show', compact('farm'));
    }

在我的情况下,获得农场用户的同时,我想通过此关系获得农场管理员注册的动物

Model

class Clinic extends Model
{
    protected $guarded = [];

    public function user(){
        return $this->belongsTo(User::class);
    }
}

通过我的修补匠,这种关系运转得很好 这是我的索引页面

@extends('layouts.app')

 @section('content')
 <br><br><br><br><br><br>
 <div class="container">
       <div class="row justify-content-center">
             <div class="col-md-20">
                   <div class="card">
                         <div class="card-header">
                               <center>
                                     <h1>Clinic Dashboard</h1>
                               </center>
                         </div>

                         <div class="card-body">
                               @if (session('status'))
                               <div class="alert alert-success" role="alert">
                                     {{ session('status') }}
                               </div>
                               @endif

                               <center>
                                     <h2>Welcome! <strong>{{ Auth::user()->name }}</strong></h2>
                               </center>
                               <hr>
                               <br>
                               <div class="container box">
                                     <div class="table-responsive">
                                           <table class="table table-striped table-bordered" style="background: white">
                                                 <thead>
                                                       <tr>
                                                             <th>Farm Id</th>
                                                             <th>Farm Name</th>
                                                             <th>Action</th>
                                                       </tr>
                                                 </thead>
                                                 @foreach( $farms as $farm)
                                                 <tbody>
                                                       <tr>
                                                             <td>{{ $farm->id }}</td>
                                                             <td>{{ $farm->name }}</td>
                                                             <td><a href="/clinic/{{ $farm->id }}"><button
                                                                               class="btn btn-outline-primary">View Farm
                                                                               Animals</button></a></td>
                                                       </tr>
                                                 </tbody>
                                                 @endforeach
                                           </table>
                                     </div>
                               </div>

                         </div>
                   </div>
             </div>
       </div>
 </div>

@endsection

还有我的route

Route::get('/clinic/{farm}', 'ClinicController@show');

最后是显示view的所有错误信息

 @extends('layouts.app')

 @section('content')
 <br><br><br><br><br><br>
 <div class="container">
       <div class="row justify-content-center">
             <div class="col-md-20">
                   <div class="card">
                         <div class="card-header">
                               <center>
                                     <h1>Farm Dashboard</h1>
                               </center>
                         </div>

                         <div class="card-body">
                               @if (session('status'))
                               <div class="alert alert-success" role="alert">
                                     {{ session('status') }}
                               </div>
                               @endif

                               <center>
                                     <h2>Welcome! <strong>{{ Auth::user()->name }}</strong></h2>
                               </center>
                               <hr>
                               <br>
                               <div class="container box">
                                     <div class="table-responsive">
                                           <table class="table table-striped table-bordered" style="background: white">
                                                 <thead>
                                                       <tr>
                                                             <th>Id</th>
                                                             <th>Animal Type</th>
                                                             <th>Sex</th>
                                                             <th>Farm</th>
                                                             <th>Clinic</th>
                                                             <th>Vaccination</th>
                                                             <th>Nutrition</th>
                                                       </tr>
                                                 </thead>
                                                 @foreach( $farm as $farm)
                                                 <tbody>
                                                       <tr>
                                                             <td>{{ $farm->animals->id }}</td>
                                                             <td>{{ $farm->animals->type->category }}</td>
                                                             <td>{{ $farm->animals->gender }}</td>
                                                             <td>{{ $farm->animals->user->name }}</td>
                                                             @if(! $farm->animals->clinic)
                                                             <td>N/A</td>
                                                             <td>N/A</td>
                                                             <td>N/A</td>
                                                             <td>
                                                                   <a href="/clinic/{{ $farm->animals->id }}/create">
                                                                         <button type="button" class="btn btn-primary">
                                                                               Attach Clinic Detail
                                                                         </button>
                                                                   </a>
                                                             </td>
                                                             @elseif( $farm->animals->clinic)

                                                             <td>{{ $farm->animals->clinic->user->name }}</td>
                                                             <td>{{ $farm->animals->clinic->vaccination ?? 'N/A' }}</td>
                                                             <td>{{ $farm->animals->clinic->nutrition ?? 'N/A'}}</td>

                                                             <td>
                                                                   <a
                                                                         href="/clinic/{{ $farm->animals->clinic->id }}/edit">
                                                                         <button type="button"
                                                                               class="btn btn-primary">Edit Animal
                                                                               Clinic details</button>
                                                                   </a>
                                                             </td>
                                                             @endif
                                                       </tr>
                                                 </tbody>
                                                 @endforeach
                                           </table>
                                     </div>
                               </div>
                         </div>
                   </div>
             </div>
       </div>
 </div>

 @endsection

我希望我提供了所有可能产生错误的字段。任何帮助将不胜感激,因为这对我来说是一个非常重要的项目 我收到的错误是

  

试图获取非对象的属性“动物”

3 个答案:

答案 0 :(得分:1)

您认为自己有@foreach($farm as $farm)

您需要什么:@foreach($farms as $farm)

编辑:这仅解决了部分问题,经过仔细检查,您的人际关系不堪重负,我将看看是否可以帮忙。

答案 1 :(得分:1)

此设置允许您获取特定诊所所有用户的所有动物。我也在show()方法上利用了路由/模型绑定。无论您在URL中传递什么ID,都会自动加载诊所。

// app/User.php

class User extends Autheticatable
{

  public function animals()
  {
    return $this->hasMany('App\Animal');
  }

  public function clinic()
  {
    return $this->belongsTo('App\Clinic');
  }

}
// app/Clinic.php

class Clinic extends Model
{
  public function users()
  {
    return $this->hasMany('App\User');
  }
}
// app/Animal.php

class Animal extends Model
{
  public function user()
  {
    return $this->belongsTo('App\User');
  }
}
class ClinicController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth');
  }

  public function show(Clinic $clinic)
  {   
    return view('clinic.show', compact('clinic'));
  }
}
// view
@foreach($clinic->users as $farm)

  @foreach($farm->animals as $animal)

    {{ $animal->name }} - {{ $animal->weight }} etc...  

  @endforeach

@endforeach

答案 2 :(得分:0)

要获得我愿意去的$农场的相关动物

public function show($id)
{
    $farm = User::with(['animals'])->findOrFail($id);
    return view('clinic.show', compact('farm'));
}

然后用刀片服务器

@foreach($farm->animals as $animal)
 {{ $animal->id }}
@endforeach

这当然是假设您在用户模型中设置了动物关系,就像

public function animals()
{
    return $this->hasMany('App\Animals')
}

(所有未经测试)