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
CodePudding user response:
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()
CodePudding user response:
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.