Laravel 5.3 url pass query string but not appearing on calling - php

I have grid with multiple column and one of it for me to press the edit button so I can pass the id and on the route I will call the sql and get the relevant values and call the edit view.
Below is my codes how I pass the id value.
<td><a href="{{url('/edittestdetails/',{{$test->ID}})}}>Edit</a></td>. I prefer the url to be edittestdetails?id=value? but now nothing is appearing when I call the url the paramereter is not appearing.

The correct syntax is:
<a href="{{ url('/edittestdetails/', $test->id) }}>Edit</a>
{{}} will be converted to the echo() clause, so you can't use {{}} inside another {{}} construction.
Also, property name is id by default and not ID.

<td>Edit</td>

Related

How to add a query argument when clicking a link, and then retrieve it in Hook callback

I am trying to create an archive page that shows only posts of a custom type made by a specific user. Without overriding the the Author archive page of said user, nor the posts archive page. Basically I wanna leave the default wordpress archives intact.
so far I fugured I would do something like
<a href="<?php echo add_query_arg('type', get_post_type(), get_author_posts_url(get_the_author_meta("ID")));?>">
posted by <?php the_author() ?>
</a>
this basically generates a link that goes like
www.example.com/author/john/?type=custom_type
now I am trying to retrieve the query argument that I added in the pre_get_posts hook callback function that I use in my functions.php file like so:
add_action("pre_get_posts",function($query){
///code to retrieve parameters goes here
}
the problem is that the only parameter I am getting in the $query paramter is the author username. As for the post type (aka the string that comes after "?type="). That part of the url does not show up in the query vars. I tried inspecting every property of the $query parameter that is being passed to the callback but I cant find that post type. I can only find the author.
Anyone know what I am doing wrong or how to fix it?
OK I realized what I was doing wrong
You cannot use a made up key name when creating a key value pair in query vars. In other words if you want to assign a value to a queryvar, the key of the var has to be one of the wordpress query var predefined names. The full list can be found here https://codex.wordpress.org/WordPress_Query_Vars
When using a made up name like "type" in my case, you can not retrieve that value using the get_query_var method because the key value pair is not added to the query vars in the first place. Instead you have to manually retrieve it from the super global variable $_GET[<variable_name>] so in my case I had to look in $_GET["type"]

Laravel retrieve id from URL

I would like to retrieve an id from a Laravel URL.
I've tried this:
$request->route('id');
But it would tell me that $request is undefined.
The url I am using is:
http://192.168.1.14/public/tasks/create?id=1
I've got a sidebar and within that I've got an if statement.
#if((request()->is('projects/1')) || (request()->is('tasks/create')))
<a href="{{ route('tasks.create') }}?id={{ $project->id }}">
#endif
I've hid the unnecessary code, but the second parameter is used for highlighting what page I am on in the sidebar. And the first for showing the create task option when I am on a project page.
Now, the if statement causes that its hidden ( so I can't see the error ( the error would say that there is no id but its true because it only receives the id by being on a project page with an id ))
Now, the task form on the page itself has a hidden input field with project_id so I can bind the task to the project.
Inside the value I have the $request->route('id') but it gives me an error that request is undefined.
Now I ask, how do I retrieve the id from the url, or else, can I retrieve the id another way? All the project and task blade files extend the project.index and the project.index extends the layouts.blade so its a layout within a layout.
Also, can I change the (request()->is('projects/1')) to something else so whatever project page I am on, the statement is true? For example 'projects/{project}'
Thanks for any help
Best way to do this is
You don't need to write 'create'
{{ app('request')->input('id') }}
or
{{ request()->get('id') }}
IF you are not sure how it works just
store in a variable and print it on view to cross-verify like this.
<?php
$id = app('request')->input('id');
//or
$id = request()->get('id');
?>
You can always check all request URL parameters like this.
$request = request();
print_r($request);
Create Route for getting id from url
Example:
Route::name('subscriptions.store')->post('/subscriptions/store/{id}', 'CreateSubscriptionController');

How to pass a VueJS variable into a Laravel blade route

I'm looping over a JSON array in VueJS and outputting each item to the screen but I need to create a link/route to a resource controller with the ID being returned for each row like so:
<tr v-for="item in searchResults.group">
<td>
<button type="button" class="btn btn-info btn-sm">Edit</button>
So I've tried putting the variable into the route like so #{{ item.id }} but get the error:
syntax error, unexpected '{' (View: /application/resources/views/admin/edit.blade.php)
The way I have done it is not the correct way obviously but I can't find anything in the documentation to achieve this.
EDIT:
Further input on this. The route function requires a second parameter, in this case, the ID of the item to edit. In pure PHP/Blade I have this and it works:
<button type="button" class="btn btn-info btn-sm">Reduce</button>
For the dynamic search page, I need to somehow pass that second parameter into blade/php from a vuejs variable but I just can't work out how to do it.
You can try this using backtick.
Its works for me. I hope it's helpful for you.
<a :href=`{{ route('admin.edit', '') }}/${item.id}`>
You are mixing two concepts that won't go together like this.
The blade template is rendered server-side, whereas the parts of your vue.js related markup will be parsed on the client side (e.g. "browser").
Because of that, referencing a property of item within a blade expression will fail (as it does).
<a href="{{ route('admin.edit', #{{ item.id }} ) }}"
route(...) refers to an expression that is related to blade, an item is supposed to be a part of your vue.js app.
What you need to do is to dynamically create the link for editing the ressource within your vue.js app once you loop your searchResult.group.
You could achieve this by injecting a "route-template" for editing the ressource into your vue.js app and bind the href property to a vue.js method, like this:
<tr v-for="item in searchResults.group">
<a v-bind:href="getEditLink(item.id)">Edit</a>
....
The according method getEditLink(id) then assembles the actual link based on the given id of item and the provided "route"-template.
You can try by appending the id to the end like so:
{{ route('admin.edit') }}/#{{ item.id }}
I'm guessing you are following a REST URI guides so that would work for you.
Use Like this
Click

Laravel 3 how to include variables in flash message url

I have a form which is used to write to a database. I managed to successfully complete everything except including php variables as part of the url in the flash message:
if($sale->save() && $updateowed)
{
return Redirect::to('/')->with('flash_notice','Sale record id '.$sale->id.' created. <a href=/printmemo/'.$sale->id.'>Print Cash Memo</a>');
}
The first $sale->id is displayed correctly. However, the $sale->id inside the href prevents the form form redirecting to '/' and shows a blank message. Checking the database reveals the $sale->save() is indeed successful. I kind of know it's the problem with how I use quotes, however I'm unable to solve the problem. Any help is much appreciated.
You are doing it wrong, you should only pass the data to the session and build the tag from your view, for example you may pass the $sale->id form the controller using something like this:
return Redirect::to('/')->with('sale_id', $sale->id);
Then in your view where you want to show the link using $sale->id build it like:
<?php if(Session::has('sale_id')): ?>
<a href=<?php echo "printmemo/" . $sale->id ?>>Print Cash Memo</a>
<?php endif; ?>
If you are using Blade then use blade syntax to echo the id.

Passing the value to the next page without a form

I have a page I am constructing and I need to pass in the values of the option dropdowns to the next page. The problem is that these dropdowns are not in a form.
http://posnation.com/test/pre_config/pre_config_step_2.html
Basically what i need to pass to the next page is that when i click "Proceed To Next Step" I need to pass the value of the type of field like "restaurant" and the number of stations "2" if the user selects restaurant and 2.
HTML:
<a id="proceed" href="foo.html">Proceed!</a>
JS:
$('#proceed').click(function() {
location.href = this.href +'?someVal='+ escape($('#my_select').val());
return false;
});
Working example that executes a formless google search: http://jsfiddle.net/CKcbU/
You basically just add what you want to the query string with javascript.
But really, if at all possible, you should use a form with method="get" which pretty much does this for you without any JavaScript at all.
Use a query string.
http://posnation.com/test/pre_config/pre_config_step_2.html?restaurant=The+Eatery&stations=2
In other words, pass them as part of the URL when calling the next page. The next page will be responsible for reading the query string and extracting the values out.
http://en.wikipedia.org/wiki/Query_string
I do not know what you are using to code in so I cannot be detailed regarding the mechanics of constructing the URL or parsing out the values from the query string on the receiving page.
Here is an article on doing it using JavaScript:
http://javascript.about.com/library/blqs.htm
You can use JavaScript for the same. Assign a onclick event with button of proceed and call a function. In this function use:
windows.location=url?rest=valuerest&opt=valueopt
I am not sure what you are working with, but with almost any framework that I know of you can pass a parameter as part of the url.
These would work fine.
http://posnation.com/test/pre_config/step_2
http://posnation.com/test/pre_config/step_3
Then, just grab the parameter and react accordingly.

Categories