laravel 5.1 foreach试图获得非物体的属性

时间:2018-02-04 09:20:29

标签: php laravel-5 foreach

我的代码中每个都有3-4个,这些代码都使用了Eloquent,并且在所有代码中我都得到了相同的错误

Trying to get property of non-object 

当我发现foreach的第一个参数是一个雄辩的时候它会返回一个json数组,其中包含该表数据库中的所有数据,所以它不返回null虽然我知道它可能会在foreach循环中中断或带来null但是我不知道其中一个地方的模型和控制器在哪里,所以这里是它自己的循环

                                <?php foreach(\App\Menu::all()->where('slug','main')->first()->items as $top_menu): ?>
                                <li class="dropdown">
                                    <a href="<?php echo e(URL($top_menu->link)); ?>" <?php if($top_menu->link_blank): ?> target="_blank" <?php endif; ?>>
                                        <?php echo e($top_menu->title); ?>

                                    </a>
                                </li>
                            <?php endforeach; ?>

这是我的控制器

class MenuItemsController extends AdminController

{

var $object = 'menu_items';
var $route_name = 'menu_items';
var $object_title = 'item';
var $object_titles = 'items';

var $attachments_config = [
    'main_image' => [],
    'default_thumb_sizes' => [],
    'require' => ['main_image']
];

var $dt_fields_db = [];
var $dt_fields_heading = ['شناسه','منو','عنوان','موقعیت','نمایش','ثبت','عملیات'];
var $dt_fields_full = [
    'id' => ['menu_items.id'],
    'menu_title' => ['menus.title'],
    'title' => ['menu_items.title'],
    'position' => ['menu_items.position'],
    'display' => ['menu_items.display'],
    'created_at' => ['menu_items.created_at'],
    'actions' => ['actions']
];

var $messages = array();

public function __construct() {
    parent::__construct();

    view()->share(['attachments_config' => $this->attachments_config]);
}

function index() {

    $data['extra_assets'] = array(
        array('type' => 'css','path' => 'datatables/datatables.bootstrap.css'),
        array('type' => 'js','path' => 'datatables/jquery.dataTables.min.js'),
        array('type' => 'js','path' => 'datatables/datatables.bootstrap.js'),
        array('type' => 'script','path' => 'pagescripts/shared/datatables_script.php'),
    );

    $data['dt_fields_heading'] = $this->dt_fields_heading;
    $data['dt_fields'] = $this->dt_fields_full;

    $data['heading'] = $this->object_titles;
    return view('admin.shared.datatable',$data);
}

function datatable() {
    $this->dt_filtered_actions = dt_actions_filter($this->route_name,['edit','delete']);

    $objects = DB::table('menu_items')
        ->join('menus', 'menu_items.menu_id','=', 'menus.id')
        ->select([
            'menu_items.id',
            'menus.title as menu_title',
            'menu_items.title',
            'menu_items.display',
            'menu_items.position',
            'menu_items.created_at'
        ]);

    return Datatables::of($objects)
        ->editColumn('display', function($model) {return label_status($model->display);})
        ->editColumn('created_at', function($model) {return ($date = jDate::forge($model->created_at)->format('%H:%M:%S - %y/%m/%d'))?$date:"";})
        ->addColumn('actions', function($model) {
            return dt_actions($this->route,$model->id,$this->dt_filtered_actions);
        })
        ->make(true);
}

function add() {

    $data['extra_assets'] = array(
        array('type' => 'js','path' => 'select2/select2.full.min.js'),
        array('type' => 'css','path' => 'select2/select2.min.css'),
        array('type' => 'css','path' => 'select2/select2-bootstrap.css'),
        array('type' => 'helper','name' => 'select2'),
    );

    $data['menus'] = Menu::all()->lists('title','id');

    $data['heading'] = 'add'.$this->object_title;
    return view('admin.'.$this->route_name.'.add',$data);
}

function create(\Illuminate\Http\Request $request) {

    $validator = Validator::make($request->all(), [
        'menu_id' => 'required',
    ]);

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    $input = $request->all();

    $input['display'] = (isset($input['display']))?$input['display']:0;

    $menu = Menu::find($input['menu_id']);
    if($menu->items->count() == $menu->capacity) {
        $this->messages[] = array('type' => 'danger', 'text' => 'menu is full');
        Session::flash('messages', $this->messages);
        return back();
    }

    MenuItem::create($input);

    $this->messages[] = array('type' => 'success', 'text' => 'success.');
    Session::flash('messages', $this->messages);
    return redirect()->route('admin.'.$this->route_name.'.index');
}

function edit(MenuItem $MenuItem) {
    $data['object'] = $MenuItem;

    $data['extra_assets'] = array(
        array('type' => 'script','path' => 'pagescripts/shared/image_preview_script.php'),

        array('type' => 'js','path' => 'select2/select2.full.min.js'),
        array('type' => 'css','path' => 'select2/select2.min.css'),
        array('type' => 'css','path' => 'select2/select2-bootstrap.css'),
        array('type' => 'helper','name' => 'select2'),

        array('type' => 'js','path' => 'ckeditor/ckeditor.js'),
        array('type' => 'js','path' => 'ckeditor/config.js'),
        array('type' => 'js','path' => 'ckfinder/ckfinder.js'),
        array('type' => 'js','path' => 'ckfinder/config.js'),
        array('type' => 'script','path' => 'pagescripts/shared/ckeditor_script.php'),
    );

    $data['menus'] = Menu::all()->lists('title','id');

    $data['heading'] = 'edit'.$this->object_title;
    return view('admin.'.$this->route_name.'.edit',$data);
}

function update(MenuItem $MenuItem,\Illuminate\Http\Request $request) {

    $validator = Validator::make($request->all(), [
        'menu_id' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator)
            ->withInput();
    }

    $input = $request->all();

    $input['display'] = (isset($input['display']))?$input['display']:0;

    $MenuItem->update($input);

    $this->messages[] = array('type' => 'success', 'text' => 'success.');
    Session::flash('messages', $this->messages);
    return redirect()->back();
}

function delete(\Illuminate\Http\Request $requests,MenuItem $MenuItem) {
    if($requests->ajax() && $MenuItem->delete()) {
        return "OK";
    } else {
        return "ERROR";
    }
}

function deleteall(\Illuminate\Http\Request $requests) {
    $ids = $requests->get('ids');
    if($requests->ajax()) {
        foreach (explode(',',$ids) as $id) {
            if(intval($id)) MenuItem::find($id)->delete();
        }
        return "OK";
    } else {
        return "ERROR";
    }
}

}

1 个答案:

答案 0 :(得分:0)

问题是因为我的数据库表没有通过菜单块获取任何行,并且查询返回null并且发生错误,当您的查询返回null时通常会发生错误感谢所有帮助