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
Related
I am trying to chunk the results of a laravel collection in my blade template to have 3 columns of records expanding to as many rows as needed. What I have is...
#foreach(Auth::user()->apps()->chunk(3) as $chunk)
<div class="row row-grid gx-0">
#foreach($chunk as $app)
<a href="{{ $app->url }}" class="dropdown-item text-decoration-none p-3 bg-none">
<div class="position-relative">
<i class="fa-solid fa-circle-fill position-absolute text-theme top-0 mt-n2 me-n2 fs-6px d-block text-center w-100"></i>
<i class="fa-solid fa-{{ $app->icon }} h2 opacity-5 d-block my-1"></i>
</div>
<div class="fw-500 fs-10px text-white">{{ ucfirst($app->alias) }}</div>
</a>
#endforeach
</div>
#endforeach
But I am getting the error Too few arguments to function Illuminate\Database\Eloquent\Relations\BelongsToMany::chunk(), 1 passed in /app/storage/framework/views/7d3f05473ba9eb01ea58150355cc97e13e862587.php on line 1 and exactly 2 expected. Taking a look at the Laravel source in Github it appears as a callback function is required which handles the looping for each chunk but this only really works if done from within PHP itself, within a blade template I cant figure out how to to a chunk with a callback. All the information I have been able to find online for Laravel blade chunking shows that this should be working.
My controller method is...
$userApps = Auth::user()->apps();
$chunk = ceil($userApps->count() / 3);
return view('apps', ['apps' => $userApps, 'appsChunk' => $chunk]);
I have this Laravel application, in which I want to display the name of the logged in user in the pages.. Thing is, I'm getting the data in the web.php file, like this:
Route::get('/', function () {
$welcomeNames = DB::table('users')->get();
return view('welcome', ['welcomeNames' => $welcomeNames]);
});
And then, in my welcome.blade.php, I return the data like this:
#if (!Auth::guest())
#foreach($welcomeNames as $key => $data)
#if ($data->name == Auth::user()->name )
<tr>
<th>{{$data->name}}</th>
</tr>
#endif
#endforeach
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();" class="text-sm text-gray-700 dark:text-gray-500 underline">
Logout
</a>
<form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
#else
#endif
My question is: is this a safe practice? I don't know if getting the data from the DB in my route is safe. Thanks in advance!
It is safe as long as you do not show it somewhere where it doesn't belong. But its bad practice and shouldn't be done if not really needed. AND controllers are way more organised in any way.
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>
I have tried the ->limit(4) methods, but that doesn't seem to work
#foreach(auth()->user()->unreadNotifications()->limit(4) as $notification)
<li>
<a href="profile.html">
<div>
{{ $notification->data['message'] }}<span class="pull-right text-muted small">{{ time_elapsed_string($notification->created_at) }}</span>
</div>
</a>
</li>
#endforeach
You do not execute the query here:
#foreach(auth()->user()->unreadNotifications()->take(4)->get() as $notification)
Also, you shouldn't run queries in Blade files.
It's hard to say what unreadNotifications really returns but if it's collection you could use:
#foreach(auth()->user()->unreadNotifications()->take(4) as $notification)
and if it's relationship you could use:
#foreach(auth()->user()->unreadNotifications()->take(4)->get() as $notification)
Try this one
foreach(\Auth::user()->unreadNotifications->take(1) as $data){echo $data->id;}
$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 }}">