Support the ongoing development of Laravel.io →
Database Eloquent

I am trying to Join multiple tables and get results

I have now 2 tables

1- projects_info (id - pro_title - pro_address ...)

2- projects_images (id - image - image_id)

I need to grape the related images from the 'projects_images' where image_id = projects_info id

here what I try

here is my Route

//Projects Route
Route::get('our-projects', 'projectController@currentProjects');
Route::get('project/{id}', 'projectController@viewProject');

//Projects details
//Route::get('project/{id}/about-project', 'projectController@viewProject');
Route::get('project/{id}/about-project', array('as' => 'about-project', 'uses' => 'projectController@viewProject'));
Route::get('project/{id}/project-images', array('as' => 'project-images', 'uses' => 'projectController@viewProjectImages'));

Projects Model

class Projects extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    protected $table = 'project_info_arabic';

    public function projectImages()
    {
        return $this->hasMany('ProjectsImages');
    }

}

ProjectsImages Model

class ProjectsImages extends Eloquent implements UserInterface, RemindableInterface
{
    use UserTrait, RemindableTrait;

    protected $table = 'projects_images';

    public function project()
    {
        return $this->belongsTo('Projects');
    }
}

and this is my ProjectController

public function currentProjects()
    {
        $pro = Projects::all();
        return View::make('projects.currentProjects', ['pro' => $pro]);
    }

    public function viewProject($id)
    {
        $vp = Projects::find($id);
        return View::make('projects.viewProject', ['viewPro' => $vp]);
    }

    public function viewProjectImages($id)
    {
        $vpi = ProjectsImages::with('project')->get();
        //$vpi = DB::table('projects_images')->select('id', 'image', 'image_id')->get();
        return View::make('projects.projectDetails.projectImages', ['viewProImg' => $vpi]);
    }

and here is how I am trying to include each page in my projectsDetails VIEW

<div class="mCustomScrollbar">
                                @if(Request::path()=="project/$viewPro->id/about-project")
                                    @include('projects.projectDetails.aboutProject')
                                @elseif(Request::path()=="project/$viewPro->id")
                                    @include('projects.projectDetails.aboutProject')
                                @elseif(Request::path()=="project/$viewPro->id/project-images")
                                    @include('projects.projectDetails.projectImages')
                                @endif
                            </div>

and last the projectImages VIEW

<ul>
            @foreach($viewProImg as $vpi)
                <li class="col-sm-3 col-xs-12">
                    {{ HTML::image("images/projects/$vpi->image", '', array('class'=>'img-responsive')) }}
                </li>
            @endforeach
        </ul>

**My Problem

1- what I got here is all the images in the table it should be only the images related to this project

any help here please

Last updated 2 years ago.
0
Solution

it was my projectController

this line

$vpi = ProjectsImages::with('project')->get();

should be

$vpi = ProjectsImages::where('image_id', $id)->get();
0

Sign in to participate in this thread!

Eventy

Your banner here too?

johnef johnef Joined 18 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.