laravel upload file - Call to a member function getClientOriginalName - laravel

I have a strange problem today because the code that I will show you is to upload and move an image in a database + uploads directory in my laravel.
The last projects worked but today with laravel 5.4 the code doesn't work anymore and when I want to upload a new image I get an exception
with this line:
Call to a member function getClientOriginalName() on null
Line : $licencie_structure->lb_photo = $request->file('lb_photo')->getClientOriginalName();
Here my blade line to upload the file :
<div class="form-group">
<label>Select a picture : </label>
{!! Form::file('lb_photo' , ['class' => 'form-control', 'placeholder' => 'Photo']) !!}
</div>
Does someone have an idea why I get an exception : Call to a member function getClientOriginalName() on null?
Thanks a lot in advance friends!

This is returning null, meaning it's not in the request object.
$request->file('lb_photo')
Is this form actually sending the upload? Did you forget to add enctype='multipart/form-data' to the form? Is the name correct?
Check the output of $request->all().

Related

Blade view for rich text field on description tag

I'm using voyager in a laravel 7 shopping app for the description of all products.
I want to output the field for the description of the product
#section('description', {!! $item->description !!} )
and
for the facebook share = og:description" content="{!! $item->description !!}">
When I use {!! $item->description !!} in the body, no problem. But in the tag the output always read the p tag and all style form the description.
The weird thing is it's working find on localhost but not on the server. I tried various combination of solution with the same result. I feel there's a quick or maybe it's just not possible?
try html_entity_decode() ref link https://www.php.net/manual/en/function.html-entity-decode.php
{!! $item->description !!}
to
{!! html_entity_decode($item->description) !!} // it will render html
If you are using laravel 7/8 you can create a blade component then you can simple call
<x-editor :content="$description></x-editor>
check out this laravel docs
try this:
{{strip_tags(trim($item->description)}}

MethodNotAllowedHttpException in RouteCollection

I have the MethodNotAllowedHttpException error when I try to update a data
I try to change the Form::model route to PUT and PATCH
Here's my form::model:
{!! Form::model($mission, ['route' => ['missions.update', $mission->id_missions], 'method' => 'PUT', 'class' => 'form-horizontal panel']) !!}
And here's my route:
Route::resource('missions', 'MissionsController');
I got the error mentionned above
Can somebody help me please ?
Maybe you forgot to spoof the PUT method in your form, you can do that by using blade's #method('PUT').
This is how you can implement it :
<form action="/foo/bar" method="POST">
#method('PUT')
</form>
So try also changing the form's method to POST when you use the Form::model helper because HTML forms can only be sent by GET or POST methods hence why one has to spoof the other CRUD methods.
You can read more about that here.

How to save multiple checkboxes in Laravel?

I have created a blade (using Laravel Collective) with a multipe checkboxes:
#foreach($subsc as $subsc)
<div>
{{Form::checkbox('checkbox['. $subsc->Scheme->Scheme_id .']', '1')}}
{!! Form::label('SchemeName', $subsc->Scheme->Scheme_Name.$subsc->Scheme->Scheme_id, ['class' => 'control-label']) !!}
</div>
#endforeach
Now I want to save each checked box in a table as the scheme_id. How do I do that?
I guess you could use an array as name to make it easier and try something like this.
Form::checkbox('schemeIDS[]', $subsc->Scheme->Scheme_id, true);
// Parameters checkbox: name, value, checked
In the controller function use
$schemeIDS = $request->get('schemeIDS'); // get all the checked values as array
foreach($schemeIDS as $schemeID)
{
// insert into the database
}

Error while using yield in Form:text value

1) Making CRUD so wanted to use yield in value in Form:text to load the values
{!! Form::Text('employeeName_txt',
'#yield('editEmployeeName')',
array('class' => 'form-control','placeholder'=>'Employee Name')) !!}
Getting error
FatalErrorException in 2168be22f5078758fb418696bf6815fc3ab642a4.php line 59:
syntax error, unexpected 'editEmployeeName' (T_STRING)
Can anyone please help what is wrong in this
& also
2) Which is the best method for CRUD
Creating Edit page differently & extending it with create page and yield or section the values
Or in create page only, with the use of if else condition do the coding eg: if route name contains edit then method= put ,delete button show etc.
Try using this
{!! Form::Text('employeeName_txt',"yield('editEmployeeName')", array('class' => 'form-control','placeholder'=>'Employee Name')) !!}

Laravel 4 - Showing edit form with OLD data input as well as DB information

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value

Resources