here is my code form my controller
i9ts seem to Image::get() not working when I use image::all() then its work
public function index(){
$images = Image::get();
return view('home', compact('images'));
}
here is my home.blade.php
<div class="container">
<div class="row justify-content-center">
<form action="{{route('ablum.store')}}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="image" class="form-control">
<button class="btn btn-primary" type="submit">submit</button>
</form>
#foreach($images as $image)
<p> this is image name {{$image->name}}</p>
#endforeach
</div>
</div>
when I navigate to the home view I get this error
$images is undefined
Make the variable optional in the blade template. Replace {{ $images }} with {{ $images ?? '' }}
how can I solve this ?
You can try and rewrite your controller code like this:
return view('home')->with(compact('images'));
Official Laravel docs here does not say anything about any second parameters to view() helper.
with() should actually append the data and $images should be available in blade now.
Also, for simplicity, just use Image::all(). If this again is not working, you can try and ditch the compact method and in with() send an array like:
return view('home')->with(['images' => $images]);
Related
Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;
I'm very confused, I need your help, this is the error:
Missing required parameter for [Route: single.temp] [URI: singlepost/{name}] [Missing parameter: name]. (View: C:\Users\Toshiba\Desktop\working\mouhawla\resources\views\index.blade.php)
The search field:
<div class="search">
<form role="form" action="{{route('single.temp')}}">
<i class="fa fa-search"></i>
<div class="field-toggle">
<input type="text" name="name" class="search-form" autocomplete="off" placeholder="Search">
</div>
</form>
</div>
The method:
public function getPostByName($name) {
$products = DB::table('templates')
->where('name', $name)
->first();
return view('singlepost', compact('products'));
}
The route:
Route::get('/singlepost/{name}', 'App\http\controllers\TemplatesController#getPostByName')->name('single.temp');
The final view:
<h1 style="text-align: center;">ACH-template</h1>
<table>
<tr>
<td><img src="/storage/{{$products->image_path}}"></td>
<td><img src="/storage/{{$products->image_path2}}"></td>
<td><img src="/storage/{{$products->image_path3}}"></td>
<td>
<p>
<h2 style="text-align:center;">{{$products->name}}</h2>
</br>
<p>{{$products->description}}</p>
</p>
</td>
</tr>
</table>
<a href="/storage/{{$products->file_path}}" class="btn btn-primary">
<button class="btn" style="width:100%">
<i class="fa fa-download"></i> Download
</button>
</a>
The error is very explanatory. You are trying to use /singlepost/{name} route, but on your blade file, you are doing route('single.temp'), it is telling you that it needs the parameter name, else it cannot create the URL as it is a missing parameter.
You should have something like:
<form role="form" action="{{route('single.temp', ['name' => VALUE'])}}">
But that will not solve your problem, as you are trying to do a search, so you want something like /singlepost/John, and John is going to be input by the user on the input field. So you have to do an AJAX call because {{ route('single.temp') }} is going to be rendered by PHP and served to the user, so it is always going to miss the needed parameter.
What you can also do is get that value from the Request instead of a URL parameter.
You have defined a route which requires a parameter: {$name}. You have also used the route helper to generate a URL which takes the name of a route as the first argument and an array of parameters as an optional second argument.
When you have used route('single.temp') in your form action, you have not specified any parameters and so Laravel is throwing the error you're seeing. To resolve this error, you would need to specify a $name parameter as the second argument (i.e. route('single.temp', ['name' => 'something'])). This is not ideal though as if you're using $name as a search term, you don't know the value when the page is first rendered and so can't provide that value.
There are a few ways you could achieve your goal of searching records, a basic example of how you could do this follows.
web.php
Define two routes, the first to return a view with a form and another to process the form submission and show the results.
Route::get('/templates', [TemplateController::class, 'index'])
->name('templates.index');
Route::get('/templates/search', [TemplateController::class, 'search')
->name('templates.search');
TemplateController.php
Define the two functions which will be used when one of the routes defined above is requested.
class TemplateController extends Controller
{
// return a view
public function index()
{
return view('templates.search', ['templates' => []]);
}
// process the form submission
// perform a search for the $request search term
// return a view with the results
public function search(Request $request)
{
$request->validate([
'term' => ['required', 'string']
]);
$templates = Template::where('name', $request->term)->get();
return view('templates.search', ['templates' => $templates]);
}
}
templates/search.blade.php
{{-- create a form which will submit to the search route --}}
{{-- note I use GET rather than POST here, explained later --}}
<form action="{{ route('templates.search') }}" method="GET">
#csrf
<input type="text" id="term" name="term" />
<button type="submit">
{{ __('Search') }}
</button>
{{-- loop over and show results if there are any --}}
#forelse ($templates as $template)
{{ $template->name }}
#empty
{{ __('Empty') }}
#endforelse
</form>
The above should be self explanatory. My reason for using GET rather than POST in the search is because the value will be added to the URL as a query string parameter meaning it can be bookmarked or shared with ease.
I am using model biding, but I am basically trying to create a form that can edit and update comments. It runs perfectly and even prompts me to edit the form. However, every time I try to update it the POST method is not supported for the root. which is bogus as I did the spoofing correctly, someone help me please.
Here us my CommentsController methods
public function edit($id)
{
$comment = Commenting::find($id);
return view('comments.edit', compact('comment'));
}
public function update(Request $request, $id, $posts)
{
$comment = Commenting::find($id);
$comment->update($request->all());
$comment->posts_id = $posts;
return view('post.show', compact('comment'));
}
Here is the routing in my web.php
Route::get('/posts/comment/{comment}', 'CommentsController#edit')->name('comments.edit')->middleware('auth');
Route::put('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Here's what I call the link to edit the form in my show.blade
Edit
lastly this is my edit.blade file
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
#csrf
#method('PUT')
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
Take a look your update method
public function update(Request $request, $id, $posts)
Try to delete the $posts variable on update function
Try
{{method_field('put')}}
or
<input type="hidden" name="_method" value="PUT">
add patch method in blade file:
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
write post method in route file :
Route::post('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Your route binding is named comment not comments. This will not generate a URL to your comments.update route:
route('comments.update', ['comments' => $comment])
This will generate a URL to /posts/comment?comments=3 not /posts/comment/3. You want to use the same name as the route parameter so it knows to replace that route parameter:
route('comments.update', ['comment' => $comment])
Which would generate a URL to /posts/comment/3.
Also your controller method
public function update(Request $request, $id, $posts)
has a $posts variable but there is no parameter for that defined for the route, so where does that come from?
If you actually want to use Model Binding, which it would appear you are not, you could use Implicit Bindings by type-hinting a $comment argument on that controller method:
public function update(Request $request, Comment $comment)
as the route pararemeter is named comment not id.
Laravel 5.8 - Routing - Route Model Binding - Implicit Binding
i have a question about the Laravel search function, i had follow the guildeline online and i still fail to search the category, can someone guide me and tell me where i did wrongly ? Much appreciated
My category Controller php code:
public function search(Request $request)
{
$search = $request->get('search');
$posts = DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['posts' => $posts]);
}
My index.blade code
<div align="left">
<div class="col-md-4">
<h1>Policy</h1>
</div>
<div class="col-md-4">
<form action="/search" method="get" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="_method" placeholder="Search ID / Code"> <span class="input-group-btn">
<button type="submit" class="btn btn-primary">Search</button></span>
</div>
</form>
</div>
</div>
web.php
Route::get('/search','categoryController#search');
What error i get is here
Error image
interface
Database
You are sending $posts variable to your view. But the error says you are referencing a $category variable.
return view('category.index',['posts' => $posts]);
Maybe you might want to update view to use $posts. If you could post your full code (category/index.blade.php) we might be able to help you better.
__
Here is how I would do:
$categories= DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['categories' => $categories]); //you can also use compact return view('category.index', compact('categories') );
And to display:
#foreach( $categories as $category )
<div>{{ $category->id }}</div>
#endforeach
Another tip: you can name your routes like so
Route::get('search','categoryController#search')->name('search');
Then you can reference this route (in form or anywhere else you want) like so:
<form action="{{ route('search') }}" ..>
I'm using Laravel's file storage system and I'm trying to trigger a download response to download my files through the browser, but it cant find the correct file instead it downloads my file page view script. I have a storage link set up as well.
Any suggestions would be appreciated.
File.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file)}}" download="{{$file->name}}">
{{$file->name}}</a>
</div>
</div>
#endsection
Download function
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
file routes
Route::get('files', 'FileController#index')->name('upload');
Route::post('files', 'FileController#store');
Route::get('files/{file}', 'FileController#download')->name('download');
Remove this download="{{$file->name}}" from the link.
You can add download as html attribute:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
But you don't need it in this case, use just:
{{$file->name}}
The response()->download() method in your controller will generate a response that forces the browser to download the file at the given path. So make sure your path i correct.
If your file is in your-project-root-path/storage/app/files/, you can use:
return response()->download(storage_path('/app/files/'. $file));
If your file is in your-project-root-path/storage/storage/app/files/, use:
return response()->download(storage_path('/storage/app/files/'. $file));
I think you are passing the file object instead of the filename to your download route.
Try
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file->name)}}" download>
{{$file->name}}</a>
</div>
</div>
#endsection
Try replacing {{route('download',$file)}} with {{route('download',$file->name)}}.
Also try replacing the download controller with this code
public function download($file){
return response()->download(storage_path('app/files/'.$file));
}
public function jpg_download($id)
{
if (auth()->user()->download_count < 5) {
auth()->user()->increment('download_count');
$data = DB::table('products')->where('id', $id)->first();
$path = public_path('/storage/item/jpg/' . $data->jpg);
return response()->download($path);
}
dd('Next Day Download');
}