Yura Lons
Yura Lons

Reputation: 73

Laravel - collection table

There are two tables, a table with streets, and a table with information about the house. I tried to join the tables using the street_id column in the houses table. The table works, I get the information. But now how can I make it so that when I click on the street / {ID} link, I get a list of houses that are located on it. As I understand it, it is necessary to add the code

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

DD get it wait. Where did I go wrong?

Upvotes: 2

Views: 824

Answers (2)

Lyndon
Lyndon

Reputation: 301

You can achieve this with laravel's Eloquent relationships. You need to declare the relationship in the Street Model file like this...

<?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);
    }
}

now in your controller where you return the view for houses on a street, you can do this;

$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)
    {
        //
    }
}

Upvotes: 0

Yudiz Solutions
Yudiz Solutions

Reputation: 4459

You may do like this below.

Street Model:-

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

House Model:-

<?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();
        
    }
}

Hope that helps.

Upvotes: 2

Related Questions