i am having 3-4 for each in my code which all use Eloquent and in all of them i get the same error
Trying to get property of non-object
when i dd the first argument of foreach which is an eloquent it returns an array of json with all the data in database of that table so its not returning null though i know it may break or bring null while in a foreach loop but i dont know where place one of them with model and controller here so here is the loop it self
<?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; ?>
and here is my controller
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";
}
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire