How to redirect to a specific url - laravel

I have the following code:
public function numbergrouppost() {
$groupname = Input::get('groupname');
$no_of_members = Input::get('no_of_members');
$create = Group::create([
'group_name' => $groupname,
'no_of_members' => $no_of_members,
'user_id' => Auth::id()]);
return Redirect::route('grouppage');
The question is how do I redirect to the page that contains group_id (primary key) within the url. In other words I want to redirect to a url that looks like this:
'/grouppage/{id}'
I could create a function within my home controller but how would I access the $groupname variable(code below)? Or would it be best to query the most recent group created by the user?
public function groupname() {
$groupbutton = Group::where('user_id', Auth::id())->where('group_name', $groupname)->first();
return View::make('grouppage');
Route:
Route::get('/grouppage/{id}', array( 'as' => 'grouppage', 'uses' => 'HomeController#groupname'));

Assuming $groupId is id of the group, you can pass variable on route like this :
return Redirect::route('grouppage', ['id' => $groupId]);
Then, in your groupname function you can retrieve the id :
public function groupname($id) {
// get group by id
$groupbutton = Group::where('user_id', Auth::id())->where('id', $id)->first();
// pass the gruoupbutton to the view
return View::make('grouppage', ['groupbutton' => $groupbutton]);
}

You can redirect to your route by passing parameters. For example if your group id is 10, you can try something below.
return Redirect::route('grouppage', array(10));
in your case group_id can be retrieved using $create->group_id.
return Redirect::route('grouppage', array($create->group_id));
Please refer to http://laravel.com/docs/4.2/responses#redirects.

Related

Laravel model function best prickets

im new in Laravel , I have an issue as below
I make in category model query to check is category is exist or not
as below
public function scopeIsExist($query ,$id)
{
return $query->where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();
}
and my controller is
public function edit($id)
{
$dataView['category'] = Category::IsExist($id);
if(!$dataView['category'])
{
return view('layouts.error');
}else{
$dataView['title'] = 'name';
$dataView['allCategories'] = Category::Allcategories()->get();
return view('dashboard.category.edit')->with($dataView);
}
}
my problem is when I use method isEXIST if id not found it not redirect to error page but ween i remove ISEXIST AND replace it as below
$dataView['category'] = Category::where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();
it work well .
can any one help me
That's because local scope should return an instance of \Illuminate\Database\Eloquent\Builder. You should remove the first() in the scope and put it in the controller.
Redefine your scope like so:
public function scopeIsExist($query ,$id)
{
return $query->where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC');
}
In your controller edit method:
$dataView['category'] = Category::IsExist($id)->first();
You can have a look to the doc for local scopes https://laravel.com/docs/8.x/eloquent#local-scopes

how to pass a string instead of primary int key in route view

i want to pass a name in a route view in laravel.
for a example -
in the web.php
Route::get('p/{posts}/review/{user}','ReviewController#show')->name('reviews.show');
it passed the primary key, the id of the posts and user . i want to pass the user->name.
my show method -
public function show($posts, $user)
{
return view('posts.reviews.reviewshow', ['posts' => $posts], ['user' => $user]);
}
Where you call your route() method use route('reviews.show', ['posts' => $posts, 'user' => $user->name]).
I will suggest to rename {user} to {userName} to be more clear.
First you have to change your route
Route::get('p/{post}/review/{user}','ReviewController#show')->name('reviews.show');
Then In your controller use like this
public function show(Post $post, User $user)
{
return view('posts.reviews.reviewshow', ['post' => $post,'user' => $user]);
}

Laravel 5.5 - Show specific category from API response

I am returning an API response inside a Categories controller in Laravel 5.5 like this...
public function get(Request $request) {
$categories = Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
Now I am trying to also have the option to return a specific category, how can I do this as I am already using the get request in this controller?
Do I need to create a new route or can I modify this one to return a specific category only if an ID is supplied, if not then it returns all?
Better case is to create a new route, but you can also change the current one to retrieve all models if the parameter is not supplied. You first gotta choose which approach you will be using. For splitting it into multiple calls you can see Resource controllers and for using one method you can follow Optional Route Parameters
It will be much cleaner if you will create another route. For example
/categories --> That you have
/categories/{id} -> this you need to create
And then add method at same controller
public function show($id) {
$categories = Category::find($id);
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
But if you still want to do it at one route you can try something like this:
/categories -> will list all categories
/categories?id=2 -> will give you category of ID 2
Try this:
public function get(Request $request) {
$id = $request->get('id');
$categories = $id ? Category::find($id) : Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}

Laravel - Change URL parameters using GET

I have RESTful API built on Laravel.
Now I'm passing parameter like
http://www.compute.com/api/GetAPI/1/1
but I want to pass parameter like
http://www.compute.com/api/GetAPI?id=1&page_no=1
Is there a way to change Laravel routes/functions to support this?
you can use link_to_route() and link_to_action() methods too.
(source)
link_to_route take three parameters (name, title and parameters). you can use it like following:
link_to_route('api.GetAPI', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
If you want to use an action, link_to_action() is very similar but it uses action name instead of route.
link_to_action('ApiController#getApi', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
href text
with these methods anything after the expected number of parameters is exceeded, the remaining arguments will be added as a query string.
Or you can use traditional concatination like following:
create a route in routes.php
Route::get('api/GetAPI', [
'as' => 'get_api', 'uses' => 'ApiController#getApi'
]);
while using it append query string like this. you can use route method to get url for required method in controller. I prefer action method.
$url = action('ApiController#getApi'). '?id=1&page_no=1';
and in your controller access these variables by following methods.
public function getApi(Request $request) {
if($request->has('page_no')){
$page = $request->input('page_no');
}
// ...your stuff
}
Or by Input Class
public function getApi() {
if(Input::get('page_no')){
$page = Input::get('page_no');
}
// ...your stuff
}
Yes you can use those parameters, then in your controllers you can get their values using the Request object.
public function index(Request $request) {
if($request->has('page_no')){
$page = $request->input('page_no');
}
// ...
}

Laravel 5 - can't get parameters from redirect::route

I have this code in a controller:
$id = 1;
$name = 'Phil';
return Redirect::route('myroute')->with('id',$id)->with('name',$name);
Then in my routes file I have the following:
Route::get('test/{id}/{name}',array('as' => 'myroute', 'uses' => 'MyController#myFunction'));
And finally the function in MyController:
public myFunction($id,$name) {
return $name;
}
Instead of getting the content of the variable name printed 'Phil', I get the string '{name}'.
What am I doing wrong?
Thanks in advance
Pass the route parameters as the second argument to route():
return Redirect::route('myroute', ['id' => $id, 'name' => $name]);
->with() puts the items in the Input for the next request, rather than being route parameters.

Resources