Apply some constraints on Laravel model relationship inserting - php

I tried to find some suggestions in the web, but I couldn't...
I would like to use some constraints in the save method for a relationship in Laravel, I'll do an example to explain it
Let's suppose I've the 2 models Post and Comment, like in the Laravel documentation:
class Post extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
and
class Comment extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
}
Now, I would like to do a check on a new comment inserting, for example I would like to avoid the insertion if more than ten comments for that post already exist.
I insert a new comment with these instructions
$comment = new Comment();
$post->comments()->save($comment);
I could check before these lines, but I would like to check in some other point, where all the saves are detected, is there something similar? Otherwise, is there some "standard way" to do it?

There are helper methods in eloquent Model class that are exactly what you need. Since you need to do the check before inserting, you want to use the static::creating or static::saving method. Use saving if you want to validate both when creating and updating a comment. In the Comment model add this:
protected static function boot()
{
parent::boot();
static::creating(function (Comment $comment) {
// Do validation on the $comment model.
// Feel free to make sql queries or do anything you need.
// Throw an exception if your validation fails.
});
}

You can do it with count(). Like beliw you can try. Where if the rows have less than 10 comments then it will save otherwise it will return back to the url.
$comment = new Comment();
If(count ($post->comments) <10){
$post->comments()->save($comment);
}
else
return back() ;//dont save return back;
Hope it will help

Related

How to exclude/include specific database columns when eager-loading a nested model in Laravel 8?

To keep things simple, I'll give an example in code to what I'm trying to Achieve.
Let's assume that we have a model called User with the following code:
class User
{
public function posts()
{
return $this->hasOne(Post::class);
}
}
And the Post model look something like this.
class Post
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
And now what I want to be able to do is for example:
$user = Auth::user();
$comments = $user->post->comments; //here, I only want the "comment_id" and "comment_content" fields to be included, not all the Comment Model fields !
you can simply use hasManyThrough in user model to connect comments table.
read about hasManyThrough here: docs
and after having a relation between user and comments (hasManyThrough)
you can use load (Lazy eager loading) method
there is an piece of code you can use here
$user->load(["comments" => function($q){
$q->select("comment_id","comment_content");
}]);
read more about Lazy eager loading: docs

Is there a way to manipulate inputs in Laravel Model before storing them?

In Laravel, I have used a model's create method in many controllers,
Now I need to perform strip_tags($comment) to a specific input in all those controllers before it is inserted in database with create() method like this:
Comment:create([
'comment' => $comment,
...
]);
Should I repeatedly do this in all controllers:
$comment = strip_tags($comment); // < Is it possible to do this on model's file so we don't repeat it every time?
Comment:create([
'comment' => $comment,
...
]);
Or this is something that can be achieved in the Model?
You may use model events to make checks and arrangements before saving it.
add following method to your model class;
protected static function boot()
{
parent::boot();
self::saving(function ($model) {
$model->comment = strip_tags($model->comment);
// do your pre-checks or operations.
});
}
here is a useful post to read about it
There is a way to do it directly in the model, it's called Mutators. If your column name is comment then the mutator function will be called setCommentAttribute.
public function setCommentAttribute($comment)
{
$this->attributes['comment'] = strip_tags($comment);
}
Any place where save/update is used for this model, the comment data will go through the set... function.

Get relationships after insert

In my code, I insert a new row into the database:
$post = new Post;
$post->user_id = Auth::user()->id;
// more inserts
$post->save();
In my Post.php, I have:
protected $with = [
'user', 'answers', 'questions'
];
public function users()
{
return $this->belongsTo('App\User');
}
// etc
But when I return $post after I insert, there are no relationships (users, answers, questions) attached to it.
How can I get all of the default relationships to load after an insert?
The save() method persists the data to the database, but it doesn't do anything about refreshing the data on the Model or reloading relationships.
The easiest solution would be to refresh your object after calling save(). This will automatically eager load the relationships you've defined in your $with property on the model:
// ...
$post->save();
// refresh the post from the database
$post = $post->fresh();
Another option is to just manually reload the relationships yourself, using the load() method.
// ...
$post->save();
// reload the desired relationships
$post->load(['user', 'answers', 'questions']);
However, this duplicates the code that defines the relationships you'd like to be auto loaded (defined once in the Model, and then once in this code). You can mitigate that by creating a new function on your Model.
// in Post model
public function reloadRelations() {
$this->load($this->with);
}
// code usage
// ...
$post->save();
// call your new function to reload the relations
$post->reloadRelations();
However, the only real benefit of going this route over just calling the built in fresh() method is that this won't re-run the query to get the original Post data.
If you're handling 1000s of requests a second, maybe the one query might make a difference, but other than that, I wouldn't worry about it, and just use the fresh() method. But, the options are here for you to choose.
Instead of manually setting the attribute user_id, you should use the associate method from \Illuminate\Database\Eloquent\Relations\BelongsTo class.
$post->user()->associate(Auth::user());
// now you have the user inside your post.
dd($post->user);
May be, in model Post.php:
protected $primaryKey = 'id';
public function users()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
before:
migration "posts"
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
// .....
});
Hopefully this will solve your problem

