![](/img/trans.png)
[英]MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one
[英]how to get all rows where specific column = id of other row in different table
我正在嘗試獲取付款表中的所有行,這里還有另一個稱為項目的表,我想獲取支付表中的所有行,其中project_id列=項目表中行的ID我正在使用Laravel框架,請幫我一些忙碼
付款方式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
protected $table = 'payments';
public function projects() {
return $this->belongsTo('App\Project');
}
}
項目模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $table = "projects";
public function payments() {
return $this->hasMany('App\Payment');
}
}
路由文件
Route::get('special/{project_id}', 'SpecialController@index');
控制器中的索引功能
<?php
namespace App\Http\Controllers;
use App\Project;
use App\Payment;
use Illuminate\Http\Request;
class SpecialController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Project $project_id)
{
return view('special.index')->with(['projects' => $project_id]);
}
這是我的問題,請幫忙😀
在您的控制器中,我看到您已經為Project注入了一個實例,但名為$ project_id。 多數民眾贊成在不是一個ID,ID將被解析為一個Project的實例。
public function index(Project $project_id)
只是一個筆記。 使用索引方法顯示清單,除非它是嵌套資源。
public function index(Project $project)
{
//assuming that $project is an instance of Project
$project = $project->with('payments')->get();
//$project will now include the related payments as well
//if you want to check do this, for debugging uncomment next line
//dd($project->toArray());
return view('special.index')->with(['project' => $project]);
}
代碼中發生的事情是$ project及其相關付款被檢索。 在with('payments')
,'payments'是關系的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.