I want to build a blade view from 3 tables:
But, in browser, I have the error: "Class 'Product' not found".
Is there a solution to pass to the view this function (to find the name of the product or the service based on article_type and article_id)?
I was trying also with join query, but I couldn't put so many conditions in a single join query .. where article_type is "p", then join with "products" table ... or .... where article_type is "s", then join with "services" table.
Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
The main advantage of using the blade template is that we can create the master template, which can be extended by other files.
Related to the question in your answer:
You have multiple options to achieve this that are way better:
Let's assume you have a model which you pass to the view:
$model = Model::find(1);
View::make('view')->withModel($model);
Now in your Model you could have a function:
public function someFunction() {
// do something
}
In your view you could call that function directly:
{{$model->someFunction()}}
This is nice if you want to do something with the model (the dataset).
If not you can still make a static function in the model:
public static function someStaticFunction($var1, $var2) {
// do something
}
And then:
{{App\Model::someStaticFunction($yourVar1,$yourVar2)}}
Hope it helps.
I solve the problem. So simple. Syntax error.
But I also want to know how to pass a function with parameters to view....
want to use model in view as:
{{ Product::find($id) }}
you can use in view:
<?php
$tmp = \App\Product::find($id);
?>
{{ $tmp->name }}
Hope this will help you
In new version of Laravel you can use "Service Injection".
https://laravel.com/docs/5.8/blade#service-injection
/resources/views/main.blade.php
@inject('project', 'App\Project')
<h1>{{ $project->get_title() }}</h1>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With