Custom model method to get a relation in Laravel

I try to define a custom Model method in Laravel. I have a n:m relation between Subscription and Notification over SubscriptionNotification.
I already defined the default relations:
public function subscription_notifications() {
return $this->hasMany('App\SubscriptionNotification');
}
public function notifications() {
return $this->belongsToMany('App\Notification', 'subscription_notifications');
}
Now I want to define a method, which returns a collection of notifications. I collect the IDs of the notifications I want in an array and write the following method:
public function notifications_due() {
// Collect $notification_ids
return $this->belongsToMany('App\Notification', 'subscription_notifications')->whereIn('notifications.id', $notification_ids)->get();
}
But when I want to use the mothod by $subscription->notifications_due, I get the following error:
[LogicException]
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
I'm new to Laravel (I come from Rails). I don't know if this is in Laravel even possible. Maybe someone can help me. Thanks!
Remove the ->get() part in the method notifications_due. get() will return a Collection, but when calling the method as a property (or magic method), Laravel expects the method to return an instance of Relation. Laravel will then execute the query and transform it to a Collection automatically.
Also, you can use your already defined notifications() method:
public function notifications_due() {
// Collect $notification_ids
return $this->notifications()->whereIn('id', $notification_ids);
}
Remove the get call from your relationship method, for example:
public function notifications_due() {
return $this->belongsToMany(
'App\Notification',
'subscription_notifications
')->whereIn('notifications.id', $notification_ids);
}
Use it just same:
// It'll return a collection
$dues = $subscription->notifications_due;
To get all the ids from the collection you may try this:
$ids = $dues->pluck('id');
Also, you may add more constraints if you want if you use it like:the
$dues = $subscription->notifications_due()->where('some', 'thing')->get();
Or paginate:
$dues = $subscription->notifications_due()->where('some', 'thing')->paginate(10);

Check for a many to many relation when listing a resource

I'm implementing relationships in Eloquent, and I'm facing the following problem:
An article can have many followers (users), and a user can follow many articles (by follow I mean, the users get notifications when a followed article is updated).
Defining such a relationship is easy:
class User extends Eloquent {
public function followedArticles()
{
return $this->belongsToMany('Article', 'article_followers');
}
}
also
class Article extends Eloquent {
public function followers()
{
return $this->belongsToMany('User', 'article_followers');
}
}
Now, when listing articles I want to show an extra information about each article: if the current user is or is not following it.
So for each article I would have:
article_id
title
content
etc.
is_following (extra field)
What I am doing now is this:
$articles = Article::with(array(
'followers' => function($query) use ($userId) {
$query->where('article_followers.user_id', '=', $userId);
}
)
);
This way I have an extra field for each article: 'followers` containing an array with a single user, if the user is following the article, or an empty array if he is not following it.
In my controller I can process this data to have the form I want, but I feel this kind of a hack.
I would love to have a simple is_following field with a boolean (whether the user following the article).
Is there a simple way of doing this?
One way of doing this would be to create an accessor for the custom field:
class Article extends Eloquent {
protected $appends = array('is_following');
public function followers()
{
return $this->belongsToMany('User', 'article_followers');
}
public function getIsFollowingAttribute() {
// Insert code here to determine if the
// current instance is related to the current user
}
}
What this will do is create a new field named 'is_following' which will automatically be added to the returned json object or model.
The code to determine whether or not the currently logged in user is following the article would depend upon your application.
Something like this should work:
return $this->followers()->contains($user->id);

Categories