尝试使用php5.6安装获取非对象Laravel的属性

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

You have to add the 'id' key to your data array (of course return the array as 'doc_data' like you do right now):

$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.

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.

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,

further you are overwriting $data with array which does not contain id

also you can convert result in array using toArray() function

$data = DocTracking::get()->toArray();