I got a problem on phpunit
I'm design route as
/v1/outlet/{outlet_id}/test
Route::get('outlets/{outlet_id}/test', ['as' => 'test'], function(){
return app('request')->route('outlet_id');
});
It's working when i call it in postman or brower
but in phpunit show out as error
Call to a member function parameter() on array
test code
$req = $this->call('GET', '/v1/outlets/1/test');
$this->assertResponseStatus(200);
You have outlets plural in your test but outlet singular in your route definition.
Please remove the code use WithoutMiddleware from your testing class if it is there and try.
Related
I just want to Pass a Variable to the views about page.
This is the controller file
public function about(){
$name ="maneth";
return view::make('about')->with('name', $name);
}
This is the about page
#switch($name)
#case(1)
#break
#case(2)
#break
#default
#endswitch
This is the web file
Route::get('/about',function(){
return view('about',[PagesController::class, 'about']);
});
The Error is $name is undefined
I'm Using Laravel Framework 8.75.0
and PHP 7.3.33
You are using callback in router's file and there you not sent the name variable,
You can bind controller and router file using :
Route::get('/about', [AboutController::class, 'about']);
Your controller action is never being executed as your route definition is returning a view directly.
Change your route so that it calls your controller and action.
Route::get('/about', [AboutController::class, 'about']);
As long as it doesn't require any real logic (database query etc.) you can do it with a closures in your route. Otherwise you have to call the controller from your route. This would look like this:
Route::get('/about', [AboutController::class, 'about' ])->name('about');
And this would be thee closures style:
Route::get('/about',function(){
$name = 'Slim Shaddy';
return view('about', ['name' => $name]);
});
In previous versions of Laravel I was using something like this in controller in show function
Route::resource( 'our-project', 'ProjectController' );
public function show( Project $project ) {
return view( 'portalComponents.projects.projectDetails', compact( 'project' ) );
}
I was trying the same in laravel 5.8 but the $project attributes comes empty.
Route model binding won't work for our-project/1 because laravel can't infer the model. It tries to bind the our-project placeholder to a variable that has the name name in the show method. That argument doesn't exist. Because if this the $project variable stays empty.
the following resource would work:
Route::resource( 'projects', 'ProjectController' );
because this uses the project placeholder in routes. Check the output from php artisan route:list
It is also possible to have the same resource with different prefixes:
Route::resource('projects', 'ProjectController');
Route::group(['prefix' => 'admin'], function () {
Route::resource('projects', 'ProjectController');
});
the first one is /projects/1 and the second one is /admin/projects/
For the sake of completness and as alternative to #MaartenDev right answer, if you want to define the name of the parameter used with a resource route you can use the parameters() function, i.e.:
Route::resource( 'our-project', 'ProjectController' )
->parameters(['our-project' => 'project']);
I'm developing an application with Laravel 5.8. In my application, I have a controller that handles backend articles, and it works. I want to display my user-side information in such a way that a user can click on a link and see the detail of an article. For that, I have created a new controller a with a new namespace for the function show my redirection of navigation in different page does not focus that it is en route or URL with Laravel 5.8. Below is the function.
namespace App\Http\Controllers\Cybernaut;
use App\History;
use App\Http\Controllers\Controller;
class HistoryController extends Controller
{
public function show($id)
{
$history = History::find($id);
return view('show_history', compact('history'));
}
}
At the level of the home page I wanted to have my links like these:
<li><a data-hover="history" href="{{route('history.show',$history→id)}}"><span>history</span></a></li>
Error
ErrorException (E_ERROR) Property [id] does not exist on this
collection instance. (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
And here is the route used for the show function.
Route::group(['namespace'=>'cybernaut'], function (){
Route::get('/history/{slug}','HistoryController#show')->name('history.show');
});
Try after modifying the thing I have these at the route level now.
Route::get('/', 'FrontController#index')->name('index');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin/dashboard', 'DashboardController#index')->name('admin.dashboard');
Route::group([], function () {
Route::get('/history', 'HistoryController#index')->name('history.index');
Route::get('/history', 'HistoryController#create')->name('history.create');
Route::get('/history/edit', 'HistoryController#update')->name('history.update');
Route::get('/history', 'HistoryController#destroy')->name('history.destroy');
});
Route::group(['namespace' => 'cybernaut'], function () {
Route::get('/history/{history}', [
'as' => 'show',
'uses' => 'HistoryController#show'
]);
});
At the level of the homepage I wanted to put my link like those here now;
#foreach($history as $history)
<li><a data-hover="history" href="{{url('/history/'.$history->id)}}"><span>history</span></a></li>
#endforeach
I have this error now:
Trying to get property 'id' of non-object (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
I want an internaut to be able to navigate between the pages.
You have a conflict of variables on your homepage.
#foreach($history as $history)
should be
#foreach($histories as $history)
where $histories is filled in in your FrontController.
$histories = History::all();
When actually getting your single history object, I agree with Sapnesh's answer that you best doublecheck whether or not the object actually exists.
The error occurs because find() returns NULL when a model is not found.
find($id) takes an id and returns a single model. If no matching model
exist, it returns null.
findOrFail($id) takes an id and returns a single model. If no matching
model exists, it throws an error.
In your show() method,
Change:
$history = History::find($id);
To:
$history = History::findOrFail($id);
I have this route:
Route::get('/', function () {
return view('index');
});
Route::resource('admin', 'EmployeeController');
I have model Employee and EmployeeController( with empty resource methods)
Error : Call to undefined method Illuminate\Routing\ResourceRegistrar::addResourceEmployee()
What is wrong with my code? I have used the same approach in other project and it worked.
Route::resource('admin', 'EmployeeController');
is attempting to bind to a model named Admin.
Route::resource('employees', 'EmployeeController');
should work with the model you have. To make it work with admin, name the resource parameter.
Route::resource('admin', 'EmployeeController', ['parameters' => [
'admin' => 'employee'
]]);
edit
Did you reference something outside the Laravel docs to use AddResourceEmployee(). Seems like a custom solution to me.
https://stackoverflow.com/a/16661564/320487
I'm new to Laravel and am building a simple web application. I'll show what code I'm using next and then I'll explain my problem.
Here's how my form starts in the view login.blade.php:
<?php
//build the form
echo Form::open(array('action' => 'AuthenticationController#authenticateUser'));
And here's what the route for the home page:
Route::get('/', function() {
return View::make('login', array('page_title' => 'Log in to MANGER'));
});
Finally, here's the authentication controller (for now it's a simple redirect to simulate login):
class AuthenticationController extends BaseController {
public function authenticateUser()
{
//retrive from database
return View::make('hr', array('page_title' => 'MANGER Login'));
}
}
My problem is, I'm getting an error on login.blade.php. saying: Route [AuthenticationController#authenticateUser] not defined. (View: /opt/lampp/htdocs/manger/app/views/login.blade.php)
How is the error about a route when I've defined a controller instead? And also, how can this be fixed? Please excuse any noob errors and thanks a lot in advance! :-)
You still need to define your route like this:
Route::post('authenticate', ['uses' => 'AuthenticationController#authenticateUser']);
Otherwise it won't know what method to use, or the url to create.