John Davidson

php - How to retrieve specific column value from foreign key table?

0 comments
Message:


There are two Models. One is TradeLicence and another is BusinessCategory.
I have already established relationship between them. In TradeLicence there is a foreign key category_id which local key is id of BusinessCategory.


Trade Licence Model


class TradeLicence extends Model
{
use SoftDeletes;

protected $fillable = ['slug', 'name', 'category_id'];

public function businessCategories()
{
return $this->belongsTo(BusinessCategory::class,'category_id','id');
}

}

trade_licences migration Table:


    class CreateTradeLicencesTable extends Migration
{

public function up()
{
Schema::create('trade_licences', function (Blueprint $table) {
$table->id();
$table->string('slug')->unique();

$table->foreignId->('category_id');
$table->string->('name');

$table->timestamps();
});
}
}

Business Category Model:


class BusinessCategory extends Model
{
use SoftDeletes;

protected $fillable = ['slug', 'name', 'fees'];
}

business_categories migration table:


class CreateBusinessCategoriesTable extends Migration

{


public function up()
{
Schema::create('business_categories', function (Blueprint $table) {
$table->id();
$table->string('slug')->unique();

$table->string('name');
$table->double('fees');

$table->timestamps();
});
}

For adding a "Trade Licence", I need to select a "Business Category" and it stores the value
incategory_id column of trade_licencestable. I'm able to retrieve the name and fees value through the relationship inside TradeLicense model.


The problem is:


There is a another Model named DailyEarning. The show() method of DailyEarningController will display fees amount (fee of selected category, which was selected at the time of creating a new trade licence) and created_at and updated_at value of each Trade License.


My Question is:


How to get the values of fees, created_at and updated_at or which Query should I use to retrieve the values by using "Eloquent"?



My Answer:
You can retrieve the values of fees, created_at, and updated_at from the BusinessCategory and TradeLicence models using Eloquent relationships. Here's how you can do it:

In your DailyEarning model, define a relationship with the TradeLicence model:

php
class DailyEarning extends Model
{
public function tradeLicence()
{
return $this->belongsTo(TradeLicence::class);
}
}


Then, in your DailyEarningController's show() method, you can retrieve the fees, created_at, and updated_at values like this:

php
public function show($id)
{
$dailyEarning = DailyEarning::findOrFail($id);

$fees = $dailyEarning->tradeLicence->businessCategories->fees;
$created_at = $dailyEarning->tradeLicence->created_at;
$updated_at = $dailyEarning->tradeLicence->updated_at;

return view('dailyEarning.show', compact('fees', 'created_at', 'updated_at'));
}


This code will retrieve the fees, created_at, and updated_at values from the related BusinessCategory and TradeLicence models for the specified DailyEarning record.

Rate this post

4 of 5 based on 2627 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us