Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

What have you tried so far? Can you post your code?

If you haven't started, the first place to look is the docs http://laravel.com/docs/4.2/eloquent#relationships

Relationships are insainely easy in Laravel. If you have read the docs and are still unsure which way to go and the type of relationship etc, let us know.

Last updated 2 years ago.
0

@T2theC Thanks for you reply.

I'm a bit confuse about this eloquent relation stuff. After going through the laravel doc, i found may be "Has Many Through" relation will work for me. But, can't able to fig out how to do. Here are my codes..

Trip (Models)

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Trips extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'trips';


	public function albums(){		
		return $this->hasManyThrough('TripsAlbum', 'AlbumImages');
	}

}

TripsAlbum(Model)

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class TripsAlbum extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'trips_album';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	//protected $hidden = array('password', 'remember_token');

	public function trips()
	{
	    return $this->belongsTo('Trips');
	}

}

AlbumImages(Model)

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class AlbumImages extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'album_images';

}

My Controller Function

public function tripsLanding()
{
    $userTrips = Trips::find(1)->albums;  
    echo "<pre>";
    print_r($userTrips); 
    echo "</pre>";
}
Last updated 2 years ago.
0

So what's the array printed in the controller?

Last updated 2 years ago.
0

@chimmel It's giving me following error..

Illuminate \ Database \ QueryException (42S22) 
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'album_images.trips_id' in 'field list' (SQL: select `trips_album`.*, `album_images`.`trips_id` from `trips_album` inner join `album_images` on `album_images`.`id` = `trips_album`.`album_images_id` where `album_images`.`trips_id` = 5)
Last updated 2 years ago.
0

Ok, so you can define multiple relationships on a model. I think something like this should do the trick for you...

#Trips

  • hasMany - trip_albums - if you want a trip to have more than album per trip
  • hasManyThrough - trips_albums, album_images

#TripAlbums

  • belongsToMany - Trips
  • hasMany - album_images

#AlbumImages

  • belongsTo/Many - TripAlbums - you might want an album image to belong to more than one trip album.
  • hasManyThrough trips, trip_albums

It really depends on your use for these models. Do you want to view an image and see what trip/album it relates to etc? I find it useful to sketch this out. Ask yourself things like, has a trip got more than one album etc?

The SQL error you are getting is a good start. It means things are hooked up, but not quite right. It basically means that it can't find 'trips_id' in the album_images table.

I've not use hasManyThrough for a while, so it might take a bit of playing to get these relationships right. You may not even need it if you the other relations defined. Again, it really depends on what you want for the app and how you intend to create a new entry in the db etc.

I hope that helps a little. First off, I would get the relations next door to each sorted first - trips and trip_albums and then trip_albums and album_images. After that if you find you need the hasManyThrough when querying, chuck it in.

Last updated 2 years ago.
0

@T2theC Thanks for this information. I'll check it .

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2024 Laravel.io - All rights reserved.