(Laravel) Get data from a table which an ID corres

2019-09-15 04:10发布

I was playing around with my first Laravel project and my question above might be a little bit confusing but let me explain:-

I have 3 tables (actually I have more, but lets ignore it), I have standards, stddetails & sections as shown:-

Databases

So the foreign key corresponds are below:-

  • column stdPK from table standards = column stdFK from table stddetails

  • column ssctionPK from table sections = column ssctionFK from
    table stddetails

And the scenario is this:-

Lets say I have an $id to be match on stdPK from table standards. With that $id, I am required to get all data from table sections. The problem is, I can't find a right query for that as both standards and sections tables only linked with stddetails table.

Currently my query in my web.php is this:-

Route::get('getstddtl/{id?}', function ($id) {

     $stdsec = Section::
            leftJoin('stddetails', 'ssctionFK', '=', 'ssctionPK')
            ->join('standards', function($join) use ($id){
                $join->on('stdPK', '=', 'stdFK')
                        ->where('stdFK', $id);
            });

    return view('standarddtl', [
        'stdsec' => $stdsec

    ]);
});

I thought it should be easy, boy... I was wrong... I hoped someone can help me with this because my brain have very limited thinking capacity.

UPDATE 1:-

I have set the eloquent relationship in each model and uses Laravel's Eloquent method in retrieving the data:-

$stdsec = Stddetail::with('section')->find($id);

All data is retrieved from both stddetails & sections tables, the problem now is difficulty on displaying data from column ssctionName in sections table in a display page as it return an error.

The related code on the display page is below:-

@foreach ($stdsec as $task)
       {{strtoupper($task->ssctionName)}}
@endforeach

The error shown:-

Error on display foreach

I think the eloquent method is good, now the display part is giving me trouble. Any ideas on how to solve this?

UPDATE 2:-

Here's the models for each table:-

Table standards as Standard model:-

namespace App;

use Illuminate\Database\Eloquent\Model;

class Standard extends Model
{

    public $primaryKey = 'stdPK';

    public function stddetail()
    {
        return $this->hasMany(Stddetail::class, 'stdFK', 'stdPK');
    }
}

Table stddetails as Stddetail model:-

namespace App;

use Illuminate\Database\Eloquent\Model;

class Stddetail extends Model
{
    public $table = "stddetails";
    public $primaryKey = 'sdtlPK';
    protected $fillable = ['stdFK', 'sdtlPgNo', 'sdtlLnNo', 'sdtlText', 'sdtlShrtnote', 'sdtlSchm', 'svrFK', 'ssctionFK', 'sdtlRefLbl'];

    public function standard()
    {   
        return $this->belongsTo(Standard::class, 'stdFK');
    }

    public function section()
    {   
        return $this->belongsTo(Section::class, 'ssctionFK');
    }

}

Table sections as Section model:-

namespace App;

use Illuminate\Database\Eloquent\Model;

class Section extends Model
{
    public $primaryKey = 'ssctionPK';
    public function stddetails()
    {
        return $this->hasMany(Stddetail::class, 'ssctionFK', 'ssctionPK');
    }
}

1条回答
爷、活的狠高调
2楼-- · 2019-09-15 04:51

Take a look at eloquent relationships: https://laravel.com/docs/5.4/eloquent-relationships

You should be able to set proper relationships(hasMany, belongsTo, etc) between your models (tables) and get all data with a single command (Eloquent will create needed queries for you).

On an unrelated note, I would suggest improving your naming convention. It is really hard to understand event basic links with all acronyms and short names used

查看更多
登录 后发表回答