Laravel Photo Upload using LaravelCollective - laravel

When I try to submit the form an Error occurs.
public function store(){
$story = new Story;
$story->file_link = $request->resume_link;
$story->save();
$fileName = $story->id . '.' .
$request->file('file_link')->getClientOriginalExtension();
$requests->file('file_link')->move(
base_path() . '/public/uploads', $fileName
);
$story->file_link = $fileName;
return redirect('home');
}
What this does is mainly to store the file i uploaded. Please upvote this post. I am banned due to this question. Thanks

To be more precise, Laravel Collective is really a collection of packages that are removed from Laravel core. I am assuming you are talking about the HTML/Forms package. To answer your question, I think the docs are self-explanatory enough.
To generate a file input (front end)
{{ Form::file('image') }}
To "retrieve" the file (back end controller)
$file = $request->file('image');
You need to be more precise on your question. What are you confused about?

I think this might solve your problem.
Your blade template code will be
<div class="form-group">
<label for="image">Chose the picture</label>
<input type="file" class="form-control" name="image" id="image" {{ $errors->has('image') ? 'class=has-error' : '' }} value="{{ $photo->image }}">
</div>
Your Controller code will be
$logo=$request->file('image');
$upload='uploads/logo';
$filename=$logo->getClientOriginalName();
$success=$logo->move($upload,$filename);
$doctor->image = $filename;

Related

Laravel 8 multi value select filtering

I'm trying to build Laravel project that will have a multi-select dropdown with list of categories. Selecting category should reload the page and apply filter.
"scopeFilter" in my model is actually doing filtering, but here i just need to find a way to properly form URL. The problem is that this code i have:
<form id="cats-form" action="#" method="GET">
<select multiple class="chosen-select" name="test[]">
#foreach($categories->get() as $cat) //this loop goes through existing categories in the system
#php
//if category is part of URL, pre-select it in the select:
$arr = request()->all();
$selected = '';
if(array_key_exists('test', $arr)) {
$testArr = $arr['test'];
$selected = in_array($cat->id, explode(',', $testArr)) ? 'selected' : '';
}
#endphp
<option {{ $selected }} value="{{ $cat->id }}">{{ $cat->name }}</option>
#endforeach
</select>
</form>
<script>
$(".chosen-select").chosen({ })
$('.chosen-select').on('change', function(evt, params) {
$('#cats-form').submit();
});
</script>
Is actually giving me this URL:
http://localhost:11089/?test%5B%5D=19&test%5B%5D=5
While i actually need this:
http://localhost:11089/?test=19,5
I guess it's a trivial problem to solve for someone with better knowledge of Laravel, can you tell me pls what i'm doing wrong here?
This is rather how the language or framework or library reads an URL query string. Assuming that your prefered way of URL string is a valid format (as I usually see the format ?arr[]=val1&arr[]=val2 and never see the second format that you preferred), both query string formats should be acceptable to PHP.
As Yarin stated that the best way PHP reads an array is the first method, you don't have to worry too much because the decoded URL query string is exactly in the first format.

Problem uploading images in cpanel in laravel 7

I am trying to upload images on a shared hosting but am unable to upload. On my machine it works well but when I host i cant figure how to change the paths.All the other pages are working fine except uploading images.
on my controller
foreach ($images as $image){
$move=$image->move(public_path().'/images2/',time().'_'.$image->getClientOriginalName());
if($move){
$imagedata=Images::create([
'title'=>time().'_'.$image->getClientOriginalName(),
'filename'=>time().'_'.$image->getClientOriginalName()
]);
Displaying the previous images is okay with this code on the view side
#foreach($product_images as $image)
<div class="carousel-item {{ $loop->first ? 'active' : '' }}">
<div> <img class="d-block w-100 newim" src="/images2/{{$image->filename}}"/></div>
</div>
#endforeach
i think your problem is here with the wrong "," :
$move=$image->move(public_path().'/images2/',time().'_'.$image->getClientOriginalName());
change it to:
$move=$image->move(public_path().'/images2/'.time().'_'.$image->getClientOriginalName());
and make sure your folders are same name as you wrote in your code because host is sensitive to uppercase or lowercase
I added this to index php though not sure whether it is secure but it works
$app->bind('path.public', function() {
return realpath(__DIR__.'/../public_html/');
});

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.

How to modify request text in laravel

I can't think of a good title I'm so sorry but I need help.
I have a field that is added by jquery depending on how many questions are present in the database, so for example I have 3 questions in the db i will have 3 answer fields too, it will look like this:
answer_1
answer_2
answer_3
since i am not sure how many questions there can be, i need to loop the $request->answer_1, 2 and 3 in the controller to store the answers. but i'm not sure how to do it. I have tried:
for($i = 0; $i < total.count.of.answers; i++){
$x = $i + 1;
$answer = new Answer;
$answer->answer = $request->answer_{$x};
$answer->save();
}
but not working. How do I achieve this?
(note i can already send the answers to the backend using jquery, problem is only how i can pull it in the controller)
First, I'd group my inputs as such:
<form action="/submit" method="POST">
{{ csrf_field() }}
<input type="text" name="answers[]">
<input type="text" name="answers[]">
<input type="text" name="answers[]">
<button type="submit">Submit</button>
</form>
Then, in my controller, I would be able to loop through the answers by doing the following:
foreach($request->answers as $answer) {
Answer::create(['answer' => $answer]);
}
This is just an example, but you get the idea.
Laravel: save multiple records in database.
foreach($request->all as $value){
$answer = new Answer;
$answer->answer = $value->answer;
$answer->save();
}

Laravel barryvdh/laravel-dompdf

I am using Laravel 5.2 and package "barryvdh/laravel-dompdf". I am streaming/viewing the view file like this:
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
This opens the appropriate view and is working fine... see image:
http://prntscr.com/dflj4d
At the end of url as you see '43' which is id by default..what I want to show at the end is manufacturer/product or something name instead of its id..Can we achieve this?
You can set custom routing for view pdf, send parameter by post method. Like:
Route::post('/manufacturer/product', 'ReportController#viewPdf');
If you use button then use this way:
View Pdf
Form For View PDF
<form id="viewPdf" action="{{ url('/manufacturer/product') }}" method="POST" style="display: none;">
<input type="hidden" name="product_id" value="43">
{{ csrf_field() }}
</form>
In Controller:
$manuid= $request->product_id;
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
Hope this trick will help. Thank You :)

Resources