I have Room, Gallery and Images. I want to associate a gallery to the room and then I want to access the Images of the assigned Gallery using the Room model. I'm new into Laravel, I looked in YouTube lessons, and the documentation but didn't found out a solution for my problem.
Room.php:
class Room extends Model
{
protected $table = 'rooms';
public function gallery()
{
return $this->hasOne('App\Gallery');
}
}
Gallery.php:
class Gallery extends Model
{
protected $table = 'gallery';
public function images()
{
return $this->hasMany('App\Image');
}
public function room()
{
return this->belongsTo('App\Room');
}
}
RoomController.php:
$room = Room::findOrFail($id);
$room_gallery = $room->gallery()->images;
return $room_gallery;
With Eloquent relationships, you access them as properties to access the related model(s) or access the method to query or perform additional manipulation.
Since you want a gallery model and it's related image models, you would access the properties of both:
$room_gallery = $room->gallery->images;
With a HasOne, $room->gallery is essentially equal to $room->gallery()->first(). With HasMany, $gallery->images would be essentially equal to $gallery->images()->get().
However, this may be a case where the HasManyThrough relationship would come in handy.
Related
In my database, i have two tables notification and alertFrequency. The notification has field id and website_url and the alert frequency has id and notification_id. Both tables has models which is one to many. The notification can have one or many alertFrequency.
class Notification extends Model {
public function alertFrequencies() {
return $this - > belongsToMany('AlertFrequency::class');
}
}
namespace App;
use Illuminate\ Database\ Eloquent\ Model;
class AlertFrequency extends Model {
protected $table = 'alertFrequencies';
public function notification() {
return $this - > belongsTo(Notification::class);
}
}
in the notification model, i wrote a function called alert, that will give me the laastest alert associated with a specific websie.
public function alert(){
$alert_frequency = AlertFrequency::find('notification_id')->first();
$o_notification = $this->alertFrequencies()->where('notification_id',$alert_frequency->id)
->select('created_at')->orderBy('created_at')->first();
if($alert_frequency ==null){
return false;
}
return created_at;
}
Is this a right way to extract the data? i would appreciate any suggestions and helps?
Notification hasMany AlertFrequency
public function alertFrequencies(){
return $this->hasMany('App\AlertFrequency');
}
and,
$alert_frequency = AlertFrequency::with('notification')->orderBy('created_at','desc')->first();
loads the latest AlertFrequency along with it's notification.
See One to Many relationship and Eager loading in documentation.
to get laastest alert associated with a specific websie with url $website_url.
Notification::where('website_url', $website_url)->orderBy('created_at')->first();
hasMany relation :
public function alertFrequencies(){
return $this->hasMany('App\AlertFrequency','notification_id');
}
I have a Laravel project and I'm using Eloquent for models.
Let's say I have three related tables:
User
Demographics
Roles
I would like my User model to inherit both Demographics and Roles based on Foreign Key as a one-to-one relation.
I am just looking for some feedback on what might be the most efficient / elegant / safest / performant / cleanest way to accomplish this.
So you're just looking for basic relations and eager loading those relations.
Try this:
class User extends Model
{
public function demographic()
{
return $this->hasOne('App\Demographic');
}
public function role()
{
return $this->hasOne('App\Role');
}
}
class Demographic extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
class Role extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
Then to access the User with all relations:
$user = User::with(['demographic', 'role'])->find(1);
var_dump($user->demographic); // returns the related Demographic model
var_dump($user->role); // returns the related Role model
In the Laravel documentation, there is the following example for retrieving morphedByMany relations, which are many-to-many polymorphic relations.
Laravel Many to Many polymorphic relations documentation
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
/**
* Get all of the posts that are assigned this tag.
*/
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
/**
* Get all of the videos that are assigned this tag.
*/
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
How would I get a list of all morphed relations in one query / collection, for instance, posts and videos, and then if I later added photos (or anything), that too?
I use a trick here:
Create a Model Taggable for your connection table taggable and add a hasMany relation to the Tag model.
public function related()
{
return $this->hasMany(Taggable::class);
}
Within your Taggable model create a morphedTo relation.
public function taggables()
{
return $this->morphTo();
}
Now you can get all models which are using the tag by calling:
$tagged = Tag::with('related.taggables');
Did you think to use the "union" function of the collections to merge all the different collection in order to retrieve all what you need?
class Tag extends Model
{
[...]
/**
* Get all of.
*/
public function morphed()
{
return $this->video->union($this->posts)->all();
}
}
You should be able to add a relationship on your Tag class as such
public function taggable()
{
return $this->morphedTo();
}
That will use the taggable_id and taggable_type to get the relevant Model that it is tied to.
I have a Link model, which needs a field that refers to either the Page, Redirect or Gallery model. I would like to be able to do something line $link->obj and have that return either the Page, Redirect or Gallery object depending on which one was saved to it.
Polymorphic relations appear to be what I'm looking for, except that I can't seem to get this approach to work.
Current code
<?php
$item = Page::find (1);
$link = new Link ();
$link->linkable ()->save ($item);
$link->save ();
Models
<?php
class Link extends Eloquent
{
protected $table = 'link';
public function linkable ()
{
return $this->morphTo ();
}
}
class Page extends Eloquent
{
protected $table = 'page';
public function linkable ()
{
return $this->morphOne ('Link', 'linkable');
}
}
class Redirect extends Eloquent
{
protected $table = 'redirect';
public function linkable ()
{
return $this->morphOne ('Link', 'linkable');
}
}
class Gallery extends Eloquent
{
protected $table = 'gallery';
public function linkable ()
{
return $this->morphOne ('Link', 'linkable');
}
}
The link database table has linkable_id and linkable_type fields.
I suppose I must be misunderstanding the documentation, because this does not appear to work.
You're close. Assuming you have your database setup correctly, the only issue I see is you calling save() on the morphTo relationship.
The morphTo side of the relationship is the belongsTo side. The belongsTo side does not use the save() method, it uses the associate() method.
So, the code you're looking for should be something like:
$item = Page::find(1);
$link = new Link();
$link->linkable()->associate($item); // associate the belongsTo side
$link->save();
// and to show it worked:
$link->load('linkable');
$page = $link->linkable;
echo get_class($page); // prints "Page"
I am currently developing an application for an indy movie production company. The way I have the workflow right now, the user begins by creating a new movie object by entering the movie title and synopsis. From there the user can then add more details such as price, run-time, full-screen/wide-screen, etc. The movie basic (title, synopsis) are in one database table, and the details are in another. I have set up a one-to-one relationship between the two eloquent models. I have also set up a MovieController that allows me to very easily do CRUD operations on the movie basic model, and when I am displaying the movie object to the user, I can display both the basics and details.
What I was wondering was there some way to use the already existent functions in the movie controller to do CRUD operations on the movie details without having to create new functions in the controller? Also is it possible to reuse the views I've created for each corresponding CRUD operation? In other words can I would like
something.dev/cms/create
In one instance to match to creating a new movie (title, synopsis) and in another instance to match to creating the movie detail (price, run-time, full-screen/widescreen) etc. Is this possible? I have provide the code for the two models below:
Movie_basic.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Movie_basic extends Model {
protected $fillable = ['movie_title', 'movie_synopsis'];
protected $guarded = ['id'];
public function details()
{
return $this->hasOne('App\Movie_detail', 'movie_id');
}
public function personnel()
{
return $this->hasMany('App\Movie_personnel', 'movie_id');
}
}
Model_detail.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Movie_detail extends Model {
protected $fillable = ['minutes', 'languages', 'viewer_discretion', 'screen_type', 'price'];
protected $guarded = ['id', 'movie_id'];
public function basics()
{
return $this->belongsTo('App\Movie_basic');
}
}
If I understand you, this might be an answer. (Did not test the code.)
Please note that, that code has been written to show you an example. You will probably want to edit it to make it work and act as you wanted. Maybe you want to use a repository or automate the model instance creating (I did not create new instances), and saving processes. You can use interfaces instead of your models etc...
Here is the service to store the logic.
<?php
use Movie_basic; use Movie_detail;
Class MovieService {
protected $movieBasic;
protected $movieDetail;
public function __construct(Movie_basic $movieBasic, Movie_detail $movieDetail) {
$this->movieBasic = $movieBasic;
$this->movieDetail = $movieDetail;
}
public function createMovie(array $attr) {
// TODO: Move your business logic here.
// E.g
$movie = $this->movieBasic->fill($attr);
$movie->save();
return $movie;
}
public function createMovieDetail(array $movieAttr, array $attributes) {
// TODO: Move your detail logic here.
// E.g.
$basic = $this->createMovie($movieAttr);
$detail = $this->movieDetail->fill($attributes);
$detail->basic()->associate($detail);
$detail->save();
return $detail;
}
}
And here, the controller examples:
<?php
use MovieService;
class MovieController {
public function __construct(MovieService $ms) {
$this->ms = $ms;
}
public function store() {
$this->ms->createMovie($attrToSave);
}
}
<?php
use MovieService;
class MovieDetailController {
public function __construct(MovieService $ms) {
$this->ms = $ms;
}
public function store() {
$this->ms->createMovieDetail($attrToSave);
}
}