简体   繁体   中英

Getting a single value from controller to view in Laravel

Following is my controller function for editing a given project:

public function edit($id)
    { 
        $project = DB::table("projects")->where('id', $id)->get();
        dd($project);

    }

On dumping these values, I get the following array inside a collection:

Collection {#360 ▼
  #items: array:1 [▼
    0 => {#351 ▼
      +"id": 9
      +"createDate": "2017-06-29 12:39:17"
      +"updateDate": null
      +"projectName": "proj"
      +"projectspecs": null
    }
  ]
}

Now, I would like to pass the projectName into the view, but when I try passing $project into the view, and try printing the same, it gives me an 'Illegal offset type' error.

<input type="text" name="projectName" class="form-control" value="{{$project->projectName}}">

I am just trying to print the value of projectName into a textbox, what can be done here?

You should try this:

public function edit($id)
    { 
        $project = DB::table("projects")->where('id', $id)->first();
        return view('yourviewfilepath',compact('project'));

    }

on your controller:

public function edit($id)
{ 
 $project = DB::table("projects")->where('id', $id)->get();
 return view('viewfile',compact('project'));
}

on your blade file:

<input type="text" name="projectName" class="form-control" value="{{$project[0]->projectName}}">

====================================

alternatively, to make it more laravel-like:

public function edit($id)
{ 
 $project = Project::find($id);
 return view('viewfile',compact('project'));
}

then on your blade file:

<input type="text" name="projectName" class="form-control" value="{{$project->projectName}}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM