I'm a real newbie to Laravel but I'm loving it so far. I'm struggling on thing however, I want to retrieve the data for the user that is logged in and I am not sure how to go about this.
I have a few tables but I'll keep it basic for now, I have projects table and a users table, I've defined the relationships between these two in the models as so:
user.php
public function projects() {
return hasMany('project');
}
project.php
<?php
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Project extends Eloquent
{
public function user()
{
return belongsTo('user');
}
}
I know I can do the following to retrieve all projects in the database with a foreach loop, however this doesn't retrieve the logged in users projects:
$projects = DB::table('projects')->get();
I saw one tutorial which wasn't very in depth but he said to access the model query I would have to use the following command:
$project = User::all()->projects;
However this hasn't worked either. Can anyone point me into the right direction with real tutorials or post simple examples?
Thanks in advance
Those are the projects of your logged in user:
if (Auth::check())
{
$projects = Auth::user()->projects;
}
And this must be in your relation:
class User extends Eloquent
{
public function projects() {
return this->hasMany('Project');
}
}
You also need to add $this to your Project relation:
class Project extends Eloquent
{
public function user()
{
return $this->belongsTo('User');
}
}
Related
I have a little problem,
i can't see how can i make a three table relation on Laravel.
DELETED
If someone can help me, I take notes !
Thanks !
As far as I can see, you need to create a relationship to your pivot table and then, you can create relationships to both table.
class Contact extends Model
{
public function filmJobs()
{
return $this->hasMany(ContactFilm::class);
}
}
class ContactFilm extends Model
{
public function film()
{
return $this->hasOne(Film::class);
}
public function job()
{
return $this->hasOne(Job::class);
}
}
You can now use it like that: $contact->filmJobs[0]->film and $contact->filmJobs[0]->job.
Consider the following world model:
A User can be member of zero or more UserGroups
A UserGroup can have zero or more User members
A Repo can reside in zero or more RepoGroups
A RepoGroup can contain zero or more Repos
A UserGroup can be granted 'read' or 'write' access to a RepoGroup
The (simplified) database schema implementing this model looks like this:
Setting up the Eloquent many-to-many relationship between User and UserGroup via the user_group_pivot table is straight forward, as is the equivalent relationship between Repo and RepoGroup. The access_rights table is a pivot table with additional access information, but the relationship between ClientGroup and RepoGroup is also many-to-many, and equally straight forward. Now the following challenges remain:
The relationship between UserGroup and Repo
The relationship between RepoGroup and User
The relationship between User and Repo
This is the code I've got so far:
class User extends Model {
public function userGroups() {
return $this->belongsToMany(UserGroup::class, 'user_group_pivot');
}
public function repos() {
# Eloquent wizard can makez magic here?
}
public function repoGroups() {
# Eloquent wizard can makez magic here?
}
}
class UserGroup extends Model {
public function users() {
return $this->belongsToMany(User::class, 'user_group_pivot');
}
public function repos() {
# Eloquent wizard can makez magic here?
}
public function repoGroups() {
return $this->belongsToMany(RepoGroup::class, 'access_rights')
->withPivot(['access']);
}
}
class RepoGroup extends Model {
public function repos() {
return $this->belongsToMany(Repo::class, 'repo_group_pivot');
}
public function users() {
# Eloquent wizard can makez magic here?
}
public function userGroups() {
return $this->belongsToMany(UserGroup::class, 'access_rights')
->withPivot(['access']);
}
}
class Repo extends Model {
public function repoGroups() {
return $this->belongsToMany(RepoGroup::class, 'repo_group_pivot');
}
public function repos() {
# Eloquent wizard can makez magic here?
}
public function repoGroups() {
# Eloquent wizard can makez magic here?
}
}
I've scoured the web looking for examples similar to this, but they either blatantly plagiarize the Laravel docs or are equally trivial. I've been playing around with the Model::hasManyThrough() relationship, but to no avail. Hence I put my hopes in the Eloquent wizards of the world. I can haz helps, pliis?
There is a possibility to go to repos without touching other relations.
You can add code like this below to User::repos
$repos = [];
foreach($user->userGroups() as $userGroup){
foreach($userGroup->repoGroups() as $repoGroup){
foreach($repoGroup->repos() as $repo){
$repos[] = $repo;
}
}
If you are worried about performence (example during processing a lot of users) you can use eager loading during fetch users with repos from DB
$users = App\User::with(['userGroups.repoGroups.repos'])->get();
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
I have a pivot table of users_operators.
I want to grab the operator_id of the user.
This is how i do this now, but its seems like verbose way.
if (Auth::user()->type === 'operator') {
$user = Auth::user();
// There is a better way to do this?
$operator_id = $user->operator[0]['pivot']['operator_id'];
Session::put('operatorId', $operator_id);
}
class Operator extends \Eloquent
{
public function users() {
return $this->belongsToMany('User');
}
}
class User extends \Eloquent
{
public function operator() {
return $this->belongsToMany('Operator');
}
}
I'm, battling insomnia and not functioning at 100%, but you should be able to get away with $user->operator->id based on what I'm interpreting your models to be (it looks like you had a typo when you copied them into the question).
If that doesn't work, you might want to check out the "Dynamic Properties" section in the Eloquent docs for more info, if you haven't already.
I was creating a like system for my website. in this I wanted one user can only like one time for a post. and a post can be liked by many user. Also many user can like many post.
So if I guess it right, It is a many to many reletionship.
in this context,
I create the following table
... users table:
id
name
....
posts table :
id
post
...post_likes table
id
user id
poost_id
Now I am having the following model for
user :
class User extends SentryUserModel {
public function post_likes()
{
return $this->has_many('Post_like', 'id');
}
}
post :
class Post extends Eloquent {
public function post_likes()
{
return $this->has_many('Post_like', 'id');
}
}
post_like :
class Post_like extends Eloquent {
public function posts()
{
return $this->belongs_to('Post', 'post_id');
}
public function users()
{
return $this->belongs_to('User', 'user_id');
}
}
now when I am going to insert into the database (for post_likes table) I am getting an error called
Illuminate \ Database \ Eloquent \ MassAssignmentException
user_id
Also I want to know is there any way to inset into database like
$user->like()->save($user); ?????
Thank you in advance. Happy coding . \m/
I'll start with a basic issue, firstly you might want to make sure all your tables are lower case (still as a snake case as well), it's not required but it's ultimately how it's expected to be with Laravel so it makes life easier to keep with that. Also a note to the wise, like Class names, database tables are typically in the singular so user instead of users
Secondly yes you can do an insert with $user->post_likes()->save($debate); as your post_likes method on the user class returns has_many.
Thirdly, your design of the Post_like class is a bit off, you could be better off make it like so:
class PostLike extends Eloquent { // note that PostLikes is a more standard naming for a class, they should ideally be camel case names but with all capitals for words
protected $table = 'post_like'; // specifies the table the model uses
public function post() // this should be singular, the naming of a belngs_to method is important as Laravel will do some of the work for you if let it
{
return $this->belongs_to('Post'); // by naming the method 'post' you no longer need to specify the id, Laravel will automatically know from the method name and just adding '_id' to it.
}
public function users()
{
return $this->belongs_to('User');
}
}
Fourthly, your other classes could be better as:
class Post extends Eloquent {
public function post_likes()
{
return $this->has_many('PostLike'); // doesn't require you to specify an id at all
}
}
I can't exactly tell you why you're getting that mass assign error, your post is a bit garbled and doesn't look like you've included the code that actually causes the exception? I have a feeling though is that you're trying to do an insert for multiple database rows at one time but haven't defined a fillable array for PostLike such as with here: http://four.laravel.com/docs/eloquent#mass-assignment
According to Laravel 5:
User Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
public function post_likes()
{
return $this->hasMany('App\PostLike');
}
}
Post Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
public function post_likes()
{
return $this->hasMany('App\PostLike');
}
}
PostLike Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class PostLike extends Model {
protected $table = 'post_like';
public function posts()
{
return $this->belongsTo('App\Post');
}
public function users()
{
return $this->belongsTo('App\User');
}
}
and if you want to save the post_like data then:
$inputs['post_id'] = 1;
$inputs['user_id'] = 4;
$post_like = PostLike::create($inputs);
Hope this helps!!