简体   繁体   English

Laravel:显示来自两个相关模型的信息

[英]Laravel: showing information from two related models

I'm using Laravel 5.4 in a project and I'm having trouble getting the information from related models to display correctly. 我在项目中使用Laravel 5.4,但无法从相关模型中获取信息以正确显示。 The program is for a small veterinary clinic where pet owners are first registered, and then each of their pets (each owner can have many) is added. 该计划适用于小型兽医诊所,在该诊所中首先注册了宠物主人,然后添加了他们的每只宠物(每个主人可以养很多只)。 In turn, each pet can have multiple consults, vaccinations, documents, etc. Because there are a minimum of three layers involved for each, I though it would be best to keep the url/paths as simple as possible, so instead of a pet's url being /owner/1/pet/1, I kept it as /pet/1, which I believe is why nothing I've tried seems to work as it should (all examples I've seen follow the first path). 反过来,每个宠物可以具有多个咨询,疫苗,文件等。由于每个宠物至少涉及三层,因此最好使URL /路径尽可能简单,所以最好不要使用宠物的URL /路径。 url是/ owner / 1 / pet / 1,我将其保留为/ pet / 1,我相信这就是为什么我尝试过的所有方法似乎都无法正常工作的原因(我看到的所有示例都遵循第一个路径)。

Pets Controller 宠物控制器

I can create new pets with no problem, all their information is saved and can be edited as well. 我可以毫无问题地创建新宠物,所有宠物的信息都可以保存并可以编辑。 The problem is when I try to view each of them, some all look as they should with their respective owner information, but others (the newest) give me an error Trying to get property of non-object (View: D:\\xampp\\htdocs\\veterinary\\resources\\views\\pets\\show.blade.php) 问题是,当我尝试查看每个对象时,其中的一些看上去都应具有各自的所有者信息,但是其他(最新)则给我一个错误, Trying to get property of non-object (View: D:\\xampp\\htdocs\\veterinary\\resources\\views\\pets\\show.blade.php)

public function index()
{
    $pets = Pet::all();
    $owner = Owner::find($pets);  
    return view('pets.index', compact('owner', 'pets'));
}

public function store(Request $request)
{
    $pet= new Pet;
    //////////////
    $pet->save();
}

public function show(Pet $pet)
{
    $owner = Owner::find($pet);  
    return view('pets.show', compact('owner', 'pet'));
}

Owners Controller 业主控制器

I can show the owner's information with no problem, but I can't seem to be able to retrieve a list of the pets registered under their name. 我可以毫无问题地显示主人的信息,但似乎无法检索以它们的名字注册的宠物的清单。 I've tried doing it different ways and have gotten different results (no errors but no pets found, object is null, query could not be converted to string, etc.). 我尝试以不同的方式进行操作,并获得了不同的结果(没有错误,但没有找到宠物,对象为null,查询无法转换为字符串等)。 I left the functions with the commented ways I've tried so far. 到目前为止,我对函数进行了评论。

public function index()
{
    $owners = Owner::all();
    return view('owners.index', compact('owners'));
}

public function getPets($id)
{
    $pets = Pet::owner($id)->get();
    return $pets;
}

public function store(Request $request)
{
    $owner = new Owner;
    //////////////
    $owner->save();
}

public function show(Owner $owner)
{
    //$pets = Owner::pets();  
    //$owner = Owner::find($owner->id_owner);
    //$pets = $this->getPets($owner->owner_id);
    //$pets = Pet::find($owner)->owner()->where('id_owner', '=', $owner->id_owner)->get();

    $pets = DB::table('pets')->where('owner_id','=',$owner->id_owner);
    $pets = Pet::find($pets)->owners;
    return view('owners.show', compact('owner', 'pets'));
}

Pets Model 宠物模型

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

Owners Model 车主模型

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

Apologies for the extremely long post, I'm just having a hard time figuring out how to properly achieve all of this. 很长的帖子很抱歉,我很难弄清楚如何正确实现所有这些。 I've tried all sorts of different ways over the last couple of days, ending up with partially working outcomes or straight up messing everything up. 在过去的几天中,我尝试了各种不同的方式,最终得到了部分有效的结果,或者直接弄乱了所有内容。 Thanks for your help! 谢谢你的帮助!

you can achieve this just follow this steps. 您只需按照以下步骤即可实现。

setting up our model relationship .. 建立我们的模型关系..

Pets Model 宠物模型

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

Owners Model 车主模型

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

now .. our Owner Controller 现在..我们的所有者控制器

public function index()
{
    $owners = Owner::with('pets')->get();
    return view('owners.index', compact('owners'));
}


public function store(Request $request)
{
    $owner = new Owner;
    // $owner->blahblah = $request->blahblah;
    $owner->save();
}

public function show(Owner $owner)
{
    $owner->pets;
    return view('owners.show', compact('owner'));
}

in the owner.index you can see all owners using owner.index中,您可以看到所有所有者正在使用

<ul>
@foreach($owners as $owner)
    <li>{{ $owner->somefield }}
        <ul>
        @foreach($owner->pets as $pet)
            <li>{{ $pet->somefield }}</li>
        @endforeach
        </ul>
    </li>
@endforeach
</ul>

in owner.show owner.show中

<ul>
    <li>{{ $owner->somefield }}
        <ul>
        @foreach($owner->pets as $pet)
            <li>{{ $pet->somefield }}</li>
        @endforeach
        </ul>
    </li>
</ul>

and that is how you access pets using the owner .. 这就是您使用所有者访问宠物的方式..

now our Pet Controller 现在是我们的宠物管理员

public function index()
{
    $pets = Pet::with('owner')->get();
    return view('pets.index', compact('pets'));
}

public function store(Request $request)
{
    // there are several way to save this pet ..
    // 1

    $pet = new Pet;
    $pet->owner_id = $request->owner_id;
    // $pet->somefield = $request->somevalue;
    $pet->save();

    // 2
    $owner = Owner::find($request->owner_id);
    $owner->pet()->create(['somefield'=>$request->somevalue]);

    // 3
    $pet = new Pet;
    $pet->somefield = $request->somevalue;
    $owner = Owner::find($request->owner_id);
    $pet = $owner->pet()->save($pet);
}

public function show(Pet $pet)
{
    $pet->owner;
    return view('pets.show', compact('pet'));
}

for the pet views , they have the same logic as the owner views .. 对于pet views ,它们与owner views具有相同的逻辑。

pets.index pets.index

<ul>
@foreach($pets as $pet)
    <li>{{ $pet->somefield }}
        <ul>
            <li>{{ $pet->owner->somefield }}</li>
        </ul>
    </li>
@endforeach
</ul>

pets.show pets.show

<ul>
    <li>{{ $pet->somefield }}
        <ul>
            <li>{{ $pet->owner->somefield }}</li>
            <li>My Owner's other pets
            <ul>
            @foreach($pet->owner->pets()->where('id','!=',$pet->id)->get as $otherpet)
                <li>{{ $otherpet->somefieldlikepetname }}</li>
            @endforeach
            </ul>
            </li>
        </ul>
    </li>
</ul>

hope this really helps .. 希望这真的有帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM