Laravel database error - php

Currently I'm making a ticket system with laravel.
But I'm not quite familiar with the database structure.
I've made two tables with migrations.
Called:
Tickets,
Comments
Obvisously a ticket can have multiple comments but a comment can only have one ticket. So it's a one to many relationship.
In the model of Tickets I've declared this:
class tickets extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
public function comments()
{
return $this->hasMany('App\comments', 'post_id');
}
}
In the model of Comments I've declared this:
class comments extends Model
{
protected $guarded = ['id'];
public function ticket()
{
return $this->belongsTo('App\tickets');
}
}
And this is my controller:
public function show($slug)
{
$ticket = tickets::whereSlug($slug)->firstOrFail();
$comments = $ticket->comments()->get;
return view('tickets.show',compact('ticket','comments'));
}
When exec this I receive the error:
ErrorException in TicketsController.php line 77: Undefined property:
Illuminate\Database\Eloquent\Relations\HasMany::$get
What am I doing wrong?
Thanks.

It seems you're trying to access the get property when i think your purpose is to call the get() method. Replace this:
$comments = $ticket->comments()->get;
with
$comments = $ticket->comments()->get();

Related

Scope and Relation doesn't work together - Laravel

User Model
class User extends Authenticatable
{
use HasApiTokens,HasFactory, Notifiable;
protected $table = "users";
protected $primaryKey = 'ID';
public function scopeActive($query)
{
return $query->where('activated', 1);
}
public function followings()
{
return $this->hasMany('App\Models\Contacts','user_id','ID')->active();
}
Contacts Model
class Contacts extends Model
{
use HasFactory;
protected $table = "contacts";
protected $primaryKey = 'ID';
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'ID');
}
When I call User::find(1)->followings()->count() on Tinker I get
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::active()'
How can I get followings whose activated = 1 only?
I fixed it by adding a custom scope to Contacts model
public function scopeActiveFollowings($query)
{
return $query->join('users', function($join)
{
$join->on('users.ID', '=', 'contacts.contact_id')
->where('activated', '=', 1);
});
}
Laravel model has a property called query.
This property is used automatically when you call a query for the model
these two statements are exactly the same:
User::query()->where('id',1)->get();
User::where('id',1)->get();
When you call query() directly or indirectly, Laravel register the scopes in this line
when you use $this->hasMany('App\Models\Contacts','user_id','ID') the result will be from class HasMany and this class doesn't have a method called active(), this is why you see this error.
to fix it, you have to apply the scope when you use User model query:
$result=User::query()->activated()->with('followings')->findOrFail(1);
if you want the followings count for the activated user using scope, you should apply this scope on Following Model:
public function scopeHasActivedUser($query)
{
return $query->whereHas('user', function($query)
{
$query->where('users.activated', 1);
});
}
In the controller:
$fllowingsCountForActivatedUser=Fllowing::query()->hasActivedUser()->count();
If I were you, I would get the result this way:
$user=User::withCount(['followings'=>function($query){
$query->where('users.activated', 1)})])->find(1);
now the $user model instance should have a property called 'followings_count' for the only activated followings count

Laravel 5.2 three-way pivot with custom pivot model

I have the following problem:
I have three tables:
contacts (people)
departments
contact_types (e.g. IT-Contact)
All of them are many-to-many types; One person can be a responsible for 0-n departments as 0-n Contact_types (even for the same department as multiple types). And so on.
In addition i have to have a history all over the project, so each of the tables stores "valid_start" and "valid_end" timestamps as well.
Therefore i now have this relation table:
contact_contact_type_department
id
contact_id
contact_type_id
department_id
valid_start
valid_end
What i ended up doing is creating a Model for the intermediate table:
class DepartmentResponsible extends Model {
protected $table = 'contact_contact_type_department';
protected $fillable = [...];
protected $dates = [
'valid_start',
'valid_end',
];
protected static function boot(){
parent::boot();
static::addGlobalScope(new ValidScope());
}
public function contact() {
return $this->belongsTo('cap\Contact');
}
public function department() {
return $this->belongsTo('cap\Department');
}
public function type() {
return $this->belongsTo('cap\ContactType');
}
}
Contact Model:
class Contact extends CustomModel{
protected $dates = [...];
protected $fillable = [...];
protected static function boot(){
parent::boot();
static::addGlobalScope(new ValidScope());
}
public function departmentResponsibles() {
return $this->hasMany('cap\DepartmentResponsible');
}
}
ContactType Model:
class ContactType extends CustomModel {
protected $dates = [...];
protected $fillable = [...];
protected static function boot() {
parent::boot();
static::addGlobalScope(new ValidScope());
}
public function responsible() {
return $this->hasMany('cap\DepartmentResponsible');
}
}
Department Model:
class Department extends CustomModel {
protected $fillable = [...];
protected $dates = [...];
protected static function boot(){
parent::boot();
static::addGlobalScope(new ValidScope());
}
public function responsibles(){
return $this->hasMany('cap\DepartmentResponsible');
}
//other methods down here, which have no immpact on this issue
}
I can now do things like
Department::first()->responsibles
Regarding the issue with the timestamps on the pivot table i assume i will have to make it a custom pivot table again (already had to do that once, in another case, where i had a "regular" 2-way pivot table)
So my 2 Questions now are:
1. Is this even the right way to do it? I mean the whole thing with the intermediate model and so on. I tried other ways as well, but I couldn't get anything like department->attach(contact) to work since i always need the third id as well...
2. How can i get something like Department::first()->contacts to work? (In a way, where i can access the intermediate "responsibles (=contact_contact_type_department)" table and filter based on the validity dates;eg. with a scope or with wherepivot functions)
well i finally went with the approach that i have an intermediate model called responsible. So for example if i want to print all the contacts and their contact_types for a department then i can do something like this:
$department = Department::first();
<ul>
foreach($department->responsible as $responsible){
<li>{{$responsible->contact->name}} as {{$responsible->type->name}}</li>
}
</ul>

