Home > Blockchain >  In Eloquent how would I check if a table row is associated with another?
In Eloquent how would I check if a table row is associated with another?

Time:01-19

Before I get into what my issue is, here is my setup. (FYI I am stuck using Laravel 7.4 at the moment so SOS):

Applications Table

    public function up()
    {
        Schema::create('applications', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

Reports Table

    public function up()
    {
        Schema::create('reports', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

ApplicationReports Table (I know the naming convention is off, but this is how I have to do it for code base)

    public function up()
    {
        Schema::create('applicationReports', function (Blueprint $table) {
            $table->unsignedInteger('application_id')->nullable(false);
            $table->unsignedInteger('report_id')->nullable(false);
        });
    }

Here is an example of the ApplicationReports table

application_id report_id
200 2

Then I have a many to many relationship setup between the Applications and Reports tables like so:

Applications Model

    public function reports() {
        return $this->belongsToMany(Report::class, 'applicationReports');
    }

Reports Model

    public function applications() {
        return $this->belongsToMany(Application::class, 'applicationReports');
    }

In the ReportsController I have a method that will pull all the reports that are in the reports table and then return them, that method looks a little bit like the code below (pseudo coded some of it). But what I am trying to do is only add reports that are associated with applications to the list. When I try the code below doing $report->applications->has($report->id) its returning false and I can't for the life of me figure it out.

public function getReports() {

        //Pseudo codeish right here, sorry. 
        $reports = gets all reports->with(['applications'])->orderBy('name')->get();

        $reportsList = [];

        foreach ($reports as $report) {
            if ($report->applications->has($report->id)) {
                $reportsList[] = $report;
            }
        }

        return $reportList;
    }

If I dd $report->applications the relationship is there and I can see it under #relations -> #attributes, any help would be appreciated!

CodePudding user response:

The has function is very straight forward.

You can query your reports that only contains applications by doing:

$reports = Report::with('applications')->has('applications')->get();

return $reports;

in other way you can also use whereHas this will accepts Query Builder that you can pass through.

For example:

$reports = Report::with('applications')
    ->whereHas('applications', function(Builder $query) {
       $query->orderBy('created_at');
    })->get();

return $reports;
  • Related