Laravel whereDoesntHave union whereHas - php

I'm trying to query my models that don't have relation and union it with the ones that have relations but I get an error with memory being exhausted.
Post::whereDoesntHave('comments')
->union(Post::newQuery()->whereHas('comments')->getQuery())
->paginate();`
Anyone know a better way of using the union?

Update Your Post Model with below code.
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class); //Pass Foreign Key if other than id
}
}
Update Your Comment Model with this code.
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);//Pass Foreign Key if other than id
}
public function user()
{
return $this->belongsTo(User::class);//Pass Foreign Key if other than id
}
public function scopeNoComment($query)
{
return $query->count() == 0;
}
}
$comments = $post->NoComment()->with('user')->paginate(10);
Making a relationship would be easy to access data.

Related

Laravel: unable to retrieve one to many camelCase method

I am trying to retrieve the inverse side of the one to many relationship where the method is camelCase. i.e
Owning Class: OneToMany
class Brand extends Model
{
public function products()
{
return $this->hasMany(Product::class);
}
}
Owned Class
class Product extends Model
{
public function MyBrand()
{
return $this->belongsTo(Brand::class);
}
}
retrieve the inverse related model like this:
$product = Product::find(1);
$brand = $product->my_brand;
dd($brand->name);
Error, Trying to get property 'name' of non-object
I also tried this:
$brand = $product->myBrand;
it did not work.
however, if i make my method like below it works:
public function brand()
{
return $this->belongsTo(Brand::class);
}
question is : how to make it work when the method is in CameCase ?
Try this,
public function my_brand()
{
return $this->belongsTo(Brand::class);
}
And call this relation as,
$product = Product::find(1);
$brand = $product->my_brand;
dd($brand->name);
You need to change, method name in brand(), because Eloquent will automatically determine the proper foreign key column in the Brands table.
https://laravel.com/docs/7.x/eloquent-relationships#one-to-many
If you want to keep it MyBrand() you need to specify foreign key:
public function MyBrand()
{
return $this->belongsTo(Brand::class,'product_id');
}
Specify it's foreign key in relationship
class Product extends Model
{
public function MyBrand()
{
return $this->belongsTo(Brand::class,'brand_id');
//brand_id is foreign key in your product table
}
}
$product = Product::find(1)->with('MyBrand'); //But it will be good to eager load it
$product->MyBrand->name; //It will definitely return name now

Laravel Relationship tables passing extra "s" with id name

I am getting "'lessons.subjects_id" while there is "subject_id" in lesson table. dont know where is problem in my relationship. My relationship models are as under:
class Lessons extends Model
{
public function subject()
{
return $this->belongsTo('Lea\Subjects');
}
public function category()
{
return $this->belongsTo('Lea\Category');
}
}
Subject Model is:
class Subjects extends Model
{
public function category()
{
return $this->belongsTo('Lea\Category');
}
public function Lessons()
{
return $this->hasMany('Lea\Lessons');
}
}
If you didn't respect the Laravel convention you have to teach him about yours ;) by adding you foreign key name.
In the documentation you have :
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass a second argument to the hasOne method:
class Lessons extends Model
{
public function subject()
{
// your foreign key
return $this->belongsTo('Lea\Subjects', 'subject_id');
}
public function category()
{
return $this->belongsTo('Lea\Category');
}
}
And
class Subjects extends Model
{
public function category()
{
return $this->belongsTo('Lea\Category');
}
public function Lessons()
{
// your foreign key
return $this->hasMany('Lea\Lessons', 'subject_id');
}
}

`Trying to get property of non-object` using `hasManyThrough` relationship laravel 5

I am trying to develop a blog using Laravel 5 in which i have to show comment along with user on post.
Here is my database table schema.
User
id
name
Posts
id
post_content
user_id
Comments
id
comment
user_id
post_id
Here is my User Model
public function posts()
{
return $this->hasMany('App\Models\Posts');
}
public function comments(){
return $this->hasManyThrough('App\Models\Comments','App\Models\Posts');
}
Here is my Posts Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function comments(){
return $this->hasMany('Ap\Models\Comments');
}
Here is my Comment Model
public function posts()
{
return $this->belongsTo('App\Models\Posts');
}
public function user(){
return $this->posts->name;
}
Here is my code how i am accessing user name
$comments = Comments::find(1);
$comment['comment'] = $comments->comment;
$comment['user_name'] = $comments->name;
$comment['post_id'] = $comments->posts->id;
may be i am not getting in right direction? if i am doing right then why it is not working.
In laravel 5 you do not call the model as you are doing. since the models are stored in the app folder just call like. Plus I think you need to define the relationship
class User extends Model {
public function phone()
{
return $this->hasOne('App\Phone');
}
}
class Phone extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
$phone = Phone::find(1);
For the case of foreign keys and more regarding the Eloquent relationships in laravel 5 just follow the documentation on the laravel website. Make sure to look at dynamic properties of that are allowed by eloquent
http://laravel.com/docs/5.0/eloquent

Laravel, Eloquent table relating to itself through pivot table

I have users class (table):
id
username
And relations table:
user_id
subscribed_by
Idea: each User can subscribe for each other user.
Last variant was (don't work):
/* class User extends Eloquent ... */
public function followers() {
return $this->belongsToMany('User', 'rel_table')->withPivot('user_id');
}
public function following() {
return $this->belongsToMany('User', 'rel_table')->withPivot('subscribed_by');
}
Need help: how to set up this?
First, an advice, you should rename the fields in your pivot table follower_id and followed_id (or something like that). That's more clear.
Then you have to define relations like that :
public function followers() {
return $this->belongsToMany('User', 'rel_table', 'followed_id', 'follower_id');
}
public function following() {
return $this->belongsToMany('User', 'rel_table', 'follower_id', 'followed_id');
}
The withPivot method is used to define other attributes on the relation than the two foreign keys.

Laravel Pivot Table and indirect relationship

I'm writing a survey with Laravel 4 and need the ability for users to be able to take the same survey multiple times (and have their answers saved as different instances.)
I currently have a pivot table called survey_user that links a user to an invited survey. A potentially positive side effect of the pivot table is that its primary key could be used to have unique survey instances.
My problem is figuring out how to get answers, specifically through the user model. Answers table would contain a foreign key to the primary of the pivot table.
My User model:
class User extends Eloquent {
public function surveys() {
return $this->belongsToMany('Survey', 'survey_user')
->withPivot('id', 'completed_at');
}
public function answers() {
// This should return all of the user's answers, irrespective of
// survey id's.
}
}
Tables:
surveys: id
users: id
survey_user: id, survey_id, user_id, completed_at
answers: survey_user_id, answer_text, ...
How might I accomplish this psuedo-relationship or perhaps a better way to structure?
Use relationships! Here's how I would do it:
class User extends Eloquent {
public function surveys() {
return $this->belongsToMany('Survey');
}
public function answers() {
return $this->hasMany('Answer');
}
}
class Survey extends Eloquent {
public function surveys() {
return $this->belongsToMany('User');
}
public function answers() {
return $this->hasMany('Answer');
}
}
class Answer extends Eloquent {
public function survey() {
return $this->belongsTo('Survey');
}
public function user() {
return $this->belongsTo('User');
}
}

Categories