【问题标题】:Laravel - collection tableLaravel - 集合表
【发布时间】:2021-06-13 17:01:29
【问题描述】:

有两张桌子,一张是街道,另一张是房子的信息。 我尝试使用 house 表中的 street_id 列加入这些表。该表有效,我得到了信息。但是现在我怎样才能做到这一点,以便当我点击街道/{ID} 链接时,我会得到一个位于上面的房屋列表。 据我了解,需要添加代码

public function getHouse() 
{
$houses = Houses::where('street_id', $this->street_id)->get();
dd ($houses);
}

DD 等一下。我哪里做错了?

【问题讨论】:

  • 请记住,除非您向我们展示我们不知道您的架构

标签: php mysql laravel collections


【解决方案1】:

您可以在下面这样做。

街道模型:-

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Street extends Model
{
    public function houses()
    {
        return $this->hasMany('App\Models\House');
    }
}

房屋模型:-

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class House extends Model
{
    public function street()
    {
        return $this->BelongsTo('App\Models\Street','street_id');
    }
}

HouseController:-


<?php

namespace App\Http\Controllers;

use App\Models\Street;
use App\Models\House;
 
class HouseController extends Controller
{
    public function getHouse($street_id) 
    {
       //include model

        dump($street_id); // first you can get street_id

        // You can use with()
        $houses = House::with('street')->where('street_id',$street_id)->get();

        or 

         // also you can use whereHas()
        $houses = House::whereHas('street', function($q) use($street_id){
            $q->where('id',$street_id);
        })->get();

        or

        $houses = Street::with('houses')->where('id',$street_id)->get();
        
    }
}

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    你可以通过 laravel 的 Eloquent relationships 来实现。您需要像这样在 Street Model 文件中声明关系...

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Street extends Model
    {
        /**
         * Get the houses on the street.
         */
        public function houses()
        {
            return $this->hasMany(House::class);
        }
    }
    

    现在您可以在控制器中返回街道上房屋的视图;

    $houses = Street::find(1)->houses();
    
    <?php
    
    namespace App\Http\Controllers;
    
    use App\Models\Street;
    use Illuminate\Http\Request;
    
    class StreetsController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $street = Street::all();
            return view('street.all', compact('street'));
        }
    
        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function create()
        {
            //
        }
    
        /**
         * Store a newly created resource in storage.
         *
         * @param \Illuminate\Http\Request $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            //
        }
    
        /**
         * Display the specified resource.
         *
         * @param \App\Models\Appartaments $appartaments
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            $street = Street::find(1)->houses();
    //        $house = $street->houses;
            dd($street);
            return view('street.view', compact('street'));
        }
    
        /**
         * Show the form for editing the specified resource.
         *
         * @param \App\Models\Appartaments $appartaments
         * @return \Illuminate\Http\Response
         */
        public function edit(Appartaments $appartaments)
        {
            //
        }
    
        /**
         * Update the specified resource in storage.
         *
         * @param \Illuminate\Http\Request $request
         * @param \App\Models\Appartaments $appartaments
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, Appartaments $appartaments)
        {
            //
        }
    
        /**
         * Remove the specified resource from storage.
         *
         * @param \App\Models\Appartaments $appartaments
         * @return \Illuminate\Http\Response
         */
        public function destroy(Appartaments $appartaments)
        {
            //
        }
    }
    

    【讨论】:

    • 据我所知。接下来,我必须在柜台中注明return view (...., compact (street, house) 并且在非常视图中已经使用@foreach($house as $home){{$home-&gt;number}})
    • 完全正确。 @YuraLons
    • 我做了,但是当我去“街道”时,我有一个空白页。这是DD列表Illuminate\Database\Eloquent\Relations\HasMany {#1321 ▼ #foreignKey: "house.street_id" #localKey: "id" #query: Illuminate\Database\Eloquent\Builder {#1315 ▶} #parent: App\Models\Street {#1322 ▶} #related: App\Models\Houses {#1302 ▶} }
    • 你能分享你的控制器代码吗? @YuraLons
    • 哈哈Street::find(1)-&gt;houses(); 中的“1”是 id。所以应该是Street::find($id)-&gt;houses();
    猜你喜欢
    • 2013-06-18
    • 2020-07-03
    • 2017-06-18
    • 2020-12-11
    • 1970-01-01
    • 2017-03-17
    • 2021-09-05
    • 2015-09-06
    • 2019-09-30
    相关资源
    最近更新 更多