Is it possible to remove variable from symfony url? - php

/category/{id}/{categoryName}
localhost:8000/category/2/tours-and-travels
localhost:8000/category/tours-and-travels //want to have this url.
Is it possible to remove id as shown above url?
Twig
<a class="nav-link text-white" href="{{ path('category_with_id',{'id': category.id, 'categoryName': category.name|slug('-') }) }}">
{{ category.name }}
</a>
I know we can do this with the help of unique slug, but is it possible with id?

Related

Trying to use Laravel policies into components

I'm trying to use Policies inside a Post Component, using Laravel. This is how I'm iterating through the posts in my page.
#foreach($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"/>
#endforeach
In the post.blade.php I'm trying to use a 'update' policy method to define which users can see the post:
#can('update', Auth::user(), /*What shoud I pass here?*/)
<a class="btn btn-success"href="{{route('posts.edit', $id)}}">
<i class="bi bi-pencil"></i>
</a>
#endcan
What should I pass as the second parameter of the policy? Normally, it would be a post variable. Since I'm already inside a post, I don't know to proceed.
You could check outside the component. Something like
#foreach ($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"
:canUpdate="Auth::user()->can('update', $post)"/>
#endforeach
#if ($canUpdate)
<a class="btn btn-success"href="{{ route('posts.edit', $id) }}">
<i class="bi bi-pencil"></i>
</a>
#endif

How to insert a variable into a Laravel Blade Template that is inside a HTML href tag for a web.root link

I am trying to creating a blog post with Laravel using views (blog.blade.php) inside HTML.
The code in my laravel web.php fetches the blog posts from my local mssql server looks as follow (and works):
Route::get('/blog/{id}', function($id){
$posts = DB::table('blog_posts')->get();
return view('blog', ['id'=>$id], ['posts'=>$posts]); });
In the laravel views dir home.blade.php my code works with hardcoded route: url('/blog/1')
The href link takes you to the full blog post
<h2>Blog posts:</h2>
#foreach ($posts as $post)
<p>{{ $post->title }} </p> <a href="{{ url('/blog/1') }}" >▲ Click here to view full post</a>
#endforeach
What I am trying to do is:
<p>{{ $post->title }} </p> <a href="{{ url('/blog/{{ $post->id }}') }}" >▲ Click here to view full post</a>
How do I get the $post->id that is the route (1, 2, 3 or 4) and comes from the sql data base saved to the blog row to work. I get the following error returned from my xampp php server when using {{ $post->id }} instead of hardcoding "1" .
D:\XAMPP Server\htdocs\cool-blog
ParseError
Unclosed '(' does not match '}' (View: D:\XAMPP Server\htdocs\cool-blog\resources\views\home.blade.php)
http://localhost:8000/
Pass second param to url() method
<p>{{ $post->title }} </p> <a href="{{ url('blog',$post->id) }}" >▲ Click here to view full post</a>
Ref:https://laravel.com/docs/8.x/helpers#method-url
Try this
<p>{{ $post->title }} </p> <a href="{{ url('/blog/'.$post->id.'') }}" >▲ Click here to view full post</a>

how to pass database variable in URL anchor tag

$permissions = DB::table('file_permissions')
->leftjoin('adminfiles','file_permissions.fileid','adminfiles.id')
->where('user_id', $user)
->get();
I am getting these file permissions as per user from the database and listing them in the left column of the admin panel
<ul class="treeview-menu">
#foreach($permissions as $permission)
<li><i class="fa fa-circle-o"></i> {{ $permission->filename }}</li>
#endforeach
</ul>
But in the output it makes url like this
http://localhost/laravel/$permission-%3Efileaddress
thanks in advance
Check the Laravel route parameters
https://laravel.com/docs/5.5/routing#route-parameters
Example (see docs)
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
Link like that
link
I think this will do the trick.
<a href="{{ url('') }}/{{ $permission->fileaddress }}">

Error in URL when i sharing the article on Twitter

when i'm use share from my web to twitter by:
<a href="https://twitter.com/share?
url=http://www.noor-journal.com/{{ $posts->category->slug.'/'.$posts->title }}
&text={{ $posts->author->name }} يكتب: {{ $posts->title }}
&{{ url($posts->category->slug.'/'.$posts->title) }}
&via=NoorJournals"
class="share-btn twitter"
target="_blank">
<i class="fa fa-twitter"></i>
</a>
the tweet send without URL Link because the URL it contains Arabic words and spaces like: this
i'm using PHP in Laravel.
Thank you

How to define a route for a link to go to destroy() function in Laravel 4?

I defined a resource in my routes like this:
Route::resource('shops', 'shopsController');
I need to create a link to delete an item
<a class="btn btn-xs btn-danger" href="{{ URL::to('shops/'. $row->id . '/destroy') }}" > </a>
but this URL requires me to define a new route like this:
Route::get('shops/{id}/destroy', 'shopsController#destroy');
So, how can I enforce the URL to go through the default route, through its resource, to get the destroy function??
I tried
href="{{ route('shops.destroy', $row->id ) }}" data-method="delete"
but I redirects me to show() instead!!!!
You can't delete the user via anchor tag href attr you need to delete it either with form or use Ajax.
{{ Form::open(array('url' => 'shops/'. $row->id)) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this User', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
Here's the
similar question
Update
Hope this would help you.
You can pass $row->id to the anchor Id attr like so
<a class="btn btn-xs btn-danger" id="{{$row->Id}}" onclick="delete_user(this.id) return false;" href="#" rel="nofollow" >Delete this entry</a>
and then use the below script to work with Destory method.
function delete_user(Id) {
var result = confirm("Want to delete?");
if (result==true) {
$.ajax({
url:'http://localhost:81/laravel/myapps/public/shops/'+Id,
type:"Post",
data: {'_method':'delete'},
success:function($msg){
alert($msg);
}
});
}
}
</script>
Try this
<a class="btn btn-xs btn-danger" href="{{ route('shops.destroy',array($row->id)) }}" data-method="delete" rel="nofollow" data-confirm="Are you sure you want to delete this?">Delete this entry</a>
laravel-delete

Categories