Roshanay's avatar

Trying to get property of non-object error php laravel

I am making a function in the controller to register chef, a type of a user. The user can upload a picture as well. Following is the code for that.

{

    $chef = new chef();
    $chef->name = $request->name;
    $chef->company = $request->company;
    $chef->email = $request->email;
    $chef->password = $request->password;
    $chef->address = $request->address;
    $chef->number = $request->number;
    $file = Input::file('picture');
    $file -> move ('images', $file->getClientOriginalName());
    $chef->picture = $request->file('picture');
    echo '<img src"images/'. $file->getClientOriginalName(). '"/>"';

    $chef->save();
    return view("checkimage")->with('chef', $chef);

}

It's working perfectly fine till here. The next step is to display the users.

Following is the code for that blade.

{ @foreach($chef as $chefs) {{ $chefs->name }}

@endforeach } However, it's showing an error. Trying to get property of non-object. I have tried everything, but it's not being fixed. Please tell me a solution.

0 likes
2 replies
Snapey's avatar

show instead the code for the controller where you get all the chefs, and the blade file

use three backticks ``` before and after the code block

sam78640's avatar

As far as i can see, you are passing a $chef to the view which is not an array therefore you can not loop through it as its a single items however you can render the properties of $chef.

Such as in the view you would do something like this:

<h1> {{ $chef->name }} </h1>

and that will give you the Chef's name which you are passing through without using @foreach.

Please or to participate in this conversation.