Laravel Data passed to View become a request - php

I have this annoying error in laravel where my HomeController passing a data to view changes it to an route request.
As you can see here, I am simply passing a string from my HomeController#index ...
public function index()
{
$user = Auth::user();
return view('home', ['char' => 'test']);
}
then there would be a GET request at the console equivalent to the data.
As you would have guessed, anything I pass here would trigger a GET request. Let's try passing an empty stdClass Object.
public function index()
{
$user = Auth::user();
return view('home', ['char' => new \stdClass()]);
}
and the same thing happens.
I am passing BTW to a vue component and but displaying it directly in .blade.php doesn't cause this error.
#extends('layouts.app')
#section('content')
<character-page :user="{{ json_encode($char) }}"/>
#endsection
You might say that it is probably the vue component making the GET request but no, I totally have no http request in my vue compo, it is just an innocent template-style-script component.

Related

404 a page not found in laravel

I'm getting a 404 page not found when trying to use the 'users/{id}' route, the route
leads to the ProfilesController#edit method
Profiles Controller:
public function edit($id){
$user = User::findOrFail($id);
return view('profiles.edit', compact('user'));
}
here is the routes in my web.php;
Route::get('/users/{id}', 'ProfilesController#edit')->name('user.edit');
Route::put('/users/{id}/update', 'ProfilesController#update')->name('user.update');
and i have a edit.blade file in my profiles folder
Change route to this, the call users/{id} instead of profile/{id}. Otherwise you can stick with your approach by using the second option below.
Route::get('/users/{id}/edit', 'ProfilesController#edit')->name('user.edit');
Route::put('/users/{id}', 'ProfilesController#update')->name('user.update');
Also you are trying to access the route profile/{id} but which for my undestanding it is not defined. You either will have to change your route to:
Route::get('/profile/{id}', 'ProfilesController#edit')->name('user.edit');
Route::put('/profile/{id}/update', 'ProfilesController#update')->name('user.update');

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

rerouting after calling a function in Laravel 5

I'm getting my feet wet with Laravel 5 and I need a little advice on Routing.
I'm running in to a problem over and over and I feel like I'm definitely not grasping the proper way to do this.
Lets say I have a route:
Route::post('viewarticles', 'LoginController#user_check_article_list');
This is where I am taken after I login.
This route shows me my articles that I can edit. On the page this route takes you to I have a list of articles and delete buttons next to them. When I click delete I have it linked to
delete/{{ $article->id }}
I then have a route:
Route::get('delete/{id}', 'ArticleController#deleteArticle');
Which then goes to this function:
public function deleteArticle(Request $request, $id) {
$article = Article::find($id);
$article->delete();
$articles = Article::latest('published_at')->Published()->get();
return view('admin.article_edit_list',['articles'=>$articles]);
}
The last two lines of this is me rebuilding that article list and then loading the view.
THE PROBLEM: In my url the page is still ../delete/someid
I really just need a way to transfer back to the routes.
Instead of returning a view, return a redirect. This will cause the url to change:
public function deleteArticle(Request $request, $id)
{
$article = Article::find($id);
$article->delete();
return redirect('/some-url');
}
Or if using named routes:
return redirect()->route('some.route.name');
Source
so if your url is staying at /delete/{id} you need to redirect to the the display articles view
public function deleteArticle(Request $request, $id) {
$article = Article::find($id);
$article->delete();
return \Redirect::to('show_articles.view');
}
how do you send the delete request ? is it via ajax?
if so you need to redirect to your view in the success method of your ajax call using js / most likely jQuery?
.success(function(){
location.href = '/your/new/view';
});
its worth checking your variables are getting through to your controller as well
public function ...
echo $id; exit(); // or dd($id);
and checking your collection loaded
$articles = ...
dd($artcles);
retrun view(...
also if you go in to app/config/app.php and set debug=true you should get an error stack which will help debug the issue. Sorry for the delay internet went down before I could finish typing.

Laravel 5 route giving error "This webpage has a redirect loop"

I am using Laravel 5 and have declared a route that corresponds to a controller action but it gives an error of "This webpage has a redirect loop" and net::ERR_TOO_MANY_REDIRECTS in case of ajax calls.
My route is:
Route::get('getsubcategories/{id}', 'HomeController#getsubcategories')
->where('id', '[0-9]+');
Controller:
public function getsubcategories($id){
return "abc";
}
I don't know where the problem resides. Any suggestions ?
If you are using Form Request, make sure you are not injecting the class in the constructor.
My mistake was this:
public function __construct(SupplierFormRequest $supplier)
{
$this->supplier = $supplier;
}
instead of injecting the interface there.

laravel route parameter to controller and returning JSON

If I have a route in Laravel
Route::post('/user/{user}/project/{project}/git-add', 'GitController#stageFiles');
How do I access the user and project variables from the controller function being called?
Also, do I need to specify that I am returning a JSON object in the routes file, or is that all taken care of in the controller?
For following route:
Route::post('/user/{user}/project/{project}/git-add', 'GitController#stageFiles');
You need to create stageFiles method in GitController and from your stageFiles method:
public function stageFiles($user, $project)
{
// $user && $project both are available in this method as parameters
}
This is how you access them:
$user = Input::get('user');
$project = Input::get('project');
And Laravel will understand your json just fine.

Categories