mercredi 16 mars 2016

Laravel trying to query relatinship model hasMany

I have the following tables in my application:

projects projects_plot_types

Projects can have many plot types. I have the following relationship setup in the projects model.

/**
 * The plot types that belong to the project.
 *
 * @return Object
 */
public function plotTypes()
{
    return $this->hasMany('App\Models\Project\ProjectsPlotTypes');
}

I want to be able to get the plot type by querying it's name on the project model.

I have tried this, but it does not work:

$project->with('plotTypes')->whereHas('plotTypes', function ($query) use ($row) { 
    $query->where('name', $row->plot_name);
})->first()->plotTypes->first()->id;

Can someone point me in the right direction?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire