-2

Hello Guys I Have This Function In My Model

public  function photo () {
      return  $this -> photo;
}

And I Called The Function From My Blade

{{$model = \App\Models\mainCategory::class}}

<td> <img style="width: 150px; height: 100px;" src="{{asset('assets/images/main-categories/' . $model -> photo() // Here I Have an Error `Call to a member function photo() on string`  )}}"></td>

I Have This Error Call to a member function photo() on string

  • Try to dd() $model variable and you see that this is a full name (with a namespace) of a mainCategory class, and it is a string. – Iftikhor Jul 30 '22 at 12:07

2 Answers2

0

This line {{$model = \App\Models\mainCategory::class}} is wrong. You have first to retrieve the model from the database, to use the model attributes. For example
{{$model = \App\Models\mainCategory::first()}} or
{{$model = \App\Models\mainCategory::find($id)}}.
Another thing to point, since you have the attribute photo already existing, why would write a method, that returns the same thing. You can use $model->photo directly.
Another thing, it's not recommended to retrieve data from the database, while you're in the view, you have the controller to do so.

Koussay
  • 174
  • 2
  • 8
0
use App\Models\mainCategory;

class YourController extends Controller
{

   public function index(){
      return view('your_view', ['category' => mainCategory::first() ];
   }
}

Then in your blade file, you do:

$category->photo

Instead of your current model->photo()

Bernard Wiesner
  • 961
  • 6
  • 14