![](/img/trans.png)
[英]Error : Trying to get property 'headers' of non-object when I try Closure based middleware directly in controller's constructor. Laravel 6
[英]when i try to edit or delete it gives "trying to access property of a non object error". laravel php
這是我嘗試刪除或編輯記錄時遇到的錯誤:
[![這是我嘗試刪除或編輯記錄時遇到的錯誤][1]][1]
當我嘗試更新或刪除記錄時出現此錯誤。 我使用 get() 方法從查詢中檢索結果。 它說當我使用 get() 方法時它返回一個數組。 但是顯示數據沒有問題。
這是我的控制器類方法。
public static function deleteData($childid){
$childid = $rss->input('hidCid');
// DB::enableQueryLog(); // Enable query log
DB::table('children')->where('childid', '=', $childid)->delete();
//$del=App\Flight::find($childid);
//$del->delete();
//dd(DB::getQueryLog()); // Show results of log
}
在我看來,我有這樣的事情。
<table class="table table-bordered table-striped" id="tablethree">
<thead>
<tr>
<th>ChildName</th>
<th>ChildDoB</th>
</tr>
</thead>
<tbody>
@foreach ( $chdata as $roww)
<tr>
<input type="hidden" name="" value="{{$roww->childid}}">
<td> <input class="form-control" type="text" name="chx[]" value="{{ $roww-> ChildName}}"></td>
<td> <input type="date" class="form-control" name="chxD[]" value="{{ $roww-> ChildDoB}}"> </td>
<td> <a href="{{ url('/moreDetails/delete') }}" class="btn btn-xs btn-info pull-right">Delete.{{ $roww-> childid}}</a></td>
</tr>
@endforeach
</tbody>
</table>
這是我的整個控制器類
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\people;
use App\mha_dts;
use App\mha_dvs;
use App\DoTable;
use App\child;
use App\leaveData;
use App\training;
use App\Page;
use Datatables;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
class jointableController extends Controller
{
function index()
{
return view('join_table');
}
public function showTable()
{
$dtb = DB::table('do_tables')
->join('mha_dvs', 'mha_dvs.dvs_id', '=', 'do_tables.Division')
->join('mha_dts', 'mha_dts.id', '=', 'do_tables.District')
->select('do_tables.Do_id','do_tables.NameWithInitials', 'mha_dvs.division',
'mha_dts.district','do_tables.Nic',
'do_tables.Mobile','do_tables.PermanentAddress',
'do_tables.created_at','do_tables.updated_at');
return Datatables::of($dtb)
->addColumn('action', function ($dtb) {
return '<a href= "moreDetails/'.$dtb->Nic.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> View More</a>';
})
->editColumn('Do_id', 'MHA_DO{{$Do_id}}')
->make(true);
}
public function showind($Nic){
$departmentData['data'] = Page::getDistrict();
// DB::enableQueryLog(); // Enable query log
$userss = DB::table('do_tables')
->leftJoin('children','children.ParentID','=','do_tables.Do_id')
->leftJoin('leave_datas','leave_datas.DevID','=','do_tables.Do_id')
->leftJoin('trainings','trainings.DoID','=','do_tables.Do_id')
->select('*','children.ChildName')
->where('do_tables.Nic',[$Nic])
->first();
//->groupBy('do_tables.Do_id','leave_datas.LeaveId','children.childid','trainings.TId')
// ->select(array('*', DB::raw('COUNT(children.ChildName) as followers')))
// dd(DB::getQueryLog()); // Show results of log
$chdata = DB::table('do_tables')
->Join('children','children.ParentID','=','do_tables.Do_id')
->select('*','children.ChildName','children.childid')
->where('do_tables.Nic',[$Nic])
->get();
$trdata = DB::table('do_tables')
->Join('trainings','trainings.DoID','=','do_tables.Do_id')
->select('*','trainings.Training_Name')
->where('do_tables.Nic',[$Nic])
->get();
$lvdata = DB::table('do_tables')
->Join('leave_datas','leave_datas.DevID','=','do_tables.Do_id')
->select('*','leave_datas.Year')
->where('do_tables.Nic',[$Nic])
->get();
return view('moreDetails',['userss'=>$userss],compact('chdata','trdata','lvdata'))
->with("departmentData",$departmentData);
}
public function store(Request $reequest){
// $this -> validate ($reequest,[
// 'mchname' => 'required',
// 'mdob' => 'required',
//
// ]);
$chs = new child ;
$chs -> ChildName = $reequest->input('mchname');
$chs -> ChildDoB =$reequest->input('mdob');
$chs -> ParentID = $reequest->input('mchdoid');
$chs->save();
return redirect()
->back()
-> with ('success','data saved!');
}
public function update(Request $editreq)
{
//DB::enableQueryLog(); // Enable query log
$Do_id = $editreq->input('ddoid');
$editDo = DoTable::find($Do_id);
$editDo-> District= $editreq->input('District');
$editDo-> Division= $editreq->input('Division');
$editDo-> Full_Name= $editreq->input('fname');
$editDo-> NameWithInitials= $editreq->input('inName');
$editDo-> Nic= $editreq->input('nic');
$editDo-> DoB= $editreq->input('dob');
$editDo-> Mobile= $editreq->input('mobile');
$editDo-> Landline= $editreq->input('land');
$editDo-> PermanentAddress= $editreq->input('paddress');
$editDo-> CurrentAddress= $editreq->input('caddress');
$editDo-> SpouseName= $editreq->input('spname');
$editDo-> Designation= $editreq->input('designation');
$editDo-> Workstation= $editreq->input('wstation');
$editDo-> CurrentServiceLocation= $editreq->input('workstation');
$editDo-> FromDate= $editreq->input('fromdate');
$editDo-> ToDate= $editreq->input('todate');
$editDo-> Ministry= $editreq->input('ministry');
$editDo-> Degree= $editreq->input('degree');
$editDo-> Subject= $editreq->input('sarea');
$editDo-> Institiute= $editreq->input('institute');
$editDo-> AppointmentLetterNum= $editreq->input('Aletter');
$editDo-> AppointmentDate= $editreq->input('Apdate');
$editDo-> AbsorbedLetterNum= $editreq->input('abletter');
$editDo-> AbsorbedDate= $editreq->input('abdate');
$editDo-> PermenentDate= $editreq->input('perdate');
$editDo-> LanguageProf= $editreq->input('optradio');
$editDo-> PassedDate= $editreq->input('passdate');
$editDo-> SalaryIncrement= $editreq->input('lastinc');
$editDo-> IncrementedDate= $editreq->input('incDate');
$editDo-> WOPNum= $editreq->input('WOPNum');
$editDo-> NumOfDepends= $editreq->input('NoDep');
$editDo-> OtherDetails= $editreq->input('otherd');
$editDo->save();
//dd(DB::getQueryLog()); // Show results of log
return redirect()
->back();
}
public function childUpdate(Request $rss){
$childid = $rss->input('hidCid');
//DB::enableQueryLog(); // Enable query log
$editchild = child::findOrFail($childid);
//dd(DB::getQueryLog()); // Show results of log
$cname = $rss -> input('chx');
$cdob = $rss-> input('chxD');
for ($i=0; $i < count($cname) ; $i++) {
$cdata = array(
'ChildName' => $cname[$i] ,
'ChildDoB' => $cdob[$i] );
$updateData[]=$cdata;
}
child::update($updateData);
// $editchild-> ChildName = $rss -> input('chx');
// $editchild-> ChildDoB = $rss -> input('chxD');
// $editchild->save();
return redirect()
->back();
}
public static function deleteData($childid){
$childid = $rss->input('hidCid');
// DB::enableQueryLog(); // Enable query log
DB::table('children')->where('childid', '=', $childid)->delete();
//$del=App\Flight::find($childid);
//$del->delete();
//dd(DB::getQueryLog()); // Show results of log
}
你傳遞任何請求對象嗎? 如果您有源控制器,則需要使用 delete 方法將數據作為表單數據傳遞。 路由文件
Route::resource('crud','CrudsController');
查看文件
<form method="post" action="{{route('crud.destroy',$row->id)}}">
@csrf
@method('DELETE')
<input type="submit" name="delete" value="Delete" class="btn btn-danger">
</form>
在你的控制器中
public function destroy($id)
{
//
$data=Crud::findOrFail($id);
$data->delete();
return redirect('crud')->with('success','Data is deleted successfully');
}
用於獲取$userss
的查詢使用first()
,它可以返回單個記錄或null
。 $userss
為null
,在視圖中它被訪問為$userss->District
。 哪個是null->District
會導致錯誤“試圖訪問非對象的屬性”。
你沒有刪除任何東西。 這是您要顯示的頁面,而不是刪除任何記錄。 您正在嘗試在視圖中使用$userss
變量,但沒有顯示您的視圖,因此沒有人可能知道您實際嘗試做什么。 在使用它之前,您需要檢查$userss
是否為null
。
現在由您決定如何處理可能為null
$userss
變量的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.