NotFoundHttpException error in laravel - laravel

This is my view:
<img src="../public/{{ $axid->img }}">
and this is my route :
Route::get('/ax/{axid}', function($id) {
$axid = show::find($id);
return view('ax', compact('axid'));
Images are uploaded in public/uploads and show is model name. However, I have this error:
NotFoundHttpException
...what is the problem?

If we assume that your image resolving code is functioning properly and that you are returning the full url of the image you want to show, then change your view code to:
<img src="{{ asset($axid->img) }}">

Please see how you should be setting this out:
View before moving to this a href..: Get ID
Route: Route::get('/ax/{id}', 'AxidController#get');
Controller with me assuming Show is your model name:
public function get($id, Show $show)
{
$axid = $show->find($id);
return view('page', compact('axid'));
}
On the page of ax/{id}:
<img src="{{ $axid->image }}">

Related

Laravel: Display image from public storage

I am saving an uploaded image like this:
$profile_picture = $request->file('profile_picture');
$user->profile_picture = $profile_picture->getClientOriginalName();
Storage::disk('public')->put( $user->profile_picture, $request->file('profile_picture') );
and then I am trying to display that image in HTML with:
<img class="img-fluid" src="{{ asset('storage/'. Auth::user()->profile_picture) }}">
the image is being saved in storage/app/public, but the image is not showing in html, how do i go about this?
thank you
You may use response method on a Laravel filesystem
return Storage::disk('public')->response($user->profile_picture);
N.B: In order to do that, you need a Laravel route. The above code should be located in a controller method

Undefined variable on Laravel

I´m a beginner starting with Laravel. I´m trying to show a question made on this website.
Here is the Controller page:
public function show($id)
{
//Use the model to get 1 record from the database
$question = $Question::findOrFail($id);
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
I have included at the top of the file:
use App\Question;
Here is my blade page:
#section('content')
<div class="container">
<h1> {{ $question->title }} </h1>
<p class="lead">
{{ $question->description }}
</p>
<hr />
</div>
#endsection
In the model I have not defined anything since I don´t need to specify any special rule. And finally here is the Route:
Route::resource('questions', 'QuestionController');
I got the error "ErrorException Undefined Variable: Question" and supposedly the error is on:
$question = $Question::findOrFail($id);
I´m looking forward to your observations.
Kind Regards.
You just need to change the controller section
public function show($id)
{
//Use the model to get 1 record from the database
$question = Question::findOrFail($id); // here is the error
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
Explanation:- You are going to use a variable $Question that is not defined. This is the basic PHP error, not the laravel issue.
However, You are using the "App\Question" model class not a sperate variable.

laravel-5.7: data is not saving into database, Object not found

I'm trying to save data into db but its not saving and says that object not found, can anyone suggest me solution, i am following this tutorial: https://laracasts.com/series/laravel-from-scratch-2018/episodes/10
controller:
public function index()
{
$projects = Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
routes:
Route::get('/projects','ProjectsController#index');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/create','ProjectsController#create');
create.blade.php:
<form method="POST" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Project description"></textarea>
</div>
<div>
<button type="submit">Create Project</button>
</div>
</form>
index.blade.php:
#foreach($projects as $project)
<li>{{ $project->title }}</li>
#endforeach
You have missed out passing request parameter in the controller store()
public function store(Request $request)
{
$project = new Project();
$project->title = $request->title;
$project->description = $request->description;
$project->save();
return redirect('/projects');
}
And also don't forget to include use Illuminate\Http\Request; above(outside) controller class.
The Laravel code you've posted is correct under a properly configured website. The error from your comments:
Object not found! The requested URL was not found on this server. The
link on the referring page seems to be wrong or outdated. Please
inform the author of that page about the error. If you think this is a
server error, please contact the webmaster. Error 404 localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
is an Apache error page, which means it's not requesting a page from your laravel project at all. The data is probably saving in your database, but then you redirect away to a page that is outside your project, and Apache can't find it.
Your website is located at http://localhost/laravel/public, which means you need to access the projects page at http://localhost/laravel/public/projects. However, redirect('/projects') gives you an absolute path instead of a relative path, sending you to http://localhost/projects, which does not exist.
Solutions
Since this is a local development project, I'm going to skip the issues with the improper Apache configuration and focus on other ways to avoid the error.
Option 1
Use a named route:
Route::get('/projects','ProjectsController#index')->name('projects.index');
and use the name of the route for the redirect:
return redirect()->route('projects.index');
This should generate correct urls within your project.
Option 2
Use serve for development instead of Apache.
Open a terminal in your Laravel project directory and run this command:
php artisan serve
This will start PHP's built-in webserver at http://localhost:8000, skipping Apache entirely. During development this is perfectly fine.

Why is the avatar on app.blade is broken whenever I open links that has id?

Whenever I open an item the link would be
http://localhost/mywebsite/public/item/{$item_id}
the avatar will break. Below is the code I used
<img src="uploads/avatars/{{ Auth::user()->avatar }}">
The location of the image is C:\xampp\htdocs\mywebsite\public\uploads\avatars
I presume that as you are using relative URLs the translated path in browser will be http://localhost/mywebsite/public/item/{$item_id}/uploads/avatars/{{ Auth::user()->avatar }}
And that's not what you want
Try
<img src="http://localhost/mywebsite/public/uploads/avatars/{{ Auth::user()->avatar }}">
In production in will be just
<img src="/uploads/avatars/{{ Auth::user()->avatar }}">
Am not sure it will work on xampp though
So to say, I recommend using Laravel Valet or Homestead instead of Xampp, it cuts some problems like yours off.
in File model i created a function to get file encoded base64
public function getFile()
{
$path = storage_path('app'). "/" . $this->path;
$type = File::mimeType($path);
$file = Storage::disk('local')->get("/". $this->path);
return "data:image/png;base64,".base64_encode($file);
}
in blade You can use like that
<img src = '$file->getFile()'>

Show the result of a route in blade view (Laravel)

I have a route that return an image, and i want the show the result of this route in a blade view, is there a way to directly call controller action or result of this route directly in the blade view ?
Thanks in advance.
You rather don't want to return image in blade. You should create separate controller action, that will return this image and link image src to this route, for example in controller:
public function image()
{
return $img = Image::make('image.jpg')->resize(800, 500)
->insert('sub_image.jpg', 'bottom-right', 10, 10) ->response();
}
then you create route to this controller action:
Route::get('my-image', 'MyController#image')->name('get-my-image');
and then in your Blade view you should link image src to this action:
<img src="{{ route('get-my-image') }}">
You can do this by giving the URL in src attribute of <img> tag as:
<img src="{{ route('image.url') }}">
Well if it's an image, why don't you use an img tag?
<img src="{{ route('my.image') }}" alt="My Image">

Resources