类Illuminate \ Support \ Collection的对象无法转换为int -laravel

时间:2018-05-20 15:41:09

标签: php laravel

当我运行我的项目时,我收到错误

  
    

类Illuminate \ Support \ Collection的对象无法转换为int

  

指向代码行

public function show() { $selectedClass = Input::get('class',0); $classes =['0'=>'All']+ClassModel::pluck('name','code'); if($selectedClass){ $Subjects= DB::table('Subject') ->join('Class', 'Subject.class', '=', 'Class.code') ->select('Subject.id', 'Subject.code','Subject.name','Subject.type', 'Subject.subgroup','Subject.stdgroup','Subject.totalfull', 'Subject.totalpass','Subject.gradeSystem','Subject.wfull', 'Subject.wpass','Subject.mfull','Subject.mpass','Class.Name as class','Subject.sfull','Subject.spass', 'Subject.pfull','Subject.ppass') ->where('Subject.class',$selectedClass) ->get(); } else{ $Subjects= DB::table('Subject') ->join('Class', 'Subject.class', '=', 'Class.code') ->select('Subject.id', 'Subject.code','Subject.name','Subject.type', 'Subject.subgroup','Subject.stdgroup','Subject.totalfull', 'Subject.totalpass','Subject.gradeSystem','Subject.wfull', 'Subject.wpass','Subject.mfull','Subject.mpass','Class.Name as class','Subject.sfull','Subject.spass', 'Subject.pfull','Subject.ppass') ->get(); } return View::Make('app.subjectList',compact('Subjects','classes','selectedClass')); }

我怎样才能解决这个问题?

PS:Laravel初学者

控制器

import Foundation
import UIKit

class FilterViewController: UIViewController, UITableViewDelegate,UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate,UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    let properties = ["Diet","Body Type","Looking For","Ethnicity"]
    var values:NSArray!
    let dietOpts:NSArray = ["Vegan","Plant-based","Vegetarian","Meat Eater"]
    let bodytypeOpts:NSArray = ["Muscular","Athletic","Lean","Husky","Chubby","Large","Fat"]
    let lookingforOpts:NSArray = ["Friends","Dates","Chat","Networking","Relationship","Right Now"]
    let ethnicityOpts:NSArray = ["Hispanic/Latino","Black","White","Middle Eastern","South Asian","Asian","Native American","Pacific Islander"]

    var userFilterTable : UITableView!

    var cellTypes = [FilterTableViewCell.self,FilterTableViewCell.self,FilterTableViewCell2.self,FilterTableViewCell2.self]
    var reminderCells = [FilterTableViewCell(),FilterTableViewCell(),FilterTableViewCell2(),FilterTableViewCell2()]
    let cellIDs = ["cellId","cellId","cellId2","cellId2"]
    var toolBar : UIToolbar!
    var i:Int!
    var myUIPicker = UIPickerView()
    var multiplePicker = MultiplePicker()
    var myValues: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.blue

        let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height
        userFilterTable = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))

        userFilterTable.register(FilterTableViewCell.self, forCellReuseIdentifier: "cellId")
        userFilterTable.register(FilterTableViewCell2.self, forCellReuseIdentifier: "cellId2")

        userFilterTable.backgroundColor = UIColor.clear
        userFilterTable.isScrollEnabled = false
        userFilterTable.delegate = self
        userFilterTable.dataSource = self
        userFilterTable.rowHeight = 40

        self.userFilterTable.reloadData()
        self.view.addSubview(userFilterTable)
    }

    @objc func buttonAction(_ sender: UIButton!){
        print("Button tapped")
    }

    func generalPicker(){
        myUIPicker.becomeFirstResponder()
        self.myUIPicker.delegate = self
        self.myUIPicker.dataSource = self
    }

    @objc func donePicker() {
        reminderCells[i].valueTextField.resignFirstResponder()
    }

    func doMultiplePicker(){
        multiplePicker.becomeFirstResponder()
        //multiplePicker.delegate = self
        //multiplePicker.dataSource = self

        reminderCells[i].valueTextField.backgroundColor = UIColor.white
        toolBar = UIToolbar()
        toolBar.sizeToFit()
    }

    @objc func donePicker2() {
        reminderCells[i].valueTextField.text = multiplePicker.str
        reminderCells[i].valueTextField.resignFirstResponder()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        reminderCells[indexPath.row].valueTextField.becomeFirstResponder()

        i = indexPath.row
        print(properties[indexPath.item]) //print("Diet")
        switch properties[indexPath.item] {
        case "Diet":
            myValues = dietOpts
            generalPicker()
        case "BodyType":
            myValues = bodytypeOpts
            generalPicker()
        case "Looking For":
            myValues = lookingforOpts
            generalPicker()
        case "Ethnicity":
            myValues = bodytypeOpts
            doMultiplePicker()
        default:
            print("Some other character")
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // retuen no of rows in sections
        return properties.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        print(indexPath.row)
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIDs[indexPath.row], for: indexPath) as! cellTypes[indexPath.row]

        switch properties[indexPath.item]{
            case "Diet","Body Type":
                let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
                toolBar.setItems([doneButton], animated: false)
                cell.valueTextField.inputAccessoryView = toolBar
                cell.valueTextField.inputView = myUIPicker
            case "Looking For","Ethnicity":
                multiplePicker.frame = CGRect(x: 0, y: 0, width: 100, height: 300)
                multiplePicker.myValues = ethnicityOpts as! [String]
                let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker2))
                toolBar.setItems([doneButton], animated: false)
                cell.valueTextField.inputAccessoryView = toolBar
                cell.valueTextField.inputView = multiplePicker
            default:
                print("default")

        }
        cell.backgroundColor = UIColor.clear
        cell.property.text = properties[indexPath.item]
        cell.isUserInteractionEnabled = true
        cell.valueTextField.isUserInteractionEnabled = true
        cell.valueTextField.delegate = self
        cell.valueTextField.tag = indexPath.item
        toolBar = UIToolbar()
        toolBar.sizeToFit()
        reminderCells[indexPath.item] = cell
        return cell
    }

    // data method to return the number of column shown in the picker.
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    // data method to return the number of row shown in the picker.
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        print(myValues.count)
        return myValues.count
    }

    // delegate method to return the value shown in the picker
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return myValues[row] as? String
    }

    // delegate method called when the row was selected.
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        reminderCells[i].valueTextField.text = myValues[row] as? String
    }
}

1 个答案:

答案 0 :(得分:0)

您必须将集合转换为数组:

$classes = ['0'=>'All'] + ClassModel::pluck('name','code')->all();