Laravel - 合集table
Laravel - collection table
有两个 table,一个 table 有街道,一个 table 有关于房子的信息。
我尝试使用房屋 table 中的 street_id 列加入 table。 table 有效,我得到了信息。但是现在我怎样才能做到当我点击街道/{ID} link 时,我会得到一个位于它上面的房屋列表。
据我了解,有必要添加代码
public function getHouse()
{
$houses = Houses::where('street_id', $this->street_id)->get();
dd ($houses);
}
DD 等一下。我哪里错了?
您可以通过 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);
}
}
现在在您的控制器中 return 街道上房屋的视图,您可以这样做;
$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)
{
//
}
}
您可以按照下面的方式进行操作。
街头模特:-
<?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');
}
}
家庭控制器:-
<?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();
}
}
希望对您有所帮助。
有两个 table,一个 table 有街道,一个 table 有关于房子的信息。 我尝试使用房屋 table 中的 street_id 列加入 table。 table 有效,我得到了信息。但是现在我怎样才能做到当我点击街道/{ID} link 时,我会得到一个位于它上面的房屋列表。 据我了解,有必要添加代码
public function getHouse()
{
$houses = Houses::where('street_id', $this->street_id)->get();
dd ($houses);
}
DD 等一下。我哪里错了?
您可以通过 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);
}
}
现在在您的控制器中 return 街道上房屋的视图,您可以这样做;
$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)
{
//
}
}
您可以按照下面的方式进行操作。
街头模特:-
<?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');
}
}
家庭控制器:-
<?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();
}
}
希望对您有所帮助。