Yousef Altaf
Yousef Altaf

Reputation: 2773

Laravel 5.2 - eloquent relations

I have two tables one is templates and the other is template_images

Showing migration of the two tables.

Templates

Schema::create('templates', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title', 155);
            .....
});

Template Images

Schema::create('template_images', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('template_id')->unsigned();
            $table->boolean('master');
            $table->binary('image');
$table->foreign('template_id')->references('id')->on('templates')->onDelete('cascade');
            ...
        });

I have made relations in the both Models like this

Template-Model

class Template extends Model
{
public function tempImage()
    {
        return $this->hasMany('App\Models\TemplateImage', 'template_id');
    }
}

Template Image-Model

class TemplateImage extends Model
{
public function tempImage ()
    {
        return $this->belongsTo('App\Models\Template', 'template_id');
    }
}

What I want is

Select all from templates and select from template_images where template_images.template_id = templates.id and template_images.master = 1

Update This is how I need to get the image in blade

$temp->tempImage->image

What I try in my controller

$temps = Template::query()->with('tempImage')->get();

but still cant get it the right way.

Upvotes: 1

Views: 54

Answers (2)

Ankit
Ankit

Reputation: 1887

<?php

class Template extends Model
{
    public function tempImages()
  {
      return $this->hasMany('App\Models\TemplateImage', 'template_id');
  }

  public function tempMasterImage(){
    return $this->hasOne('App\Models\TemplateImage', 'template_id')->where('master', 1);    
  }
}

If every template can only have 1 master template image then it's better to create a separate relation in model for that. See the example model Class above. tempMasterImgage() is the new relation and it has hasOne relation with TemplateImage. With this in place you can simply do.

$template->tempMasterImage->image

from anywhere, You just need access to Template object.

Edit It's better to name your hasMany relationship function as plural. That's why I used tempImages in my example

Example Usage.

$template = Template::with(['tempImages', 'tempMasterImage'])->first();

//if you want tempImages to exclude masterImage as we have now seperate relation for that then
$template = Template::with(['tempImages' => function($query){
    $query->where('master', '<>', 1);
}])->with('tempMasterImage')->first();

//get master image
$template->tempMasterImage->image

//get all images
foreach($template->tempImages as $tempImage){
    echo $tempImage->image // 
}

Upvotes: 1

Nerea
Nerea

Reputation: 2157

If your $temp variable is an instance of model Template, you can not do $temp->tempImage->image becouse tempImage is a hasMany relationship.

So you can do two things, first:

$temps = Template::with(['tempImage' => function($query){
        $query->where('master',1);
    }])->get();

And then in your view

foreach ($temps as $temp) 
{
      foreach ($temp->tempImage as $temp_image) 
      {
        // Here your code for $temp_image->image
      }
}

Or second

$temps = TemplateImage::where('master',1)->with('tempImage')->get();

And then in your view

foreach ($temps as $temp) 
{
        // Here your code for $temp->image
}

Upvotes: 0

Related Questions