I'm having an issue with route to my controller. Route does not use method which is defined in a controller.
This is my route:
Route::get('movie/{$id}', [
'as' => 'getMovie',
'uses' => 'MovieController#getMovie'
]);
That's the controller:
class MovieController extends Controller
{
public function getMovie($id)
{
$movie = Tmdb::getMoviesApi()->getMovie($id);
dd($movie);
}
}
And last thing, the anchor tag
<a href="{{ Route('getMovie', $movie['id']) }}" data-toggle="movie-overview">
When i go into that link it gives me URL:
http://localhost/public/movie/395992
and error NotFoundHttpException
What's wrong with it?
Change the route to:
Route::get('movie/{id}', ....
Related
In my laravel application whenever I go to this route: admin/slider-groups I get the following error message:
We have following routes.
/slider-groups
/slider-groups/1
/slider-groups/1/sliders/create
/slider-groups/1/sliders/10/edit
blade.php
<a href="{{ route('admin::sliders.index') }}" class="mr-1">
web.php
Route::group([
'prefix' => 'admin',
'as' => 'admin::',
], function() {
Route::resource('slider-groups', 'Admin\SliderGroupController');
Route::prefix('slider-groups/{sliderGroup}')->group(function(){
Route::resource('sliders', 'Admin\SliderController');
});
});
SliderGroupController.php
public function index(Request $request)
{
if ($request->search) {
$sliderGroups = SliderGroup::search($request->search)->paginate(30);
} else {
$sliderGroups = SliderGroup::paginate(30);
}
if ($sliderGroups->count() == 0 && $request->search ) {
msg()->warning('دقت کنید', 'هیچ رکوردی یافت نشد.');
}
return view('slider::admin.groups.index', compact('sliderGroups'));
}
To note I'm not including all the html code of index.blade.php as it's too long the only blade code is from what i have pasted.
Since you are prefixing siderGroup, you need to pass the $sliderGroup->id to each route in
Route::resource('sliders', 'Admin\SliderController');
For example do:
Link
instead of:
Link
I am new in Laravel using version 5.8
I do not want to set route manually for every controller.
What i want is that if i give any url for example -
www.example.com/product/product/add/1/2/3
www.example.com/customer/customer/edit/1/2
www.example.com/category/category/view/1
for the above example url i want that url should be treated like
www.example.com/directoryname/controllername/methodname/can have any number of parameter
I have lots of controller in my project so i want this pattern should be automatically identified by route and i do not need to specify manually again and again Directory Name, Controller , method and number of arguments(parameter) in route.
try this:
Route::get('/product/edit/{id}',[
'uses' => 'productController#edit',
'as'=>'product.edit'
]);
Route::get('/products',[
'uses' => 'productController#index',
'as'=>'products'
]);
in the controller:
public function edit($id)
{
$product=Product::find($id);
return view('edit')->with('product',$product);
}
public function index()
{
$products=Product::all();
return view('index')->with('products',$products);
}
in the index view
#foreach($products as $product)
Edit
#endforeach
in the edit view
<p>$product->name</p>
<p>$product->price</p>
I want to add # symbol to url. Just like this. I have tried this in web.php.
Route::get('/#{user}', 'ProfilesController#show');
It did not work. Then I tried Route::get('/#/{user}', 'ProfilesController#show');
It worked but how can I remove the (slash) '/' between # symbol and user id ?
User model:
public function getRouteKeyName()
{
return 'nick';
}
ProfilesController:
public function show(User $user)
{
return view('profiles.show', [
'profileUser' => $user
]);
}
web.php:
Route::get('/#{user}', 'ProfilesController#show');
The way you have it should work. Check your controller code to make sure you are accepting the variable. The code below is what we have working on our site.
web.php
Route::get('/#{username}', [
'as' => 'profile',
'uses' => 'ProfilesController#show'
]);
ProfilesController.php
public function show($username){
...
}
You are better off refactoring your code.. the # sign is a reserved character as is stated here
My framework is Laravel 5.2, How to use {faq} in blade ?
Route is:
Route::get('help/{faq?}', ['as' => 'help', 'uses' => 'Site\Help\IndexController#index']);
URL is:
http://localhost:8000/help/general
I have get {faq} in url.
In php, if this url: http://localhost:8000/help?faq=general use $_GET['faq'] But not work $_GET in balde in laravel.
please guide me.
use request()->route('faq') or {{request()->route('faq')}} in blade
Route::get('help/{faq?}', ['as' => 'help', 'uses' => 'Site\Help\IndexController#index']);
means that $faq is an acceptable argument for Site\Help\IndexController#index
So when we look at that
<?php
namespace App\Http\Controllers\Site\Help;
use Illuminate\Http\Request;
class IndexController {
public function index(Request $request, $faq) {
return view('site.help.index', compact('faq'));
}
}
If Faq is not passed as an argument, then you can get it off of the request object.
class IndexController {
public function index(Request $request) {
$faq = $request->has('faq') ? $request->get('faq') : null;
return view('site.help.index', compact('faq'));
}
}
Now in the view site.help.index you can use $faq.
Alternatively, you can use - as indicated by #sam, request->route('faq') in your view. Make sure you check that it exists however, first:
{{ request()->has('faq') ? request()->get('faq') : '' }}
In Laravel blade use {{request()->get('faq')}}
Since faq an optional parameter, you should do this in index() action:
public function index($faq = null)
Then you can use $faq variable and check if parameter does exist with is_null($faq)
I've got a problem with using URL::route. There is a public function in my controller called AuthController called delete_character, here's how it looks:
public function delete_character()
{
$player->delete();
return View::make('index')->with('danger', 'You have successfully deleted your character!');
}
Also, I've created a named route:
Route::post('delete_character', array(
'as' => 'delete_character',
'uses' => 'AuthController#delete_character'
));
All I want to do is to execute the $player->delete. I don't want it to be a site, just when I click a button it's gonna delete the player.
I've also done the button:
<td><a class="btn btn-mini btn-danger" href="{{ URL::route('delete_character') }}"><i class="icon-trash icon-white"></i> Delete</a></td>
But I constantly get MethodNotAllowedHttpException. Any hints?
In my example, I am using GET request method (POST is used when form is being submited, for instance) to capture this action.
I pass ID of client I wish to delete in the reqeust URL, which results into URL in this form: http://localhost:8888/k/public/admin/client/delete/1 (You should post it from form, according to your example/request).
Not posting whole solution for you to force you to learn! My answer is not 100% identical to your situation, but will help, for sure.
// routes.php
Route::group(['prefix' => 'admin'], function(){
Route::get('client/delete/{id}', 'Admin\\ClientController#delete');
});
// ClientController.php
<?php
namespace Admin;
use Client;
class ClientController extends BaseController
{
...
public function delete($clientId)
{
$client = Client::findOrFail($clientId);
// $client->delete();
// return Redirect::back();
}
...
}
// view file, here you generate link to 'delete' action
delete