I am using laravel 5.2 and I am making a web site which requires to have locations on different category. I want to store model name in the database and fetch using the value retrieved from the database.
I have a hotel model
-id
-name
And I have restaurant model
-id
-name
And i have location model
-id
-name
-latitude
-longitude
-modal_name
I want to store App\Hotel in modal_name column and want to fetch data from the same model.
How do I go about solving this problem?
The only way I know is to use polymorphic relation.
You should add to your locations table 2 fields ( 1 for the model name and 1 for the id key on that model ) and the naming convention is usually ****able_type and ***able_id
for instance: locationable_type and locationable_id.
than on your location model add the following relation
class Location extends Model
{
/**
* Get all of the owning locationable models.
*/
public function locationable()
{
return $this->morphTo();
}
}
than on your hotel model add this relation
class Hotel extends Model
{
/**
* Get the hotel location.
*/
public function location()
{
return $this->morphMany('App\Location', 'locationable');
}
}
same thing for your restaurant model
class Restaurant extends Model
{
/**
* Get the restaurant location.
*/
public function location()
{
return $this->morphMany('App\Location', 'locationable');
}
}
than to use it you simply can use:
$hotel = App\Hotel::find(1);
dd($hotel->location);
for more information you can checkout laravel documentation
Good luck :)
Related
I am trying to make many to many relationships b/w three table I don't know how to make that please confirm me my relationship correct or not.
Project table
id | name
user table
id | name
project_assign
id | user_id | project_id
User Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','user_id','id');
}
project_assign Model
public function user()
{
return $this->belongsToMany('App\User','Project_assign','user_id','id');
}
Project Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','project_id','id');
}
you don't need to make a relation in project_assign model. only Project and User will have relation,
Project Model
public function users(){
return $this->belongsToMany('App\User','project_assign','project_id','user_id');
}
User Model
public function projects(){
return $this->belongsToMany('App\Project','project_assign','user_id','project_id');
}
In controller you can get like this
User::with('projects')->get();
Project::with('users')->get();
The idea is that I have relation between two table's section and user
the section_id exist in users table it relation with id section
SO for ex : section_id = 2 > want to bring all users belongs to id section
My code here use id from url and I don't want to do this I want without id bring all users :
public function getAllUsers($id)
{
$data = User::where('section_id',$id)->get();
}
If I'm understanding you correctly, you want to get all the users for a particular Section that have the relationship to that section.
Assuming you have the relationship set up correctly on the Section model:
public function users() {
return $this->hasMany('App\User');
}
I suggest you go about this 'backward' and just pull the users from the Section model itself (eager loading allows one query):
$section = Section::with('users')->first();
$usersForThisSection = $section->users;
If you setup a One to Many relationship you can say things like: $user->section to return the section a user belongs to...and $section->users to get a collection of all the users in a section -- the getAllUsers() is redundant in this case.
Make sure your User and Section models contain these methods. Sounds like your migration was setup properly if you have a section_id column on your users table.
More info on one-to-many here: https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
// app/User.php
class User extends Model
{
public function section() {
return $this->belongsTo('App\Section');
}
}
// app/Section.php
class Section extends Model
{
public function users() {
return $this->hasMany('App\User');
}
}
Test out your relationships in php artisan tinker like so...
$user = User::find(1);
$user->section
$section = Section::find(1);
$section->users
you need a unique identifier to receive section_id's users. receive it from url(get) or ajax(post) . if you worry about users access then use "auth"
In restaurant table, foreign key do not get value of user table. I make relation one to many in user and restaurant tables. user can have many restaurants.
class Restaurant extends Model
{
protected $guarded=['user_id'];
protected $table ="rest_info";
public function menus() {
return $this->hasMany('App\Menu');
}
public function dishes(){
return $this->morphMany('App\Dish','dishable');
}
public function user(){
return $this->belongsTo('App\User','id','user_id');
}
}
If you wish to access these relationship with in your controller function you can use with keyword of laravel you have to something like this:-
If you wish to get the menus you can use something like this in your contoller Function
$getResturantdata = Resturant::with('menus')->get();
dd($getResturantdata);
If you wish to get the menus and users both you can use something like this in your contoller Function:-
$getResturantdata = Resturant::with('menus','users')->get();
dd($getResturantdata);
I can't think of how to handle this in Eloquent. I have a many-to-many relationship that needs a one-to-one relationship assigned to it.
Here is the basic database structure I designed in its simplest form:
ACCOUNTS: id
AGENTS: id
FEES: id
ACCOUNT_AGENT: account_id, agent_id, fee_id
Each Account belongsToMany Agents.
Each Agent belongsToMany Accounts.
Each "Account_Agent" (the many-to-many pivot table) belongsTo Fee.
How do I define that third relationship in an Eloquent model?
I want to be able to access the "account_agent" fee like this (or similar):
$account = Account::first();
foreach ($account->agents as $agent) {
echo $agent->fee;
}
Thanks, hopefully my question is clear.
See here:
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
Scroll down to Defining Custom Intermediate Table Models
Basically what you have to do is to define a class like this:
class AccountAgent extends Illuminate\Database\Eloquent\Relations\Pivot
{
/**
* Many:1 relationship with Fee model
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function fee()
{
return $this->belongsTo(Fee::class);
}
}
Then you define your many:many relationship in your Account class like this:
/**
* The agents that belong to the account.
*/
public function agents()
{
return $this->belongsToMany(Agent::class)->using(AccountAgent::class);
}
Then in your code, instead of this:
foreach ($account->agents as $agent) {
echo $agent->fee;
}
... do this:
foreach ($account->agents as $agent) {
/** #var Fee $fee_object */
$fee_object = $agent->pivot->fee;
}
In that loop $fee_object is of class Fee (the model class that covers the fees table) so you could echo $fee_object->fee_amount or whatever other columns you need to use.
I have a posts and a users table.
In the posts table, i have a posts_author which is the ID of the user from the users table.
Is there an easy way to display the email address of the user, which is in the users table?
Cheers,
As long as you've set your relationships up it should just be a simple query.
http://laravel.com/docs/eloquent#relationships
Look at the one to many relationships.
(1 User, Multiple posts)
Remember to set the inverse of the relationship up also
If your model has the right relationships then should be as simple as $post->author->email().
You must tweak the author relationship because Eloquent assumes the key will be named author_id.
// Post
public function author() {
return $this->belongsTo('Author', 'posts_author');
}
// Author
public function posts() {
return $this->hasMany('Post');
}
Remember to use eager loading in case you are retrieving emails from more than one post object, or you will end up with n+1 queries.
Providing that you've configured the relationships properly, it should be pretty easy.
Post Model:
class Post extends Eloquent
{
protected $table = 'posts';
public function author()
{
return $this->belongsTo('User', 'posts_author');
}
}
Then User Model:
class User extends Eloquent
{
protected $table = 'users';
public function posts()
{
return $this->hasMany('Post', 'posts_author');
}
}
Then when loading the post you can do the following.
$post = Post::with('author')->find($id);
This will tell Eloquent to join on the users table and load the user data at the same time. Now you can just access all of the user information like this:
$post->author->username;
$post->author->email;
$post->author->id;
// etc etc
Obviously this is just a skeleton, but the assumption is that you have the rest setup.