Image not displaying in view. from database - php

In my laravel project I tried to load images from database. But it displays the broken link. But when I inspect the image it shows the link. But when i clicked the link it shows 404 error.....
this is my codes
this is the code of view
.............
#extends('layouts.app')
#section('content')
<div class="d-flex justify-content-end">
Add Posts
</div>
<div class="card card-default">
<div class="card-header">Posts</div>
<div class="card-body">
<table class="table">
<thead>
<th>Image</th>
<th>Title</th>
</thead>
<tbody>
#foreach ($posts as $post)
<tr>
<td><img src="{{ asset($post->image) }}"></td>
<td>{{ $post->title }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
.............
and this is the code of controller
public function store(CreatePostsRequest $request)
{
//Upload the image
$image = $request->image->store('public/img');
//create the post
Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image
]);
//flash a message with seasson
session()->flash('success','Post created successfully');
//redirect user
return redirect(route('posts.index'));
}
and i linked the storage to the public folder. But still its not working

You need to give the absolute path and not use {{ asset... }}. Try something like this:
<img src="/xx/storage/app/public/FinalFolder/{{$post->image}}">

Related

Fetching and Printing data from Database in Laravel 8

Hi I just started working in laravel 8 and I would like to know how to fetch data in database and print data
I have a template of a table with a search bar to see and find user in our database
Here is our controller for the table
DashboardController
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function dashboard()
{
$dashboardTitle = "Dashboard";
$isCurrent = "dashboard";
return view('dashboard.index', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function profile()
{
$dashboardTitle = "Profile";
$isCurrent = "Profile";
return view('dashboard.profile', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function directory()
{
$dashboardTitle = "Directory";
$isCurrent = "Directory";
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function journal()
{
$dashboardTitle = "Journal";
$isCurrent = "Journal";
return view('dashboard.journal', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function files()
{
$dashboardTitle = "Files";
$isCurrent = "Files";
return view('dashboard.files', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
}
Here is the view
directory
#extends('dashboard.layouts.dashboard-layout')
#push('css')
<!-- Custom styles for this page -->
<link href="{{asset('dashboards/vendor/datatables/dataTables.bootstrap4.min.css')}}" rel="stylesheet">
#endpush
#section('content')
<!-- Page Heading -->
<h1 class="h3 mb-2 text-gray-800">Tables</h1>
<p class="mb-4">DataTables is a third party plugin that is used to generate the demo table below.
For more information about DataTables, please visit the <a target="_blank"
href="https://datatables.net">official DataTables documentation</a>.</p>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">DataTables Example</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<td></td>
<td></td>
<td></td>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
</div>
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
#endsection
#push('script')
<!-- Page level plugins -->
<script src="{{asset('dashboards/vendor/datatables/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('dashboards/vendor/datatables/dataTables.bootstrap4.min.js')}}"></script>
<!-- Page level custom scripts -->
<script src="{{asset('dashboards/js/demo/datatables-demo.js')}}"></script>
#endpush
Our table name is users and I want to print it in the table using a loop
Database
Add new rows in database if you haven't (position, office ...)
Controler
...
$users = User::all();
return view('dashboard.directory', [
'users' => $users
]
...
Blade
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->postion }}</td>
<td>{{ $user->office }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->start_date }}</td>
<td>{{ $user->salary }}</td>
</tr>
#endforeach
To retrieve all users in your db, you have to :
Dashboard Controller
public function directory(){
$dashboardTitle = "Directory";
$isCurrent = "Directory";
$users = User::all();
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent,
'users' => $users
]);
}
In Blade
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->postion }}</td>
<td>{{ $user->office }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->start_date }}</td>
<td>{{ $user->salary }}</td>
</tr>
#endforeach

No Image showing in a home Laravel

in laravel, i have to link the storage using php artisan storage:link so i can show the image that when user to post a article with a image, the image will be shown. but for now, the image didn't show up. How do i fix it.
here's the code from views/post/index.blade.php
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ asset($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
when the user post a image, the image will be stored in the linked storage that i've doing before. so here's the code from postscontroller.php if i change the 'posts' to 'post' it says error. because i didn't suitable for the function 'posts' that i write before.
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = $request->image->store('posts');
// create the post
$post = Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image,
'published_at' => $request->published_at,
'category_id' => $request->category,
'user_id' => auth()->user()->id,
]);
if ($request->tags) {
$post->tags()->attach($request->tags);
}
// flash a message
session()->flash('success', 'Post created successfully');
// redirect the user
return redirect(route('posts.index'));
}
if you guys see any problems that i didn't appeared here, just ask me. Thank you guys so much
If you are working with Laravel Storage it's recommended to use the storage functions to store, get or delete media files.
Use theses commands to do it:
To store:
Storage::disk(config('filesystems.default'))->put($path, $file);
To get:
Storage::disk(config('filesystems.default'))->url($file);
To remove:
Storage::disk('local')->delete($path);
Your code will be like this:
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ Storage::disk(config('filesystems.default'))->url($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
And:
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = Storage::disk(config('filesystems.default'))->put('posts', $request->image);
Note: Don't forget to check if your filesystem is setted like 'local' - if you are using local storage.

Laravel 5.8 Routed page not found 404 error

I am new to Laravel. I am learning Laravel from tutorial and I drive into one problem which I can't solve.
I think I have problem somewhere into Routing, but I can't find it
Funny thing is that if the href is {{route('tag.create'}}, then it goes to creating page, but when I need to use ID it's not working...
I had same functionality for posts and categories, but everything worked fine for those two. So I really need your help to see what I can't see. I have these files:
index.blade.php:
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<th>
Tag name
</th>
<th>
Delete
</th>
</thead>
<tbody>
#if($tags->count()>0)
#foreach($tags as $tag)
<tr>
<td>
{{$tag->tag}}
</td>
<td>
<i class="fa fa-trash" aria-hidden="true"></i>
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">
No tags yet
</th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
web.php - this is the place where I define routes for tags for TagsController.php:
//Tags
Route::get('/tags',[
'uses'=>'TagsController#index',
'as'=> 'tags'
]);
Route::post('/tag/update/{$id}',[
'uses'=>'TagsController#update',
'as'=> 'tag.update'
]);
Route::get('/tag/create',[
'uses'=>'TagsController#create',
'as'=> 'tag.create'
]);
Route::post('/tag/store',[
'uses'=>'TagsController#store',
'as'=> 'tag.store'
]);
Route::get('/tag/delete/{$id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
TagsController.php - at first I tried to destroy the element, then I tried to return create view(because when I go through /tag/create rout everything works), but neither worked here
public function destroy($id)
{
return view ('admin.tags.create');
/*
Tag::destroy($id);
Session::flash('success', 'Tag deleted succesfully');
return redirect()->back();*/
}
I believe that you should set the route to Route::get('/tag/delete/{id}',[ 'uses'=>'TagsController#destroy', 'as'=> 'tag.delete' ]); because in your case you are telling the route to expect a variable called $id
Please change the parameters in the route setup in web.php from $id to id. I should solve your issue.
Eg: Route::get('/tag/delete/{id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
Thanks !!.

How to call controller's update function

I am trying to use controller's update function on my dashboard.blade.php page, but when I press on "Save" it switches to show.blade.php page instead of updating.
My dashboard.blade.php page:
#if(count($posts) > 0 )
<table class="table table-striped">
<tr>
<th>Title</th>
<th>Body</th>
<th>Employee Number</th>
<th>Edit</th>
<th>Save</th>
</tr>
#foreach($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td><input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
<td>{{$post->employee_no}}</td>
<td><a href="{{ action("PostsController#update", $post->id) }}" >Save</a></td>
<td>Edit</td>
</tr>
#endforeach
</table>
#else
<p>You Have No Posts</p>
#endif
My update function on PostsController.php page:
public function update(Request $request, $id)
{
$this->validate($request,[
//'title' => 'required',
'body' => 'required'
]);
//Update Post
$post = Post::find($id);
//$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success','Post Updated');
}
I read that "action" will go to the first route that matches the pattern, but I do not know how to solve this problem.
My web.php page:
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::resource('posts','PostsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
How can I call the update function correctly from dashboard.blade.php page?
To update data you have to use form with put method.
and route will be like this,
Route::put('dashboard/update/{id}',[
'uses' => 'DashboardController#update',
'as' => 'dashboard.update'
]);
For more Laravel Routing
Hope it helps :)
You can solve this by using form submit with hidden PUT method.
<form action="{{ url('customers') }}/{{$data['customer']->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
#csrf
</form>
How about having a simple form for each row in the table :
#if(count($posts) > 0 )
<table class="table table-striped">
<tr>
<th>Title</th>
<th>Body</th>
<th>Employee Number</th>
<th>Edit</th>
<th>Save</th>
</tr>
#foreach($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->employee_no}}</td>
<td>
<!-- Form for each row -->
<form action="{{ route("posts.update", [ 'post' => $post->id ]) }}" method="POST">
#method('PUT')
#csrf
<input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
<button type="submit">Save</button>
</form>
<!-- Form for each row -->
<td>Edit</td>
</tr>
#endforeach
</table>
#else
<p>You Have No Posts</p>
#endif

Server side pagination of laravel 5

I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.

Categories