So let's assume we have two Models based on our tables (Post and Tag)
The Post has a (one to many) relationship to have a so called Highlighted Tag field which only accepts one tag.
The Post has also a (many to many) relationship to have tags assigned to that post, So it's just like normal tags.
As far as I understand, You can not have more than one relationship assigned to the same Table, How is this possible using Laravel? What is the best practice?
If I understand you correctly you want that a post has a main(highlighted tag) tag and multiple normal tags. This is pretty easy.
Your Post model functions:
public function tag()
{
//Your highlighted tag
return $this->belongsTo(Tag::class);
}
public function tags()
{
//All normal tags
return $this->hasMany(Tag::class);
}
And these are the columns of your tables:
posts table:
id: int
title: string
content: string
tag_id: int
tags table:
id: int
name: string
post_id: int
You can do following :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function tags(){
return $this->hasMany('App\Tag');
}
public function highlightedtag(){
return $this->tags->where('tag_type', 'highlighted')->first();
}
}
?>
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function posts(){
return $this->hasMany('App\Post');
}
}
?>
Related
How can I get all the records from the relationship? I mean not only the related records, but also the rest.
Let's say I have a post which belongs to some category. If I want to change the category of this post I need a list of all available categories. Can I get this list from the relationship?
The Post model:
class Post extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
The Category model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->hasMany('App\Post');
}
}
In the PostsController I tried:
$postModel->category->find('all'); // Returns null
$postModel->category->all(); // Returns only the related categories
I know I can simply use the Category model in the PostsController, but I prefer to do it using the relationship.
If you feel you must use the relationship to get to the other model you could try:
$categories = $post->category()->getRelated()->get();
I am very new to polymorphic relationships and am struggling on my first task. My real life scenario is complicated, so for the purpose of this question, I have simplified it a little.
I have a range of products. Each of these products can be 'tagged' to one or more "Categories", "Brand" and "Consumer". For example:
I figured with this setup, I would need a table for my polymorphic relationships as below:
I have created a new Taggable class which contains the following
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Taggable extends Model
{
public function taggable()
{
return $this->morphTo();
}
}
...and added a method to my Product class:
public function taggedCategories()
{
return $this->morphMany(Taggable::class, 'taggable');
}
Finally, in my ProductController, I am trying to retrieve all products with their relationships as such:
$products = Product::with('taggedCategories')
Whilst this isn't producing an error, there are no categories returned in my results. Looking at the SQL output in LaravelDebugBar, I see the following SQL.
select * from `taggables` where `taggables`.`taggable_id` in (1) and `taggables`.`taggable_type` = 'App\Models\Product'
This clearly is not right, but I cannot for love nor money figure out where I have gone wrong. I feel I am close, but not quite there.
Can anyone explain what is wrong? Also, would I need to do something different for getting the "Brand" as this is a one-to-many relationship, not many-to-many?
Thanks
Your model structure is going to be like:
class Categories extends Model
{
public function products()
{
return $this->morphToMany('App\Tag', 'productable');
}
}
class Brand extends Model
{
public function products()
{
return $this->morphToMany('App\Tag', 'productable');
}
}
// and Consumers, ....
User Model:
class Product extends Model
{
public function categories()
{
return $this->morphedByMany('App\Categories', 'productable');
}
public function brands()
{
return $this->morphedByMany('App\Brunds', 'productable');
}
}
Database schema:
categories
id - integer
...
brands
id - integer
...
consumer
id - integer
...
productable
product_id - integer
productable_id - integer
productable_type - string
Now, you can retrieve the relations:
$categories = App\Categories::find(1);
// retrieve product of a type
foreach ($categories->products as $product) {
//
}
$product = App\Product::find(1);
// retrieve categories of a product
foreach ($product->categories as $categories) {
//
}
Actually, your type product (categories, brands, consumers) are productable.
My current blog like app has a Page that could have many Paragraphs of a different structure (Text, Image, File download, Registration form, ... ). When trying to translate this to Eloquent Models with relations, I think this is an easy way to do so:
Table pages:
id
title, ...
Table paragraphs:
id
page_id
paragraphable_id
paragraphable_type
Table paragraph_texts:
id
text
Table paragraph_images:
id
image_path
Table paragraph_downloads:
id
file_path
And the Models:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
public function paragraphs() {
return $this->hasMany(Paragraph::class);
}
}
?>
The Paragraph Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Paragraph extends Model
{
public $table = 'paragraphs';
public $timestamps = [];
public function page() {
return $this->belongsTo(Page::class);
}
public function paragraphable() {
return $this->morphTo();
}
}
The Text Type model as sample:
<?php
namespace App\Models\ParagraphTypes;
use Illuminate\Database\Eloquent\Model;
class Text extends Model
{
protected $table = 'paragraph_texts';
public function paragraph() {
return $this->morphOne(Paragraph::class, 'paragraphable');
}
}
I now want to view the page in nova and create a new paragraph by a selectable type. I created the Resource classes (for page, for paragraph and for each paragraph type) according to the eloquent relations (HasMany field for the page, MorphTo for the paragraph and just a TextArea for the Text type). When I see the details of a page and want to add a new paragraph, I can see the form to add a paragraph and can select the paragraph type in a dropdown but can only see the already existing entries, not new one. I will never want to add an existing paragraph type like this.
So questions:
Is the transformation of the structure into eloquent correct, are there any improvements?
How could laravel nova handle the creation of a new paragraph type for a page "through" the paragraph model? I feel like having to add a custom field to do so, that maybe asks for the type to create first...
Regarding your first question: You should use the relationships from the documentation:
class Page extends Model
{
public function texts() {
return $this->morphedByMany(Text::class, 'paragraphable', 'paragraphs');
}
}
class Text extends Model
{
public function pages() {
return $this->morphToMany(Page::class, 'paragraphable', 'paragraphs');
}
}
You can get multiple paragraph types with an accessor:
class Page extends Model
{
public function getParagraphsAttribute() {
return $this->texts->toBase()->merge($this->files)->merge([...]);
}
}
$paragraphs = $page->paragraphs;
I have created both employes and employes_detail tabel with the data
i have created model for both of the table that is given below:
emloye model:
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Http\Model\EmployeDetail;
class Employe extends Model
{
public function employes_detail()
{
return $this->hasOne(EmployeDetail::class);
}
}
and eployedetail model:
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class EmployeDetail extends Model
{
public function employe()
{
public function employe()
{
return $this->belongsTo(Employe::class);
}
}
}
and in controller i used like :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Model\Employe;
use App\Http\Model\EmployeDetail;
class EmployeController extends Controller
{
public function index(Request $request)
{
$Employe=Employe::all();
$convert=$Employe->toArray();
echo "<pre>";print_r($convert);exit;
//return view('employe.employe');
}
}
it showing only employe table data how can i show the data for the
employes_detail as well as .still i am not able to understand it on
laravel documentation can anyone please help me related this.
how can i get the all data from employes and employes_details table for all the records
but when i used this code in controller:
public function index(Request $request)
{
$Employe=Employe::where('id',1)->first();
//$convert=$Employe->toArray();
echo "<pre>";print_r($Employe->employes_detail);exit;
//return view('employe.employe');
}
its shows me the employe_detail table data
but i want both of the table data in a same array and i dont want to use where condition here.
the function employes_detail and employe in your models only declares the relationships between the models but if you want to load the relationship, you can try this :
Employe::with('employes_detail')->get();
or
$employees = Employe::all(); $employees->load('employes_detail');
Then you can access for each employees the relation attribute like that :
foreach($employees as $employe) {
$employe->employes_detail->id;
}
Hopes it helps you.
I'm not a professional programmer, so I don't know that I'm describing this very well.
Eloquent relationships are established in the model, using syntax and functions such as ... - >belongsTo.. etc.
Behind these models, are tables in my database.
In my (laravel) application, I have a logged in user who needs certain information about other users. At the end of the day, they're all just users, persisting in the user's table.
So when I use a relationship to another object, (e.g. car) all is good. When I try use a relationship to another user I get errors like Cannot redeclare class App\Models\User.
I think I'm misunderstanding something here.
I get the feeling maybe I should be 'instantiating' another version of my User (as 'manager') ... But do I really need to? It's more of a lookup than anything else. I'm not sure I would even know how to do that.
Some pointers please?
It sounds like you created two distinct "User" models:
// /app/User.php:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function user() {
return $this->hasOne('App\Models\User');
}
}
// /app/models/User.php:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function user() {
return $this->belongsTo('App\User');
}
}
Instead you want to have a single class which belongs to itself:
// /app/User.php:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function parent() {
return $this->belongsTo('App\User');
}
public function children() {
return $this->hasMany('App\User');
}
}
Then in your database make sure that the users table has a user_id property (edit database/migrations/2014_10_12_000000_create_users_table.php):
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
Now you can attach users to one another:
<?php
$manager = new User();
$employeeOne = new User();
$employeeTwo = new User();
$manager->children()->saveMany([
$employeeOne,
$employeeTwo
]);
dd( $employeeTwo->parent->name ); // Manager's name