致命错误类名称必须是有效对象或字符串

福齐诺夫

我是laravel和php的新手,我的控制器中有此功能,它使我看到致命错误类别名称必须是有效对象或字符串

public function getIndex () {
    $categories = array();

    foreach ( $Category::all() as $key=> $category) {
        $categories[$category->id] = $category->name ;
    }

这是我的整个控制者

<?php 

class ProductsController extends BaseController {



public function __construct(){

    $this->beforeFilter('csrf' , array('on'=>'post')) ;
 }

public function getIndex () {
    $categories = array();

    foreach ( $Category::all() as $key=> $category) {
        $categories[$category->id] = $category->name ;
    }


    return View::make('products.index')
    ->with('products' , Product::all())
    ->with('categories' , $categories);
 }

public function postCreate(){

    $validator = Validator::make(Input::all() , Product::$rules);

    if ($validator->passes()){
        $product = new Product ; 
        $product->category_id = Input::get('category_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');

        $image = Input::file('image');
        $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(468,249)->save('public/img/products/'.$filename);
        $product->image = 'img/products/'.$filename;
        $product->save(); 

        return Redirect::to('admin/products/index')
        ->with('message' , 'Product Created');


    }
    return Redirect::to('admin/products/index')
    ->with('message', 'Something went wrong')
    ->withErrors($validator)
    ->withInput() ;
    }

public function postDestroy(){

        $product = Product::find(Input::get('id'));

        if($product){
            File::delete('public/'.$product->image);
            $product->delete() ;
            return Redirect::to('admin/products/index')
            ->with('message' , 'Product Deleted');

        }
        return Redirect::to('admin/products/index')
            ->with('message' , 'Something went wrong');


     }

    public function postToggleAvailability(){
        $product = Product::find(Input::get('id'));
        if($product){
            $product->availability = Input::get('availability');
            $product->save();
            return Redirect::to('admin/product/index')->with('message', 'product updated');

        }
        return Redirect::to('admin/product/index')->with('message' , 'Invalid Product');

    }


}
溴化硼

假设该类称为Category,则应Category::all()在foreach中使用制作:$ categories = array();

foreach ( Category::all() as $key=> $category) {
    $categories[$category->id] = $category->name ;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类名必须是有效的对象或字符串

类名称必须是有效对象或字符串Laravel 5.7

Laravel“类名必须是有效的对象或字符串”

Yii2 Codeception 接受类名必须是有效的对象或字符串

使用Laravel 5.4和PHP 7进行Composer更新“类名必须是有效的对象或字符串”

函数名称必须是字符串致命错误

PHP致命错误:函数名称必须是字符串

在Java中将字符串有效地解析为具有2个浮点的类对象

无法使用TortoiseGit检查差异:“致命:空字符串不是有效的pathspec”

Laravel验证:必须是带有“ json”规则的有效JSON字符串

Powershell:字符串变量不是有效的十六进制字符串错误

添加字符串的子字符串 IP 地址生成错误但有效

必须是有效的 JSON 字符串 Laravel 和 Postman

AWS Policy必须包含有效的版本字符串

第一个参数“ email”必须为有效字符串。”

JS-querySelector返回一个非法或无效的字符串错误-但是console.log中类名的有效字符串

可恢复的致命错误:类 PDOStatement 的对象无法转换为字符串 PHP 错误消息

Vuex操作无法正常运行。显示错误“ signInWithEmailAndPassword失败:第一个参数“ email”必须为有效字符串”

在 Django 中导致错误的有效 (?) JSON 数据必须作为字符串提供给前端并由 JSON.parse() 在 javascript 中转换 - 为什么?

可捕获的致命错误:无法将 AppBundle\Entity\Location 类的对象转换为字符串

可恢复的致命错误:无法将 PDOStatement 类的对象转换为字符串

Symfony 2可捕获的致命错误:无法将类UserCategory的对象转换为字符串

symfony2可捕获的致命错误:类的对象无法转换为字符串

可捕获的致命错误:DateTime类的对象无法转换为字符串

可捕获的致命错误:无法将MongoCursor类的对象转换为字符串

PHP,可捕获的致命错误:无法将mysqli类的对象转换为字符串

可捕获的致命错误:Instamojo类的对象无法转换为字符串

验证给定字符串是否为有效FQDN名称的最简单方法?

如何从字符串创建有效的MYSql数据库名称?