I have a resource route:
Route::resource('product', 'ProductController#index', ['only' => ['index', 'show', 'destroy']]);
The index lists all items in the database:
public function index()
{
return view('product', ['products' => Product::all()]);
}
and at the moment the show just echos the ID:
public function show($id)
{
return 'Show '.$id;
}
if I go to url/product the correct data shows up.
if I go to url/product/{ProductID} the index page shows up... not the echo of the id.
Has anyone experienced this issue? Do you know if I have done something silly?
Remove the action name after the controller
Route::resource('product', 'ProductController', ['only' => ['index', 'show', 'destroy']]);
// -------------------------------------^
When using RESTful Resource Controllers, we only need to pass the controller name and it will stub the action itself.
source: http://laravel.com/docs/5.0/controllers#restful-resource-controllers
Related
I have route like
Route::get('admin/selfcontacteditdata','SelfcontectController#edit')->name('selfcontectedit');
Route::post('admin/selfcontactupdatedata','SelfcontectController#update')->name('selfcontectupdate');
If i just go to my browser and right admin/selfcontacteditdata it redirect me to
admin/newsshowdata
And my index function is
public function __construct()
{
return $this->middleware('auth');
}
public function index()
{
request()->validate([
'email' => 'required',
'mobileno' => 'required',
'facebook'=>'required',
'google'=>'required',
'map'=>'required',
]);
$data = selfcontect::find(1);
return view('/admin/selfcontectedit',compact('data'));
}
And my middleware is
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
My rest admin routes are working fine.
I had the same problem but I was writing table name wrong and my file was not saved as .blade please check are you also doing the same thing and there is no meaning of validation in edit function your edit function must be like
public function edit()
{
$data = selfcontect::find(1);
return view('/admin/selfcontectedit',compact('data'));
}
and your function name should be edit
You should use Accept key not Content/type
You can't redirect through view, actually your are calling view.
Correct syntax is
return view('view_name',compact('data'));
If you want to redirect to any route you have to call like this
return redirect()->to('admin/selfcontacteditdata');
Redirect to a Route
If in your routes.php file you have a route with a name, you can redirect a user to this particular route, whatever its URL is:
app/Http/routes.php:
get('books', ['as' => 'books_list', 'uses' => 'BooksController#index']);
app/Http/Controllers/SomeController.php
return redirect()->route('books');
This is really useful if in the future you want to change the URL structure – all you would need to change is routes.php (for example, get(‘books’, … to get(‘books_list’, …), and all the redirects would refer to that route and therefore would change automatically.
And you can also use parameters for the routes, if you have any:
app/Http/routes.php:
get('book/{id}', ['as' => 'book_view', 'uses' => 'BooksController#show']);
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', 1);
In case of more parameters – you can use an array:
app/Http/routes.php:
get('book/{category}/{id}', ['as' => 'book_view', 'uses' =>
'BooksController#show']);
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', [513, 1]);
Or you can specify names of the parameters:
return redirect()->route('book_view', ['category'=>513, 'id'=>1]);
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 need to get 'mp3' value in controller !
( to check posts from mp3s type )
my post types :
video, album , mp3
(web.php)
Route::group(array('prefix' => 'mp3s'), function($pt) {
Route::get("/", "PostController#archivePosts");
Route::get("mp3/{slug}", "PostController#singlePost");
});
Route::group(array('prefix' => 'albums'), function($pt) {
Route::get("/", "PostController#archivePosts");
Route::get("album/{slug}", "PostController#singlePost");
});
Route::group(array('prefix' => 'videos'), function($pt) {
Route::get("/", "PostController#archivePosts");
Route::get("video/{slug}", "PostController#singlePost");
});
#danial dezfooli
To Get Prefix value you can inject Request Dependency inside controller's method as below.
public function index(\Illuminate\Http\Request $request){
dd($request->route()->getPrefix());
}
or you can do in another way also
public function index(){
dd($this->getRouter()->getCurrentRoute()->getPrefix());
}
For more reference you can refer : Laravel 5 get route prefix in controller method
Route::get("mp3/{slug}", "PostController#singlePost");
In PostController, you can get it like
public function singlePost($slug) {
dd($slug)// to check slug value
}
I have a simple User - Department relationship. My User Model has the following
public function department() {
return $this->belongsTo('App\Department', 'departmentId');
}
And my Department Model has
public function user() {
return $this->hasMany('App\User');
}
At the moment I am working with the departments side of things. My index function looks like the following
public function index() {
$departments = Helper::returnDepartmentsFromLdap();
return view('departments.index', compact('departments'));
}
What it basically does it gets all the departments from LDap (Active Directory) and displays them. On the index page for departments, I have
{!! link_to_route('departments.updateDepartments', 'Update Database', null, array('class' => 'btn btn-info')) !!}
So the database can be updated if new departments are added to our server. I do not have a create function as it is not needed.
Anyways, at the moment, my routes are like so
Route::model('departments', 'Department');
Route::bind('departments', function($value, $route) {
return App\Department::whereId($value)->first();
});
Route::resource('departments', 'DepartmentsController', ['except' => ['show', 'edit', 'create', 'delete', 'update', 'destroy']]);
Route::post('departments/updateDepartments', array('as' => 'departments.updateDepartments', 'uses' => 'DepartmentsController#updateDepartments'));
And in my updateDepartments function I am simply doing the following for now
public function updateDepartments()
{
dd("TEST");
}
If I click on the button on my index page to update the database, which should trigger the above, I am seeing a MethodNotAllowedHttpException.
Am I missing something obvious here?
try to use get:
because you can only pass data using get method with link link_to_route
Route::get('departments/updateDepartments', array('as' => 'departments.updateDepartments', 'uses' => 'DepartmentsController#updateDepartments'));
Route::post('departments/updateDepartments', ...) means you only allow POST requests on that route. Make sure the form method is POST instead of GET (default) on your index page
I have a multilingue website created using Laravel 4, and I have lot of pages such as : "policy, "terms", "how it works" in database, so to access thoses pages I use this route:
// Group by locale
Route::group(
array( 'prefix' => $locale ), function () {
Route::get('{slug}', array('uses' => 'PageController#show','as' => 'pages.show');
// Website routes
});
And then I search for the given slug and the current locale.
My is problem is that I can't add for example a page link in the footer because the slug is dynamic. so is there any solution to resolve that.
It's make a sense ?
Thanks
You are already catching the slug in
Route::get('{slug}', array('uses' => 'PageController#show','as' => 'pages.show');
part. all you need is to inject this slug into controller like this:
class PageController extends BaseController {
public function show($slug)
{
return 'showing slug ' . $slug;
}
}
and whatever value the route receive for {slug} part in route laravel will automatically inject that value into the controller.