Here is my urlto in laravel
{{ URL::to('forgot'); }}
It will point as mysite.com/forgot
How can i insert the value here
i.e.,
$value = '1';
{{ URL::to('forgot'); }}
And it should point mysite.com/1/forgot
How can i insert the value of $value before the forgot
You should concatenate your $value before your url
i.e.,
$value = '1';
{{ URL::to($value.'/forgot/'); }}
You can use named routes with parameters.
Example route:
Route::get('{value}/forgot', array('as' => 'forgot', function($value = null)
{
return "My value: " . $value;
}));
Now building your URL:
{{ route('forgot', 1); }}
You need to use like this check the documentation Documentation Here
$value = '1';
{{ URL::to($value.'/forgot'); }}
one way is to add a variable to your route
an example here my route is
Route::get('/user/{id}'
then at the destination if i call $id, i get the value that was entered in id.
so if i put /user/1 , $id =1.
this can be a issue however if there is nothing. you need to include code to handle if the $id has no value. another way to do it is
return View::make('searchresults', compact('challenges', 'ideas','users'));
here i have sent arrays with search results, and on the page searchresults if i call $ideas i get that array, this will also work with strings i believe.
Related
I have a url : http://localhost:8888/projects/oop/2
I want to access the first segment --> projects
I've tried
<?php echo $segment1 = Request::segment(1); ?>
I see nothing print out in my view when I refresh my page.
Any helps / suggestions will be much appreciated
Try this
{{ Request::segment(1) }}
BASED ON LARAVEL 5.7 & ABOVE
To get all segments of current URL:
$current_uri = request()->segments();
To get segment posts from http://example.com/users/posts/latest/
NOTE: Segments are an array that starts at index 0. The first element of array starts after the TLD part of the url. So in the above url, segment(0) will be users and segment(1) will be posts.
//get segment 0
$segment_users = request()->segment(0); //returns 'users'
//get segment 1
$segment_posts = request()->segment(1); //returns 'posts'
You may have noted that the segment method only works with the current URL ( url()->current() ). So I designed a method to work with previous URL too by cloning the segment() method:
public function index()
{
$prev_uri_segments = $this->prev_segments(url()->previous());
}
/**
* Get all of the segments for the previous uri.
*
* #return array
*/
public function prev_segments($uri)
{
$segments = explode('/', str_replace(''.url('').'', '', $uri));
return array_values(array_filter($segments, function ($value) {
return $value !== '';
}));
}
The double curly brackets are processed via Blade -- not just plain PHP. This syntax basically echos the calculated value.
{{ Request::segment(1) }}
Here is how one can do it via the global request helper function.
{{ request()->segment(1) }}
Note: request() returns the object of the Request class.
An easy way to get the first or last segment, in case you are unsure of the path length.
$segments = request()->segments();
$last = end($segments);
$first = reset($segments);
Here is code you can get url segment.
{{ Request::segment(1) }}
If you don't want the data to be escaped then use {!! !!} else use {{ }}.
{!! Request::segment(1) !!}
https://laravel.com/docs/4.2/requests
i'm trying to make a loop in php while using twig and inside this loop,
i am creating a parameter which contains the record from the database query.
The problem is that when I use the parameter in my HTML file, it only return 1 record from the while loop, even if there are 3 or 4 or even more..
This is the php code I have:
public function getWidgetsByName($name)
{
global $params;
$get = $this->db->query("SELECT * FROM profile_items
WHERE category = 'widget'
AND username = '". $name ."'");
if($get)
{
while($key = $get->fetch())
{
$params["profile_widget_name"] = $key['name'];
}
}
}
And this is the HTML parameter in my HTML twig rendered file:
{{ profile_widget_name }}
The paremeters just get rendered how they are supposed to be rendered:
echo $twig->render('app/views/'. $_REQUEST['p'] .'.html', $params);
And yes the $params variable is an array, in the config it file it first gets used as $params = array("..." => "..."); and I add things to this array by doing $params["..."] = "...";
So, I hope someone can help me.
Thanks in advance,
Best Regards.
At the moment, the value of $params["profile_widget_name"] is just one string. Every time you go through the while loop, you overwrite the previous value of the key with the current value.
So when you pass $params to Twig, the value of profile_widget_name is the value of name in the last row of the database to be selected.
I think what you want instead is for the value of profile_widget_name to be an array. Then every time you go through the loop, the current value of name is added to the array, instead of overwriting it.
You do this by doing something like:
$params["profile_widget_names"][] = $key['name'];
Now in your Twig template, you'll need to do something like:
{% for profile_widget_name in profile_widget_names %}
{{ profile_widget_name }}
{% endfor %}
Using Multiple Parameters
If you want multiple parameters to be on there, you can do this:
$params["profile_widgets"][] = [
'pos_x' => $key['pos_x'],
'name' => $key['name'],
];
And in Twig:
{% for profile_widget in profile_widgets %}
Name: {{ profile_widget.name }}
Pos X: {{ profile_widget.pos_x }}
{% endfor %}
I am making a simple search engine in which, If the selected list from the dropdown would match with the one inside the 'destinationto' column from the database then it would fetch all the items inside that row. But when I hit the find button, it would not return any item from the database. It would be giving me an empty array.
object(Illuminate\Database\Eloquent\Collection)[141]
protected 'items' =>
array (size=0)
empty
What have I done wrong?
Here are the snippets
OnewayflightControllers.php:
public function onewayflightresults()
{
$destinationto = Input::get('destinationto');
$results = Oneways::where('destinationto','=',$destinationto)->get();
var_dump($results);
}
public function onewayflight()
{
$onewaysfrom = DB::table('oneways')->distinct()->lists('destinationfrom');
$onewaysto = DB::table('oneways')->distinct()->lists('destinationto');
return View::make('content.onewayflight')->with(['destinationfrom'=>$onewaysfrom,'destinationto'=>$onewaysto]);
}
onewayflight.blade.php:
{{ Form::label('destinationto','To: ') }}
{{ Form::select('destinationto', $destinationto)}}
It's only a guess but you should make sure you have only one form element with name destinationto
If you have in form for example
{{ Form::label('destinationto','From: ') }}
{{ Form::select('destinationto', $destinationfrom)}}
{{ Form::label('destinationto','To: ') }}
{{ Form::select('destinationto', $destinationto)}}
If you think it's ok, you should add var_dump($destinationto); to your function to make sure value is what you expect
EDIT
I thought select will use values as keys but it's not so you should probably do something like that:
$onewaysfrom = DB::table('oneways')->distinct()->lists('destinationfrom','destinationfrom');
$onewaysto = DB::table('oneways')->distinct()->lists('destinationto','destinationto');
and not:
$onewaysfrom = DB::table('oneways')->distinct()->lists('destinationfrom');
$onewaysto = DB::table('oneways')->distinct()->lists('destinationto');
so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))
maybe someone know, how can I get variable with name "foo" from blade template get to static function which is used in this template.
For example I have:
<div class="collumns large-8">
{{ Helpers::setLetters('variable_name') }}
</div>
<div class="collumns large-8">
{{ $variable_name }}
</div>
Both divs should set same string because in setLetters function will be
return ${$name};
But, that should be instead $name, because in case about, will be error.
I use laravel 4.1
This doesn't go in your View, it should be in your controller.
Controller
$name = Helpers::setLetters('variable_name');
return \View::make('yourview', array( 'variable_name' => $name);