php Laravel 5.5 使用参数从刀片视图调用控制器函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48474160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 call Controller function from blade view with parameter
提问by Konstantinos Natsios
I have a slider of products and i'm trying to find the lowest price of each product.
我有一个产品滑块,我试图找到每种产品的最低价格。
So i'm trying, first of all, to successfully call a function from a Controller and pass the id
of the product and also print it out.
所以我首先尝试从控制器成功调用一个函数并传递id
产品的 并将其打印出来。
blade.php
刀片.php
<span class="text-bold">
@php
use App\Http\Controllers\ServiceProvider;
echo ServiceProvider::getLowestPrice($product_cat->id);
@endphp
</span>
Route
路线
Route::post('/getLowestPrice/{id}', 'ServiceProvider@getLowestPrice');
Controller
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ServiceProvider extends Controller
{
public static function getLowestPrice($id) {
return $id;
}
}
And I get an error
我得到一个错误
Parse error: syntax error, unexpected 'use' (T_USE)
Any idea why use
is not working here?
知道为什么use
不在这里工作吗?
回答by Joel Hinz
@Ali is obviously correct, and I suggest you accept his answer.
@Ali 显然是正确的,我建议您接受他的回答。
I'd like to add, though, that there's a service injection directive as well which was built for this exact purpose, and which is a little cleaner to use.
不过,我想补充一点,还有一个服务注入指令,它是为这个确切目的而构建的,并且使用起来更简洁。
@inject('provider', 'App\Http\Controllers\ServiceProvider')
<span class="text-bold">
{{ $provider::getLowestPrice($product_cat->id) }}
</span>
回答by Ali
you can't use use
keyword inside method
你不能use
在方法中使用关键字
you can write the full class path , like this
你可以写完整的类路径,像这样
<span class="text-bold">
@php
echo App\Http\Controllers\ServiceProvider::getLowestPrice($product_cat->id);
@endphp
</span>
回答by TheLastCodeBender
//Create a static type function in your controller.The function must be a static type function.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ServiceProvider extends Controller
{
public static function getLowestPrice($val){
// do your stuff or return something.
}
}
//Then Call the ServiceProvider Controller function from view
//include the controller class.
<?php use App\Http\Controllers\ServiceProvider;?>
//call the controller function in php way with passing args.
<?php echo ServiceProvider::getLowestPrice($product_cat->id); ?>
// Or call the controller function in blade way.
{{ServiceProvider::getLowestPrice($product_cat->id)}}
回答by Davide Casiraghi
It can be even easier, you can call it directly like this:
它可以更简单,您可以像这样直接调用它:
<span class="text-bold">
{{App\ServiceProvider::getLowestPrice($product_cat->id)}}
</span>
回答by Murad
A simple concept, to call controller function from view is: e.g. You have a Button in view with href="Your URL"
一个简单的概念,从视图调用控制器函数是:例如,你有一个带有 href="Your URL" 的按钮
<a href="/user/projects/{id}" class="btn btn-primary">Button</a>
Now define a route in web.php:
现在在 web.php 中定义一个路由:
Route::get('/user/projects/{id}', 'user_controller@showProject');
A function in respective controller will be looks like this:
相应控制器中的函数如下所示:
public function showProject($id){
$post =posts::find($id);
return view('user.show_projects')->with('post', $post);;
}
I hope this will help you.
我希望这能帮到您。