I have 3 tables.
1.posts
2.categories
3.category_post.
My Posts Tables here :
+------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| slug | varchar(255) | NO | | NULL | |
| reporter | varchar(255) | NO | | NULL | |
| meta | varchar(255) | NO | | 0 | |
| body | text | NO | | NULL | |
| image | varchar(255) | NO | | 0 | |
| top | tinyint(1) | NO | | 0 | |
| post_count | int(11) | NO | | 0 | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+------------------+------+-----+---------------------+----------------+
My Categories Table Here:
+---------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| category_slug | varchar(255) | NO | | NULL | |
| parrent_id | int(11) | NO | | 0 | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+---------------+------------------+------+-----+---------------------+----------------+
My category_post table:
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category_id | int(10) unsigned | NO | MUL | NULL | |
| post_id | int(10) unsigned | NO | MUL | NULL | |
+-------------+------------------+------+-----+---------+----------------+
I can create post with multiple category selected . But When i want to showing post by category, then i can't access category table . That means, i want to view category name with each post .
Here is my Models:
In Category Model:
public function posts()
{
return $this->belongsToMany('Post');
}
In Post Model:
public function categories()
{
return $this->belongsToMany('Category');
}
My Query Helper function is for Post by category:
public static function cat_post($category, $limit, $top)
{
$posts = Post::whereHas('categories', function($q) use ($category, $top)
{
$q->where('name', 'like', $category);
$q->where('top', 'like', $top);
})->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
return $posts;
}
I can view all post data . But category name not .
My Post Loops:
<?php $headline = Helper::head_post(10, 1); ?>
#foreach ($headline as $post)
<li>{{ $post->title }}</li>
#endforeach
when i try this for category name not working:
#foreach ($headline as $post)
<li>{{ $post->categories->name }}</li>
#endforeach
Please help me.
Because it's a many to many relation $post->categories->name will not work. It doesn't know which category (of the many) you want the name of.
So you could for example use $post->categories()->first()->name to get the name of the first category or loop over them to get the name of all categories.
Related
I'm working in Laravel and need help with database relations. I have three tables:
projects:
+------------+--------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(191) | NO | | NULL | |
| url | varchar(191) | YES | | NULL | |
| updated | bigint(20) unsigned | YES | | NULL | |
| type | enum('adobe','invision','pdf') | NO | | NULL | |
| preview_id | int(10) unsigned | YES | MUL | NULL | |
+------------+--------------------------------+------+-----+---------+----------------+
users:
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(150) | NO | UNI | NULL | |
| password | varchar(179) | NO | UNI | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
projects_users
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| project_id | int(10) unsigned | YES | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
+------------+------------------+------+-----+---------+----------------+
Situation: There are several projects, and multiple users can work on multiple projects (ManyToMany).
I need to select (SELECT-statement) all users working on project with e.g ID 1.
How would I do that in plain SQL, and how would I do that in Laravel code (without a raw-sql-query function).
I already looked here but I ain't really catching it.
Thanks!
Use the whereHas() method:
$users = User::whereHas('projects', function($q) use($projectId) {
$q->where('id', $projectId);
})->get();
class Project extends Model{
public function users()
{
return $this->belongsToMany('App\User', 'projects_users');
}
}
class User extends Model{
public function projects()
{
return $this->belongsToMany('App\Project', 'projects_users');
}
}
$project = Project::find(1);
$project->users //list of all the users
So i'm trying to get my head around using eloquent for many to many relationships in my application.
I have three tables as followed
user
+----------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| password | varchar(60) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| active | enum('yes','no') | NO | | NULL | |
| last_login | timestamp | NO | | 0000-00-00 00:00:00 | |
+----------------+------------------+------+-----+---------------------+----------------+
user_has_address
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| address_id | int(10) unsigned | YES | MUL | NULL | |
| users_id | int(10) unsigned | YES | MUL | NULL | |
+------------+------------------+------+-----+---------+-------+
address
+---------------+----------------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name_number | varchar(45) | NO | | NULL | |
| first_line | varchar(45) | NO | | NULL | |
| second_line | varchar(45) | NO | | NULL | |
| town_city | varchar(45) | NO | | NULL | |
| state_country | varchar(45) | NO | | NULL | |
| post_zip | varchar(45) | NO | | NULL | |
| type | enum('delivery','billing') | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| deleted_at | timestamp | YES | | NULL | |
+---------------+----------------------------+------+-----+---------------------+----------------+
in my user repository i have the following
namespace App\Libraries\Repositories\Core\Users;
use Schema;
use App\Models\Core\User;
use Bosnadev\Repositories\Eloquent\Repository;
use Symfony\Component\HttpKernel\Exception\HttpException;
class UserRepository extends Repository
{
public function getUsersAddresses()
{
return $this->userModel->hasManyThrough('App\Models\Bundle\Addresses\Address','App\Models\Bundle\Addresses\UserHasAdress','id','address_id');
}
}
Im returned an object that shows parent and related classes but im not actually returned my users address. Is there something im missing?
Alexrussell made some good points in his comment that you could possibly address, however I believe your immediate problem is a missing ->get() at the end of your return line.
Without it, you would be required to call your method like:
$repository->getUsersAddresses()->get();
As hasManyThrough will return an instance of Illuminate/Database/Eloquent/Relations/HasManyThrough not the actual results
For reference:
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html
Note in the example here: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
The example usage includes a get(): $roles = App\User::find(1)->roles()->orderBy('name')->get();
Hope this helps
I have three models:
Vehicles:
+--------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| registration | varchar(255) | NO | | NULL | |
| vin | varchar(255) | NO | | NULL | |
| titular_id | int(10) unsigned | NO | | NULL | |
| titular_type | varchar(255) | NO | | NULL | |
| renter_id | int(10) unsigned | NO | | NULL | |
| renter_type | varchar(255) | NO | | NULL | |
+--------------------+------------------+------+-----+---------------------+----------------+
SiretCompanies:
+------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| siret | varchar(255) | NO | | NULL | |
| siren_company_id | int(10) unsigned | NO | MUL | NULL | |
+------------------+------------------+------+-----+---------------------+----------------+
SirenCompany:
+---------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| siren | varchar(255) | NO | | NULL | |
+---------------------+------------------+------+-----+---------------------+----------------+
A vehicle can be related to the SirenCompany either through a titular, or a renter. What I want is to get all related vehicles for a SirenCompany.
In raw MySQL, this is my query:
select count(*) FROM `vehicles`
inner join `siret_companies`
on (
(`vehicles`.`titular_type` = 'SiretCompany' and `vehicles`.`titular_id` = `siret_companies`.`id`)
OR
(`vehicles`.`renter_type` = 'SiretCompany' and `vehicles`.`renter_id` = `siret_companies`.`id`)
)
inner join `siren_companies`
on `siren_companies`.`id` = `siret_companies`.`siren_company_id`
where `siren_companies`.`id` = 410
Now what I would like to do is run that as an Eloquent query, but I cannot seem to figure it out.
If I only consider the titular, I have this:
return Vehicle::join('siret_companies',function($join) {
$join
->where('vehicles.titular_type', '=', 'SiretCompany')
->where('vehicles.titular_id','=', 'siret_companies.id');
})
->join('siren_companies', function($join) {
$join->on('siren_companies.id', '=', 'siret_companies.siren_company_id');
})
->where('siren_companies.id','=',$this->id)
->count();
But I cannot seem to figure out how to write the join so that it corresponds to the above query.
This is what I've come up with
Vehicle::join('siret_companies', function ($q) {
$q->on('vehicles.titular_type', '=', 'SiretCompany');
$q->orOn('vehicles.titular_id','=', 'siret_companies.id');
})->join('siret_companies', function ($q) {
$q->on('siren_companies.id', '=', 'siret_companies.siren_company_id');
})->where('siren_companies.id','=', $id);
I have two tables. This one is called teams:
+------------+--------------+------+----------------------+----------+
| Column | Type | Null | Default | Comments |
+------------+--------------+------+----------------------+----------+
| id | int(10) | No | | |
| apikey | varchar(255) | Yes | NULL | |
| name | varchar(255) | Yes | NULL | |
| logo | varchar(255) | Yes | NULL | |
| url | varchar(255) | Yes | NULL | |
| hashtag | varchar(255) | Yes | NULL | |
| created_at | timestamp | No | 0000-00-00 00:00:00 | |
| updated_at | timestamp | No | 0000-00-00 00:00:00 | |
+------------+--------------+------+----------------------+----------+
The other one is called streamers:
+--------------+--------------+------+----------------------+----------+
| Column | Type | Null | Default | Comments |
+--------------+--------------+------+----------------------+----------+
| id | int(10) | No | | |
| apikey | varchar(255) | Yes | NULL | |
| name | varchar(255) | Yes | NULL | |
| team | int(11) | Yes | NULL | |
| type | varchar(255) | Yes | NULL | |
| twitch | varchar(255) | Yes | NULL | |
| mlg | int(11) | Yes | NULL | |
| url | varchar(255) | Yes | NULL | |
| twitter | varchar(255) | Yes | NULL | |
| status | int(11) | Yes | NULL | |
| viewers | int(11) | Yes | NULL | |
| created_at | timestamp | No | 0000-00-00 00:00:00 | |
| updated_at | timestamp | No | 0000-00-00 00:00:00 | |
| last_live | timestamp | No | 0000-00-00 00:00:00 | |
| last_edit_by | int(11) | Yes | NULL | |
+--------------+--------------+------+----------------------+----------+
As you can see, the streamers table has a team field which is only the team ID.
My Team.php model already has this:
public function streamers()
{
return $this->hasMany('Streamer', 'team');
}
Here's what I want to do
I want to search the streamers by team, HOWEVER the end user should only have to use the name not the ID. How do I make the table relationship so that I can query the streamers table for the team name instead of the ID? Is this possible at all?
I would've assumed this is exactly what relationship hasOne is for, but apparently I was wrong. Since I obviously don't have the streamer ID's in the team table because every streamer ID on a team would be awful. Laravel relationships seems kinda wrong here... Because hasOne would fit on streamers but it only works with teams, but essentially the streamers have a team and the team belongs to many streamers and that would mean belongsToMany would make sense.
Thanks
What you're looking for is the belongsTo relationship.
In streamers include:
public function team()
{
$this->belongsTo('Team','team');
}
You can then query streamers by team like so:
$streamer = Streamer::whereHas('team', function($q)
{
$q->where('name', 'like', $teamName);
})->get();
More details can be found in the docs: http://laravel.com/docs/eloquent#querying-relations
I'm using Laravel 4. I have many to many relationships in my system. And I choose to use Wordpress taxonomy table scheme.
But how can I make models relationships with Laravel 4 Eloquent ORM? Here is my database tables;
Table terms:
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | MUL | | |
| slug | varchar(200) | NO | UNI | | |
+------------+---------------------+------+-----+---------+----------------+
Table term_taxonomy:
+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| term_id | bigint(20) unsigned | NO | MUL | 0 | |
| taxonomy | varchar(32) | NO | MUL | | |
| description | longtext | NO | | NULL | |
| parent | bigint(20) unsigned | NO | | 0 | |
| count | bigint(20) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+----------------+
Table term_relationships:
+------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+-------+
| object_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_order | int(11) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+-------+
Normally we can do return $this->belongsToMany('Term'); but how can we do 2 relationships? We need 2 relationships first find term taxonomy from "term_taxonomy" table, after find term relations with "taxonomy_id".
And an example for how I want to use;
$categories = Post::find(1)->categories; // get terms with taxonomy="post_category"
$tags = Post::find(1)->tags; // get terms with taxonomy="post_tag"
I don't want to do this with basic database class "DB::table('table')->join('...')..." I want to use Eloquent relation methods and models.
You can create getter methods to handle these:
In your Post model, create new methods something along the lines of:
public function getCategories()
{
return $this->hasMany()->where('taxonomy', 'post_category');
}
public function getTags()
{
return $this->hasMany()->where('taxonomy', 'post_tag');
}