public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.SingleOrDefault(x => x.EmployeeId == id);
return View(employee);
}
}
//here i am getting only one row with respect to id .. how can i get the whole rows from the table ?and how can i view that on the view page?
To return the whole table, just select the whole table instead of filtering.
Employee[] allEmployees = employeeContext.Employees.ToArray();
To show them in a view, just have the model type for your view be an array instead of just a singular Employee. Basically just make the model Employee[]. Then you can just loop over the model.
#model Employee[]
#foreach(var employee in Model)
{
// do something interesting
}
Related
I have a controller with the action:
public function getCities(): JsonResponse
{
return response()->json([City::all()], 200);
}
Entity City has relation to Country.
How I can add country.id for every item in the result City::all()?
Laravel has amazing feature for creating virtual attributes. Add these lines to your City model for this:
NOTIFICATION: I assume you have the CountryCity model
public $appends = ['country_id'];
public function getCountryIdAttribute()
{
$country = CountryCity::where('city_id',$this->id)
if($country){
return $country->id;
}
return null;
}
You should look at the documentation : https://laravel.com/docs/8.x/eloquent-relationships
The best way is to use eager loading, using :
City::with('country')->get();
Then, you can access country id like :
$city->country->id;
I want to display all food_item_category name in view which is belongs to food item
Here is my 3table informations
food item table column: "food_item_id", "name" , "image"
food item category table column: "food_item_category_id" , "name"
pivot table name "food_items_have_categories" and its column is
-> id,
--> food_item_id
-->food_item_category_id
Here is fooditem model
public function foodItemCategory() {
return $this->belongsToMany(FoodItemCategory::class, 'food_items_have_categories', 'food_item_id', 'food_item_category_id')
->withTimestamps();
}
I am displaying food details in ajax request. Here is my controller
public function getProduct()
{
$products = FoodItem::all();
return response()->json($products);
}
Here is in view
$.each(response, function(key, value){
data = data + "<tr>"
data = data + "<td>"+i+++"</td>"
data = data + "<td>"+ "<img src='/storage/items/food/"+value.image+"'/>" +"</td>"
data = data + "<td>"+value.name+"</td>"
Now, I want to display food_item category in the table. How to do that through pivot table?
Try this here:
public function foodItemCategory() {
return $this->belongsToMany(FoodItemCategory::class, 'food_items_have_categories', 'food_item_id', 'food_item_category_id')
->withPivot('food_item_id', 'food_item_category_id')
->withTimestamps();
}
I am working on a roll format for a laravel project.
I have successfully created the roll, and can update the roll status in the roll table which looks like this
|id|roll_id|member_id|status|created_at|updated_at|
One of my status is "Present/Voucher" using funds off a Voucher
I have the Voucher Table
|id|member_id|voucher|date|balance|created_at|updated_at|
When a user clicks on the present/voucher icon in the roll, the following is called in the controller
public function voucher($id)
{
$r = Roll::find($id);
if ($r != null)
{
$r->status = "V";
$r->save();
return redirect(action('RollController#index'))->with ('success', 'Member Paid with Vocuher');
}
return redirect(action('RollController#index'));
}
This works perfect, what I would like to know is how to insert a record into my Voucher table, while I can find the id record in the roll table, I need to pull that member_id so I can add this into the voucher table
For example if the Roll Record looks like this
|id|roll_id|member_id|Status|
|20|2|13|V|
I need to grab 13 are the Member_ID from roll record 20, so I can insert in the vouchers table
|id|member_id|voucher|date|balance
|2|13|Weekly Fees|currdate|-10
Please note the 10 is also stored in a settings table, so if I can grab from that table that would be great
Settings Table:
|id|Setting|Value|
|1|weekly Subs|10|
Thanks
You could first define model relationship.
Member
class Member extends Model {
public function vouchers()
{
return $this->hasMany('App\Voucher');
}
}
Roll
class Roll extends Model {
public function member()
{
return $this->belongsTo('App\Member');
}
}
Voucher
class Voucher extends Model {
public function member()
{
return $this->belongsTo('App\Member');
}
public function setting()
{
return $this->belongsTo('App\Setting');
}
}
Then you can execute fluently in controller, you should also use lockForUpdate and DB::transaction
public function voucher($id)
{
DB::transaction(function () use ($id) {
$roll = Roll::lockForUpdate()->find($id);
if ($roll != null) {
$roll->status = "V";
$roll->save();
$voucher = new Voucher();
$voucher->member_id = $roll->member_id;
...
// Correct me if I am wrong, form your table structure it looks like you linked table voucher and setting with 'value'
// I strongly recommend you to have setting_id as foreign key in table voucher
// So you could do
// $setting = Setting::where('Setting', 'weekly Subs')->where('Value', 10)->first();
// $voucher->setting_id = $setting->id;
$voucher->save();
return redirect(action('RollController#index'))->with ('success', 'Member Paid with Vocuher');
}
}
return redirect(action('RollController#index'));
}
I have a table with about 3 items. I wish to populate the Dropdown List using that table. But i have a condition that i want to show only 2 items in the Dropdown List. How can it be done?
You could use a view model:
public class MyViewModel
{
public string SelectedItemId { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
and then take the first 2 items (obviously if your requirements is to take some other 2 items based on some condition you could chain with the .Where() extension method in order to filter first before calling .Take()):
public ActionResult Index()
{
var model = new MyViewModel();
model.Items = db.Items.Take(2).ToList().Select(x => new SelectListItem
{
Value = x.Id,
Text = x.SomeText
});
return View(model);
}
The view is standard stuff, simply call to the DropDownListFor helper:
#model MyViewModel
#Html.DropDownListFor(x => x.SelectedItemId, Model.Items)
I am developing an MVC 3 application in C# and I would like to know how to list database entries, based on a condition. In my case, I only want to display invoices that have the status "Paid" or "Partly Paid".
So this is where I list them:
public ViewResult Index()
{
var invoices = db.Invoice.Include(c => c.Client);
return View(db.Invoice.ToList());
}
How I would I add the condition into this code?
Thanks,
Amy
Try this:
public ViewResult Index()
{
var invoices = db.Invoice.Include(c => c.Client).Where(i => i.Status !="Confirmed";
return View(invoices.ToList());
}