Missing required parameters for [Route: reviews.show] (from controller) - laravel

i want to route a new page from controller, i am getting data successfully when passing in route but getting error.
here is my particular part of controller -
$use = User::find($user->id);
$pos = posts::find($post->id);
//dd($user1, $post1);
return redirect()->route('reviews.show', $pos, $use);
in web.php part -
Route::get('p/{posts}/review/{user}','ReviewController#show')->name('reviews.show');
in show method -
public function show($posts, $user)
{
return view('posts.reviews.reviewshow', ['posts' => $posts], ['user' => $user]);
}

You have to use like this
return redirect()->route('reviews.show', ['posts' => $pos, 'user' => $use]);

route() calls in general take URL parameters as associative array. You are using helper function for it, but it still applies.
redirect()->route('reviews.show', ['posts' => $pos, 'user' => $use]);

When you pass 2 paramters to a route you send them as Array
return redirect()->route('reviews.show', [ $pos, $use ]);
This is your issue

Related

Laravel 8 multiple models to single view Error $address Not defined

I'm trying to create 2 rows in the DB using findOrNew() but when I use the ID from the Users model to create another model(Address) the programs returns undefined variable $address. I don't know if I'm using the correct approach or not. Bellow you can view my approach. Can you lead me to the right approach or where to find it?
2 models one view:
seeing what you have in your method is returning an undefined because it is not executing the findOrNew method correctly, check this link, maybe it will help you and this same
the second is that if you are passing the values by post everything will come to you in the $req parameter and only there then if you want to use the id you would have to access through $req->id if you send the data correctly
the third I see that in the view method you are passing 3 parameters when you should only pass two the first the name of the view the second the arrangement with the data that you will pass to the view
public function detail(Request $req)
{
$user = User::firstOrNew($req->id);
$user->user_type_id = 1;
$user->name = $req->name;
$user->last_name = $req->last_name;
$user->email = $req->email;
$user->password = Hash::make(Str::random(8));
$user->save();
$address = UserAddress::firstOrCreate(['user_id' => $req->id]); //or maybe $user->id
return view('user.detail', [
'user' => $user,
'adderss' => $address
]);
}
finally you may prefer to use the updateOrCreate method
public function detailV2(Request $req)
{
$user = User::updateOrCreate(
['id' => $req->id],
[
'user_type_id' => 1,
'name' => $req->name,
'last_name' => $req->last_name,
'email' => $req->email,
'password' => Hash::make(Str::random(8)),
]
);
$address = UserAddress::firstOrCreate(['user_id' => $user->id]);
return view('user.detail', [
'user' => $user,
'adderss' => $address
]);
}

Invalid ruote rule to catch request from vue-tables-2 component

Using vue-tables-2 component in my #vue/cli 4.0.5 app
I see that GET request generated
http://local-ctasks-api.com/api/adminarea/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
and I try in Laravel 6 Backend REST API to set route to catch it as :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
But I got 404 error,
Is my route invalid ?
UPDATED # 1:
Yes, var with “/api” was unaccesible. I fixed it and running request without “/adminarea”
http://local-ctasks-api.com/api/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
I moved route definition out of any block :
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
I got error in browser :
"error": "INCORRECT ROUTE"
with control action defined in app/Http/Controllers/API/Admin/ActivityLogController.php :
public function filter( $query, $limit, $ascending, $page, $column )
{
\Log::info('!!++ filter $this->requestData ::');
\Log::info(print_r( $this->requestData, true ));
Why error ?
Thanks!
I think you forgot put api on prefix
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'api/adminarea', 'as' => 'adminarea.'], function ($router) {
EDIT:
Don't put parameter on route like that, use Request instance
Route::get('activity-logs-filter,'API\Admin\ActivityLogController#filter');
and controller
public function filter(Request $request){
$query = $request->query;
$limit = $request->limit;
$ascending = $request->ascending;
$page = $request->page;
$column = $request->column;
dont forget use Illuminate\Http\Request; on your controller

Laravel Best Practice: Print Json response

I've been wondering what is the best way in Laravel to return a json array back to an ajax call. This is the way I'm working right now:
Route in web.php
Route::group(['prefix' => 'users'], function () {
Route::post('getOneTimeLink', [
'as' => 'adminUserOneTimeLink',
'uses' => 'AdminUsersController#createOneTimeLink'
]);
});
Controller in AdminUsersController.php
public function createOneTimeLink(){
$aResponse = [ 'someData' => 'someValue'];
// do some stuff here
echo json_encode($aResponse);
die()
}
but I think there is another way to return the call instead of adding the json_encode then die() the execution... but I don't know it yet. I've tried to search but haven't yet found an answer. I hope any of you can help me.
Thank you so much!
return response()->json($aResponse);
More information: https://laravel.com/docs/5.5/responses#json-responses
Please try to embed this logic into your code:
$response = array( 'status' => 'success', 'message' => $Info );
return response() ->json($response)->withHeaders($this->headerArray);

Laravel route optional parameter issue

My route is:
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2');
When i click the url link from blade,
Edit
it goes to the first route and calls Sample1Controller#sampleFn1. Why?? Please help..
When I click the link..I want to go the second route and calls Sample2Controller#sampleFn2. Any help?
Thanks in advance.
You need to add route where condition to parameters. First I quess is for stings, the second for integers:
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1')->where([
'name' => '[a-z]+',
'id' => '[0-9]+',
]);
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2')->where([
'id' => '[0-9]+'
]);
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Both URL's look the same for laravel, in this case, $name is being set to "1" and $id is being set to "edit".
You need to avoid ambiguity by moving the optional parameter to the end
And the status text one level back, in this case:
Route::get('members/edit/{id}', 'Sample2Controller#sampleFn2');
Route::get('members/{id}/{name?}', 'Sample1Controller#sampleFn1');
It's not validating name or id against any specific constraints so you're /1/edit qualifies... to use the same route definitions you can simply reverse the order of the definitions.
try the following:
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2');
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Route::get('/post-delete/{post_id}',[
'uses' => 'PostController#getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth'
]);
My controller is like this
public function getDeletePost($post_id)
{
$post = Post::where('id',$post_id)->first();
if(Auth::user() != $post->user)
{
return redirect()->back();
}
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'Sucessfully Deleted Taunt']);
}
and my blade.php is like this
Delete
the above code is for delete u c an try it for edit

How can i access this multi URL in laravel 4?

How can i access this multi URL in laravel 4.2.
http://localhost/new/public/library/course/1/First%20Video
My code is
Route::get('library/course/{id}', function($id){
return View::make('course')->with('id',$id);
});
Route::get('library/course/{id}/{video}', function($id, $video){
$array = array('id' => '$id', 'video' => '$video');
return View::make('course')->withvideos($array);
});
You are accessing the URL correctly, and it should reach your second route.
But you have a bug in your route:
// Your code
$array = array('id' => '$id', 'video' => '$video');
You shouldn't put single quotes around your variables -- it should be:
$array = array('id' => $id, 'video' => $video);
The single quotes force PHP not to resolve the two variables, so they are simply literal strings -- not what you want here.
Also, to pass the parameters to the view, you should adjust your code to use:
return view('course', $array);

Resources