Obtaining a list of non-linked models, in many_to_many relations, using Laravel Eloquent

I have a User-Roles model, using Laravel 4 where a user can have many roles, using Eloquent. I can access all roles linked to a user easily using this code :
class User extends Model {
protected $table = 'user';
protected $fillable = array('name');
public function rolesLinked() {
return $this->hasMany('App\UserRoleLink', 'user_id');
}
}
I've been trying to obtain the roles that are not linked to a user, to display on the specific user's page in a select box. Using this function, included in the User class.
public function rolesNotLinked() {
$user = this
$roles = Roles::whereDoesntHave('App\UserRoleLink',function($query) use ($user){
$query->where('user_id',$user->id);
});
}
The problem is, calling this function gives me the following error.
Call to undefined method Illuminate\Database\Query\Builder::App\UserRoleLink()
I've tried using has with < 1 to see if the function was problematic, but after reading this and the online source code, the function call pretty much does what I've tried.
Is something wrong in my function call, or have I messed up configurations somewhere?
For reference, here are my other Model classes:
class UserRoleLink extends Model{
protected $table = 'user_role_link';
protected $fillable = array('role_id','user_id);
public function role() {
return $this->hasOne('App\Role', 'role_id');
}
}
class Role extends Model{
protected $table = 'role';
protected $fillable = array('name');
}
EDIT: I've found out that I messed up by fillables when I copy-pasted. It didn't fix the issue, but I guess that's one step closer.
To use whereDoesntHave method, you must add the relation in your Role Model.
class Role extends Model{
protected $table = 'role';
protected $fillable = array('name');
public function UserRoles() {
return $this->hasMany('App\UserRoleLink', 'id');
}
}
Also, the whereDoesntHave method first parameter is not thte model but the function of the relation:
public function rolesNotLinked() {
$user = this
$roles = Roles::whereDoesntHave('UserRoles',function($query) use ($user){
$query->where('user_id',$user->id);
});
}

Ordering of nested relation by attribute with laravel

Hi there i'm trying to sort a collection by attribute of the relation.
This is my model
class Song extends \Eloquent {
protected $fillable = ['title', 'year'];
public function artist(){
return $this->hasOne('Artist','id', 'artist_id');
}
}
class SongDance extends \Eloquent {
protected $table = 'song_dances';
protected $fillable = ['rating'];
public function dance(){
return $this->belongsTo('Dance', 'dance_id');
}
public function song(){
return $this->belongsTo('Song', 'song_id');
}
}
class Dance extends \Eloquent {
protected $fillable = ['name'];
public function song_dances(){
return $this->hasMany('SongDance','dance_id','id');
}
public function songs(){
return $this->belongsToMany('Song', 'song_dances', 'dance_id', 'song_id');
}
}
this is how far i'm by now:
$dance = Dance::find(1);
$songs = $dance->songs()
->with('artist')->whereHas('artist', function ($query) {
$query->where('urlName','LIKE','%robbie%');})
->where('song_dances.rating', '=', $rating)
->orderBy('songs.title','asc')
->where('songs.year', '=', 2012)
->get();
Yeah i just could add a ->sortBy('artist.name'); to the query, but he result-collection can be quite big (about 6000 items) therefore i would prefer a databased sorting.
is there a possibility to do this?
Since Eloquent's relations are all queried separately (not with JOINs), there's no way to achieve what you want in the database layer. The only thing you can do is sort the collection in your app code, which you've already dismissed.
If you feel you must sort it in the database then you should write your own join queries instead of relying on Eloquent's relations.

Laravel eloquent error with "hasmany" relationship

I have this annoying problem when trying to execute a hasMany method:
I have a Task model, of which I want to retrieve all the states
class Task extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function states(){
return $this->hasMany('TaskState','task_id');
}
...from this TaskState model (db has columns: task_id, user_id, status)
class TaskState extends Eloquent {
public $table= "taskstates";
protected $guarded = array();
public static $rules = array();
public function task(){
return $this->belongsToMany('Task','task_id');
}
However when I try to execute this,
$task = Task::find($id);
return $task->states;
it keeps giving this error:
Call to undefined method TaskState::newQuery()
I'm stuck on this for like two days. Would anyone know what the problem might be here?
Thanks in advance!
If there is one-to-many relationship between models, then you should use 'belongsTo' in TaskState instead of 'belongsToMany;

Categories