I cant seem to get my edit function to work in my resourceful controller. This is my controller:
class UserController extends Controller{
public function index()
{
return view('testindex');
}
public function test(){
return 'test';
}
public function edit(User $user){
return 'test2';
}
public function create(){
return 'test3';
}
}
And my routes:
Route::post('test','UserController#test');
Route::resource('/','UserController');
Which mean that edit should be in the resource controller.
Create works but edit isn't, it gives me a
NotFoundHttpException
This is the form:
Edit
And yes, the variable $id works and is showing in the url.
What am I doing wrong here?
This is because you're not naming the resource i.e.
Route::resource('user', 'UserController');
To get around this you will need to change you route to be:
Route::resource('/', 'UserController', ['parameters' => [
'' => 'user'
]]);
(The above will allow you to keep your urls the same).
Please note that you will have to keep this Route at the bottom of your file.
Hope this helps!
Related
I have install laravel in my local server. I have route with following code
Route::get ( '/', function () {
return view ( 'welcome' );
});
Route::get('/home', 'HomeController#index');
And controller file with following code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
echo 'test';
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
echo 'test1';exit;
return view('welcome');
}
}
But i am not able to access my view file name welcome.blade.php in resources/views directory.
i have try following urls in web browser but i got blank page every time.
URL 1:
http://localhost/StripeIntegration_laravel-master/public/home
URL 2:
http://localhost/StripeIntegration_laravel-master/public/index.php/home
URL 3:
http://localhost/StripeIntegration_laravel-master/public/
Please help me to solve out this.
I check your code if your current code runs then Your output display will appear like this:
testtest1
If you remove exit; from your index function then Your output display will appear like this:
testtest1 //And along with the code of your welcome file will also be printed here
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
echo 'test';
}
public function index()
{
echo 'test1';
return view('welcome');
}
}
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index')->name('home');
Please remove exit; from your index function
public function index()
{
echo 'test1';
return view('welcome');
}
You shouldn't have public in your URL.
Your webserver should be pointing at the public directory as the web root and the URL you need should be http://localhost/StripeIntegration_laravel-master/home
first of all, remove the echo 'test'; in the construct function, then I want to say to u that this code doesn't make sense at all: exit; return view('welcome');
in your controller. if you go to localhost/yourprojectname/public it should show the welcome view since the root in your route says it returns the welcome view :
Route::get ( '/', function () {return view ( 'welcome' );});
if u got any questions, feel free to ask
I want to fetch user based on id but it's returning null but User::all() is working correctly.
index and show methods in UsersController :-
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use DB;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth',['except'=>['index','show']]);
}
public function index()
{
$users=User::all();
return view('dev.index')->with('users',$users);
}
public function show(User $user)
{
return view('dev.show')->with('user',$user);
}
}
Route:-
Route::resource('devs','UsersController');
On view I have {{dd($user->name)}} and it's returning null on url public/devs/{dev}.
but working fine on index on url public/devs
This is because you are defining your base route like this:
Route::resource('devs', 'UserController');
This means that laravel will format the show method as follows:
Route::get('devs/{dev}', 'UserController#show');
Laravel will try to solve this dependency doind Implicit Model Binding and given that {dev} doesn't match any of your defined model classes, it will indeed return null.
So to solve it, define this match explicitly doing Explicit Binding. To accomplish this, go to your:
App/Providers/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::model('dev', App\User::class);
}
So now wherever Laravel reads the {dev} route parameter, will match it with the User model.
You don't initialize users for your entire controller.
Each function uses their own variables
First of all, I would reconfigure your route like so:
Route::get('devs', 'UsersController#show')->name('showUsers')
In your function show I would do the following
public static function show(){
$id = 1;
$users = User::where('id', $id')
return view('dev.show', compact('users'));
}
Been searching hours for solution online but could not find a solution to this problem:
BadMethodCallException in RedirectResponse.php line 228:
Method [guest] does not exist on Redirect.
This is my controller:
class MemberController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('member.home');
}
}
class SessionController extends Controller
{
public function __construct()
{
$this->middleware('guest', ['except' => 'destroy']);
}
public function create()
{
return view('session.create');
}
}
This is my routes/web.php:
Route::get('/member', 'MemberController#index');
Route::get('/login', 'SessionController#create')->name('login');
When I try to access 127.0.0.1/member, the above error pops up.
Any idea?
you are setting your /member route to point to create method, which is not exists in your member controller object,
this line:
Route::get('/member', 'MemberController#create');
you may need to change it to :
Route::get('/member', 'MemberController#index');
OR
by changing your index method name in your member controller, or creating new method called create if you are using index method in another context:
public function index()
to be :
public function create()
I am working on a Laravel 5.1 based system. I have a route resource:
Route::resource('applicant', 'ApplicantController');
So as we expect it has the following functions in the controller:
index, create, store, edit, update, delete
And what I want is to apply the middleware auth in the index function only. Normally, if you want to apply Auth on the entire controller, you need to do:
public function __construct()
{
$this->middleware('auth');
}
But when I remove it and just do:
public function index()
{
$this->middleware('auth');
return view('applicant.index');
}
It doesn't work. I've done this before and works fine.
This is in my ApplicantController I want the create function to be public and only apply login authentication on index. I won't be using edit, update, delete
can you try
public function __construct()
{
$this->middleware('auth', ['only' => 'index']);
}
You can also do the reverse
public function __construct()
{
$this->middleware('auth', ['except' => ['create', 'store', 'edit', 'update', 'delete']]);
}
I am studying Laravel 5 as my new framework and I am following the video in Laracast and I got some weird error. I am displaying simple view in my controller but all I got is this error:
ModelNotFoundException in Builder.php line 125: No query results for model [App\Article].
Here's a bit of my code:
Route:
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::get('articles', 'ArticlesController#index');
Route::get('articles/{id}', 'ArticlesController#show');
Route::get('articles/create', 'ArticlesController#create'); //returns error page
ArticlesController.php
<?php namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ArticlesController extends Controller {
public function index() {
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id) {
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
public function create() {
return 'Hello World'; //display error messages
}
}
So I just confused because when I try to access the create() method the Laravel also read the show() method.
Is this correct? So in the route list the Laravel will read it's routes from top to bottom?
So in my route in order to prevent the error I should put first the route of create before the show?
So it should be like this?
Route::get('articles', 'ArticlesController#index');
Route::get('articles/create', 'ArticlesController#create'); //make it first?
Route::get('articles/{id}', 'ArticlesController#show');
ModelNotFoundException is a db exception fired from the findOrFail method if the model your trying to find don't exist
check this redirect-if-model-doesnt-exists-modelnotfoundexception-doesnt-work-for-me
Because the /create also triggers the show url /{any}
You should do what you said, put the create route over the show route, or delegate that boring job using
Route::resource ('article', 'ArticleController')
Therefore you are getting a modelNotFoundException, because you are looking for an article with id = 'create'