How to handle php artisan routes? - php

I´ve got a server running laravel.
If I type in the php artisan route:list command, it displays all routes as usual.
Now if I try using Postman to execute GET, POST or DELETE requests, it doesn´t work because I´m not sure what goes into the curly brackets at the end of the URI´s. Im running a REST API btw. Can someone help me?

It is a some sort of values that is used by controllers. It could be primitive like string or int, or an id from database that will be converted by Laravel to object and injected in your controller method.
In example, after sending GET to /users/1 (route: users/{user}), in your controller you will get an access to object User, that has an id of 1 in your database.
Take a look at route parameters in docs: https://laravel.com/docs/5.8/routing#route-parameters

Any specific error it gives while accessing the route through postman.
You can specify your route as below.
Route::get('/test/{id}', function($id){
dd('ID is : ' . $id);
});
Let me know if this helps..

generally is an ID like some int or string, but you can do whatever uri you like in ur API just like web.routes.

Related

how to use DD() method in Laravel projects?

I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}

Laravel 8 Testing Method Illuminate\Http\JsonResponse::getSession does not exist

How to get the updated session data from a JsonResponse during test, I want to make use of the updated session data to my next series of test.
I am using an api route, when i make use of $response->dumpSession() the session can be traced. But when I make use of $response->getSession()->all to pass this values to my next test request returns me a error.
when I tried to look for the property of the $response cannot find the property of session
Thanks in advance.
There is no getSession method on the TestResponse class. But instead you can use
app('session.store')->all();
to achieve the same thing. This what the dumpSession uses under the hood also.

Fetching MySQL data from AngularJS UI

I have just worked in the front-end part alone. So I am not sure how to connect or fetch data from a database. If I am using a tomcat server will I be able to do that using PHP?
I am thinking of creating the project in eclipse. I am not sure how to connect that with PHP either.
if you will use Spring as back-end than you need to create model, controller and DAO. Also, you have to create connection to DB. In controller, you have to create method that will call query from DB. After you've created back-end you have to call http.get method inside of js.
e.g.:
$http.get('/user/retrieve/' + $scope.authorization.userName).success(function(data) {
$rootScope.getName = data;
})
.error(function(data){
$rootScope.getName = $scope.authorization.userName;
});
}
After you've created http get method, if call was successful data won't be empty, so you can store it into $scope variable.
To show values of $scope just put double curly brackets around name of $scope like this {{ getName }}
There are plenty of tutorials how to get data from mysql using Spring or php.
I'm giving you some insight by posting link of tutorial: http://websystique.com/springmvc/spring-mvc-4-angularjs-example/
Hope this will help you!

Laravel 4 shows MethodNotAllowedHttpException

I am getting this error at the time of when i hit the address bar ,
I fetch data from database and send to the view, that view's working great, but when I hit the address bar tab it shows
Symfony \Component\HttpKernel\Exception\MethodNotAllowedHttpException
any help will be great for me...
In my controller:
public function getpiece()
{
$reportno = $_POST['reportno'];
$data = DB::table('reportno')->where('reportno','=',$reportno)->get();
if(empty($data)) {
return \Redirect::to('dashboard/client/piece')
->with('status','error')
->with('message','Data Not Available');
} else {
return \View::make('dashboard.client.piecewisereport')
->with('data',$data);
}
}
It doesn't work, because when you hit enter in the address bar, it is trying to use a GET instead of a POST, but it finds only a POST route. (Route::post).
To solve this, either do:
Route::any('getpiece','Controllers\Domain\Client\ClientController#getpiece');
And then I'd advise to use something like
if (Request::isMethod('post'))
{
//
}
and
if (Request::isMethod('get'))
{
//
}
to wrap your code. More on this can be found in the Request Information docs
And an alternative method is to address the Controller directly from the routes, as can be found here: Laravel 4.2 Implicit Controllers
Note: these documentation links link to Laravel 4.2.
I notice your controller method is called getpost but your accessing the $_POST variable.
I can't tell for certain without looking at your routes file but if your route uses Route::get('routename') then it is making use of the wrong HTTP method. If this is the case try changing it to Route::post('routename').
Execute php artisan routes in a terminal from the root of your project, it lets you check what are your registered routes.
PS: FYI, in case of Laravel 5 it would be php artisan route:list

passing variable through controller method attatched to view::composer in Laravel

I am trying to make commenting on my site modular, so I have attempted to use view::composer to run the queries that are required to populate the comment data whenever the comment view is called, then my plan was to include the comment view in any place i needed comments, and then connect it to the appropriate comment table in my database.
The functions I have made for commenting fit each of the comment tables I've created and I've made the view composer work, the only thing is now since the function to get all the comment data is being called by the view composer when the comment view is being set up, that function no longer has access to the id that was being passed to it ( normally it is in the route like: Route::get('/{id}', 'controller#method');).
I have been searching for hours trying to figure out how to pass the variable in, but with no luck.. Maybe I'm missing some really basic detail, Idk, but I can't figure it out and haven't found anything that solves the problem, please help..
It seems like it should be so simple, I've tried doing:
$var = Route::get('/{id}', function($id){ return $id; }
Which i thought might do it, but the result was an error saying whatever the response was couldn't be converted to string.
Option 1: Access the route parameter
View::composer('comment-view', function($view){
$id = Route::current()->getParameter('id');
});
Option 2: Use the view to access the id
In your controller: return View::make('view-name')->with('commentId', $id);
View::composer('comment-view', function($view){
$id = $view->commentId;
});

Categories