Rating model
class Rating extends Model
{
protected $fillable = [
'owner_id', 'toilet_id','user_id','rating','desc',
];
public function toilet()
{
return $this->belongsTo(ToiletInfo::class);
}
}
ToiletInfo model
class ToiletInfo extends Model
{
protected $fillable = [
'owner_id', 'toilet_name','price','complex_name','address','toilet_lat','toilet_lng','status',
];
public function owner()
{
return $this->belongsTo(ToiletOwner::class);
}
public function ratings()
{
return $this->hasMany(Rating::class,'toilet_id');
}
}
RatingController
public function index()
{
return $toilets = ToiletInfo::with('ratings')->get();
//return view('admin.rating',compact('toilets'));
}
I want to get the average of rating but how to access elements inside ratings[]
Or help me improve the method I am using to get ratings for toilets that are reviewed by users
From what I understand from your question you want to get the average rating.
In your ToiletInfo model, add a new method:
public function getAverageRating()
{
$ratings = $this->ratings;
$count = $ratings->count(); // total count
$total_ratings = $ratings->sum('rating'); // add the 'rating' for all rows
return $total_ratings / $count; // average
}
In your blade file, you can simply do
$toilet->getAverageRating()
Which will give the average rating.
Related
I have two models Member and Saving with
Member('id','name')
Saving('id','member_id','amount','month')
I have to restrict duplicate entry of saving of a member for a given month.
Member.php
class Member extends Model
{
protected $fillable=['name','address','phone'];
public function loan()
{
return $this->hasOne(Loan::class,'member_id','id');
}
public function savings()
{
return $this->hasMany(Saving::class,'member_id','id');
}
}
Saving.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Saving extends Model
{
protected $fillable=['amount','month','member_id'];
public function member()
{
return $this->belongsTo('App\Member','member_id','id');
}
}
Is it possible to implement restriction, using functions in model, or controller?
EDIT:
this is how i tried.
SavingController.php
public function addSaving(Request $request){
if($request->isMethod('get')){
$memberData = Member::all();
return view($this->_pagePath.'saving.addsaving',compact('memberData'));
//return redirect()->back();
}
if($request->isMethod('post')){
$this->validate($request,[
'amount' => 'required',
'month' => 'required'
]);
$data['amount'] = $request->amount;
$data['month'] = $request->month;
$data['member_id']= $request->name;
$member= Member::find(1);
if($member->savings()->where('month',$request->month)->exists())
{
return 'Savings for this month are already added.';
}
else
{
if(DB::table('savings')->insert($data)){
return redirect()->route('saving')->with('success','data was successfuly inserted');
}
}
}
}
An enum is a wrong column for month my friend, unless you don't care for the year. Because this way you will just be able to store savings for each month, but not for each year separately.
Your relationship is set correctly, so you can then make a check like this:
// considering you have the specific member selected
$member = Member::find('ID_OF_THE_MEMBER');
if($member->savings()->where('month', APRIL)->exists())
{
return 'Savings for this month are already added.';
}
I know this is a bit silly and probably been asked many times but for this one is unique. I'm just asking just for the sake of learning. I have these arrays inside home():
public function home()
{
$menus = [ '视频', '直播', '路亚', '海钓', '渔获' ];
$submenus1 = [ '视频', '直播', '路亚', ];
return view('/layout', [
'menus' => $menus,
'submenus1' => $submenus1,
]);
}
So it's like Nav items. And these items, I want them to be available in all views. Help would be much appreciated. Please respect. Thank you.
You can pass any data with View::share() method in App/Providers/AppServiceProvider.php file. Please check following codes;
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$menus = [ '视频', '直播', '路亚', '海钓', '渔获' ];
$submenus1 = [ '视频', '直播', '路亚', ];
View::share('menus', $menus);
View::share('submenus1', $submenus1);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind('path.public', function() {
return base_path('public_html');
});
}
}
After than you can use $menus and $submenus1 variables anywhere
I have a controller that should return a list of categories. Each should also have a collection of sub-categories. Each sub-category should also have modules. The modules are of two types: the latest modules and most watched modules.
A SUMMARY STRUCTURE I WANT:
[
{
id: 1,
name: category 1,
sub_categories:
[
{
id: 1,
name: subcategory 1:
modules:
[
latestModules: [...],
mostViewedModules[...]
]
}
],
another sub-category object,
and another sub-category object
},
another category object,
and another category object,
]
My Controller
class SeriesController extends Controller
{
public function getSeries()
{
$categories = Category::with('SubCategories')->get();
$countCats = 0;
$countSubCats = 0;
collect($categories)->map(function ($category) use ($countCats, $countSubCats, $categories){
collect($category->subCategories)->map(function ($subCategory) use ($countSubCats, $categories) {
collect($categories[$countCats]->subCategories[$countSubCats])
->put('modules',
SubCategory::with('latestModules', 'mostViewedModules')
->where('id', $subCategory->id)->get()
);
$countSubCats++;
});
$countCats++;
});
return $categories;
}
}
My Category model
class Category extends Model
{
public function subCategories()
{
return $this->hasMany('App\SubCategory')
->orderby('name');
}
}
My Subcategory Model
class SubCategory extends Model
{
public function parentCategory(){
return $this->belongsTo('App\Category', 'category_id');
}
public function modules()
{
return $this->belongsToMany('App\Module', 'module_category', 'sub_category_id', 'module_id');
}
public function latestModules()
{
return $this->modules()
->orderBy('created_at', 'desc');
}
public function mostViewedModules()
{
return $this->modules()
->orderBy('views', 'desc');
}
}
My Module Model
class Module extends Model
{
public function subCategories()
{
return $this->belongsToMany('App\SubCategory', 'module_category', 'module_id', 'sub_category_id');
}
}
WHAT I WAS TRYING TO DO
1) Get all the categories via Eloquent Query
2) Collect the many categories and Map over them
3) For each ONE category i map over again to single it out into sub-categories
4) For each sub-category i run Eloquent query to return the related modules.
5) Then i put that result into categories original array by using PUT. I wanted to expand on the original $categories array by including the modules.
PROBLEM
I realize that the loop works and everything is fine, except that the global $categories variable does not update. Its like inside the loops its updated but once we exist the Map loops the value defaults to
Category::with('SubCategories')->get()
Whenever I call this function, I get the user_id correctly but the password isnt checked...
Model:
<?php
class Prometheus_model extends CI_Model {
var $tables = array(
'bots' => 'bots',
'users' => 'users'
);
function __construct() {
parent::__construct();
}
public function tablename($table = NULL) {
if(! isset($table)) return FALSE;
return $this->tables[$table];
}
public function get($table, $where = array(), $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
// You should use $q->num_rows() to detect the number of returned rows
if($q->num_rows()) {
return $result[0];
}
return $result;
}
public function update($table, $where = array(), $data) {
$this->db->update($this->tablename($table),$data,$where);
return $this->db->affected_rows();
}
public function insert($table, $data) {
$this->db->insert($this->tablename($table),$data);
return $this->db->insert_id();
}
public function delete($table, $where = array()) {
$this->db->delete($this->tablename($table),$where);
return $this->db->affected_rows();
}
public function explicit($query) {
$q = $this->db->query($query);
if(is_object($q)) {
return $q->result_array();
} else {
return $q;
}
}
public function num_rows($table, $where = NULL) {
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
public function get_bot_data_by_hw_id($bot_hw_id) {
$q = $this->get('bots', array('bot_hw_id' => $bot_hw_id));
return $q;
}
public function check_user_data($user_incredials, $user_password) {
if($this->num_rows('users', array('user_name' => $user_incredials, 'user_password' => $this->encrypt->decode($user_password))) == 1){
$q = $this->get('users', array('user_name' => $this->security->xss_clean($user_incredials)));
return $q['user_id'];
}
return FALSE;
}
}
?>
My function-calling at the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index(){
if($this->input->post('user_login')){
var_dump($this->prometheus_model->check_user_data($this->input->post('user_incredials'), $this->input->post('user_password')));
}
$this->load->view('login_index');
}
}
How can i fixx this ?
In your check_user_data() method you are using
if($this->num_rows('users', array('user_name' => $user_incredials, 'user_password' => $this->encrypt->decode($user_password))) == 1)
I think (logically) following code
$this->encrypt->decode($user_password)
should be
$this->encrypt->encode($user_password)
because, you are calling num_rows() method and it is
public function num_rows($table, $where = NULL)
{
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
which is actually querying the data base something like, for example,
select * from USERS where user_name = 'heera' and password = decode('abcde12345')
In this case, the password you are trying to match is need to be encrypted using encode (not decode) method, because the user has given you a non-encrypted (plain) password and the password saved in the database is already encrypted, so encode the plain password using encode method before you query the database to match with already encoded passwords.
In my controller i have 2 actions e.g
action1() {
//code
SomeArray=();
//code
}
How can i pass all the SomeArray data to action2?
I have tried to create a public array variable in my class and pass it but with no luck.
i have tried to pass as an argument to the action2...
e.g in action1, $this->action2(SomeArray) and then action2($param) with no luck again.
function doExam($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid exam', true));
$this->redirect(array('action' => 'index'));
}
$this->Exam->recursive=1;
$conditions_question = array('Question.exam_id' => $id);
$questions = $this->Exam->Question->find('all',array('conditions' => $conditions_question));
foreach ($questions as $question) {
**$this->questionsByExam[]** = $question['Question']['qst'];
}
//OK PASSED
echo debug($this->questionsByExam);
//OK $exam_id
$this->exam_id = $id;
}
i have another action validate_answer, and i want to pass the questionsByExam in here
any help?
Thanks in advance
I have tried to create a public array variable in my class and pass it but with no luck.
Can you show the code for this? It should work fine as class variable...
E.g:
class FooController extends AppController {
var $someArray = array();
function doExam() {
// Populate the array here
$this->someArray = array(1,2,3);
}
function bar() {
// Use it here, no need to pass it as an argument
print_r($this->someArray);
}
}