[英]Trying to get property of non-object Laravel with php5.6 installation
any help is appreciated : 任何帮助表示赞赏:
**the database have the tables created and working fine : ** **数据库已创建表并正常工作:**
but the browser returns error (Trying to get property of non-object (View: ) 但浏览器返回错误(尝试获取非对象的属性(视图:)
The controller file is : 控制器文件为:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\DocTracking;
use App\Models\User;
use Exception;
use Auth;
use PDF;
use QrCode;
use Carbon;
use Validator;
use Illuminate\Support\Facades\input;
class DocTrackingController extends Controller
{
public function __construct() {
$this->middleware('auth', ['only', 'getDashboard']);
}
public function index(Request $request,$id=null)
{
if(!Auth::check()){
return "Please Login";
}
if($id != null){
$userid= $id;
}
else {
$user_id= Auth::user()->id;
$selluser = User::find($user_id);
}
$data = DocTracking::all();
$data = array(
'name'=> $request->name,
'start_date' =>Carbon::parse( $request->start_date),
'end_date' =>Carbon::parse( $request->end_date),
'amount' => $request->amount,
'merchant_id'=> $user_id
);
/*
$doc_data = DocTracking::all();
$doc_data = response()->json($data);
$doc_data = json_encode($data);
return view('doctracking.doctracking' ,['selluser'=>$selluser, 'doc_data' => $doc_data]); // return view('doctracking.doctracking',compact('doctracking') );
}
public function add_doc(Request $request)
{
$validation = Validator::make($request->all(),[
'name'=> 'required',
'start_date'=> 'required',
'end_date'=> 'required',
'amount'=> 'required',
'select_file'=> 'required |image |mimes:jpeg,png,jpg,gif,svg |max:2048',
]);
$this->validate( $request, [
'name'=> 'required',
'start_date'=> 'required',
'end_date'=> 'required',
'amount'=> 'required',
'select_file'=> 'required |image |mimes:jpeg,png,jpg,gif,svg |max:2048',
]);
if ( $validation->passes())
{
$user_id= Auth::user()->id;
$image = time().'.'.$request->file('select_file')->getClientOrignalExtension();
$request->file('select_file')->move(public_path('images'), $image);
$file=$request->file('select_file');
$uploaded_file_path='';
if($file!=null) {
$destinationPath = 'images/docktracking/';
$uploaded=$file->move($destinationPath,$file->getClientOriginalName());
$image= $uploaded->getPathName();
$img_name= $file->getClientOriginalName();
}
$data = array(
'name'=> $request->name,
'start_date' =>Carbon::parse( $request->start_date),
'end_date' =>Carbon::parse( $request->end_date),
'amount' => $request->amount,
'merchant_id'=> $user_id,
'image' =>$img_name
);
$doc_data= DocTracking::create($data);
return response()->json($doc_data);
}
}
}
?>
The model file is : 模型文件是:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DocTracking extends Model
{
protected $table = 'doctracking';
}
?>
the view file is :
视图文件是:
@foreach ( (array)$doc_data as $data )
{{ $data->id }}
@endforeach
i think that if {{$data->id}} get the data then every thing will work fine 我认为,如果{{$ data-> id}}获得数据,那么一切都会正常进行
You have to add the 'id'
key to your data array (of course return the array as 'doc_data'
like you do right now): 您必须在数据数组中添加
'id'
键(当然,像现在一样,将数组作为'doc_data'
返回):
$data = array(
'id' => $id
'name' => $request->name,
'start_date' => Carbon::parse( $request->start_date),
'end_date' => Carbon::parse( $request->end_date),
'amount' => $request->amount,
'merchant_id' => $user_id
);
return view('doctracking.doctracking', [
'selluser' => $selluser,
'doc_data' => $data
]);
Access it like this: 像这样访问它:
@foreach($doc_data as $data)
{{ $data['id'] }}
@endforeach
You should return a view in your index function with the parameter you want to pass. 您应该在索引函数中返回带有要传递的参数的视图。
$doc_data = DocTracking::all();
return view('doctracking.doctracking',compact('doc_data') );
and in view 并鉴于
@foreach($doc_data as $data)
{{ $data->id }}
@endforeach
First of all make sure that from your Controller, you are returning a $doc_data
object because in your current code, the return statement is commented out so I don't know what's with that. 首先,请确保从您的控制器返回了一个
$doc_data
对象,因为在当前代码中,return语句已被注释掉,所以我不知道这是怎么回事。
Second, inside your view, you are having code: 其次,在您的视图内部,您拥有代码:
@foreach ( (array)$doc_data as $data )
{{ $data->id }}
@endforeach
You are basically type casting $doc_data
into an array yourself and then trying to access it as an object :) of course you are getting Trying to get property of non-object
error. 您基本上是自己将类型
$doc_data
转换为数组,然后尝试将其作为对象进行访问:)当然,您正在获取Trying to get property of non-object
错误的Trying to get property of non-object
。
Either use it as object: 可以将其用作对象:
@foreach ( $doc_data as $data )
{{ $data->id }}
@endforeach
OR use it as array: 或将其用作数组:
@foreach ( (array)$doc_data as $data )
{{ $data['id'] }}
@endforeach
Can't have it both ways :) 不能同时拥有两种方式:)
You are using $data
variable to fetch DocTracking
data, 您正在使用
$data
变量来获取DocTracking
数据,
further you are overwriting $data
with array which does not contain id
进一步,您用不包含
id
数组覆盖$data
also you can convert result in array using toArray() function 您也可以使用toArray()函数将结果转换为数组
$data = DocTracking::get()->toArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.