Create View by Code First from EF Core 3.1 [closed] - view

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create sql view like a table at EF Core by Code First. How can I do this? Could you help me please?

This can help you
Entity:
public class Personel
{
public string Name { get; set; }
}
DBContext:
public DbSet<Personel> Personels { get; set; }
On Model Creating:
builder.Entity<Personel>(eb => {
eb.HasNoKey();
eb.ToView("View_Personel");
eb.Property(v => v.Name).HasColumnName("Name");
});
Add-Migration
migrationBuilder.Sql("
CREATE VIEW View_Personel AS
SELECT MatchValue AS Name
FROM Personel");
In addition, you can create with SQL Method Procedure, Materialized View, etc. by Code First (not DBFirst).

Related

How to limit the amount of embedded entity's data in GET operations in api-platform

Say we have two entities: Question and Answer. They are related as follows:
#[ApiResource]
class Question
{
//...
#[ORM\OneToMany(mmappedBy: "question", targetEntity: Answer::class)]
private Collection $answers;
}
#[APiResource]
class Answer
{
//...
#[ORM\ManyToOne(inversedBy: 'answers')]
#[ORM\JoinColumn(nullable: false)]
private Question $question;
}
We are requested to return Question with only the 5 (five) more recent answers embedded in it, while we could use another endpoint to fetch all the question's answers paginated. How can we do that with API Platform? Thanks in advance for any help.

laravel return number and boolean as string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
in prod version laravel return number and boolean as string !!
Localhost :
{ "age": 24, "is_admin": 0, }
Prod :
{ "age": "24", "is_admin": "0", }
you can use Attribute Casting, it provides functionality similar to accessors and mutators without requiring you to define any additional methods on your model. Instead, your model's $casts property provides a convenient method of converting attributes to common data types.
class MyModel extends Model
{
protected $casts = ['age' => 'integer','is_admin' => 'boolean'];

Access value before it's converted by an attribute accessor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an accessor on my model for a boolean column:
public function getActiveAttribute($value)
{
if ($value == 0) {
return "No";
} else {
return "Yes";
}
}
The problem I am having is the corresponding form input, a radio button, does not have either options selected when I go to a create or edit view. I could amend the form so the values of the radio buttons are "Yes" and "No" and then write a mutator for the same field.
Is there a better way to do it?
Most likely you just want to leave the existing active attribute alone, so you can access the raw value, but create an accessor for display purposes.
So, rename your accessor to getActiveYesNoAttribute(), and then your boolean will be available as active, and your Yes/No will be available as active_yes_no.
class User extends Model
{
public function getActiveYesNoAttribute()
{
return $this->active ? "Yes" : "No";
}
}
Usage:
$user = \App\User::first();
dd($user->active, $user->active_yes_no);

How to choose a relationship for tables in Laravel 5.2? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have created three tables:
Student(ID, username), Teacher(ID, name) and Rating(ID, rating)
On my Rating table I want to record student's rating to a particular teacher. Each student can rate a teacher exactly once. He can rate other teachers but not the same teacher.
In this case, which relationship will suit better? I am a newbie in Laravel.
Since each student can rate a teacher only once, the most straightforward would be to add student_id and teacher_id columns to Rating table and use them as foreign keys in the relationship.
Then you can define the relationship in Student model:
class Student extends Model
{
...
// Ratings given by the student
public function ratings()
{
return $this->hasMany('App\Rating');
}
}
and Teacher model:
class Teacher extends Model
{
...
// Ratings received by the teacher
public function ratings()
{
return $this->hasMany('App\Rating');
}
}
Read more on one-to-many relationships:
https://laravel.com/docs/5.3/eloquent-relationships#one-to-many

2 models in 1 view [duplicate]

This question already has answers here:
Closed 10 years ago.
I know my question is stupid, but I dont know solution of my problem and can understand similar questions on stackoverflow.
I doing simple blog.
And when I go to one post in this blog I must see text of post and comments for him. They there are in my datebase, but I dont know how display both.
Please help me
You can create a custom ViewModel for this particular View. Something like this:
public class BlogReaderViewModel
{
// various fields which exist on either the post or the comments
}
Then you'd bind to that ViewModel for the View. The Controller action would get the Models it needs and build an instance of the ViewModel to pass to the View.
Another option would be to use a Tuple. It's a generic class which acts as a strongly-typed container for multiple other types. So the View's Model would be something like this:
Tuple<Post, Comments>
From an overall design perspective, my biggest recommendation would be to consider how your Models relate to one another and find your "aggregate root." In the case of a blog post with comments, it sounds like the post should be the aggregate root. The Model itself should have the comments within it. Something like this:
public class BlogPost
{
public string Title { get; set; }
public string Body { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
The idea is that the aggregate root is the parent object and internally knows about its child objects. You shouldn't have to manually compose those hierarchies of objects every time you want to use them.
You have to create a ViewModel to represent this View or the data that this view need, for example:
public class OrderViewModel {
public int Id { get; set; }
public DateTime DateOrder { get; set; }
public decimal Total { get; set; }
public string CustomerName { get; set; }
public List<Item> Items { get; set; }
// other properties
}
And you shoul use this ViewModel to type your view, for sample (using razor):
#model Models.ViewModels.OrderViewModel
It depends on the relationship of the comments in the model. Usually comments should be a child collection of post. So in the view you should be able to render the comments with something like this (Razor):
#foreach (var comment in Model.Comments) {
// comments display goes here
}
Be sure when you pass the model to the view from the controller that you don't produce an inefficient query. Make sure that the query gets the comments with the blog, depending on how you are getting your model in the DB. If you are using EF that would be the "Include" directive, e.g.
.Include(p => p.Comment);
One option is to Create a composite model that represents both groups of data required to render the view, and pass the off each sub model to editor templates on the view itself.

Resources