Uploading Image null on Laravel 5.0 - laravel

I had created a form like the following in my view
{!! Form::open(array('url' => 'user/posts', 'files'=> true)) !!}
<div class="form-group">
{!! Form::text('title',Input::old('title'),['class'=>'form-control', 'placeholder' => 'What is your title?']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Your Image') !!}
{!! Form::file('image') !!}
<p class="help-block">Hey! Please don't upload over 15MB images!</p>
</div>
<div class="form-group">
{!! Form::label('caption', 'Don\'t miss to caption!') !!}
{!! Form::text('caption',Input::old('caption'),['class'=>'form-control', 'placeholder' => 'Your caption']) !!}
</div>
{!! Form::submit('Post now!',['class'=>'btn btn-info']) !!}
{!!Form::close() !!}
And I try dd() for my image from my controller but it return nulll. The following one is my Controller
public function store(PostFormRequest $request)
{
dd($request->input('image'));
$post = new Post;
}
The following one is my PostFormRequest
<?php namespace App\Http\Requests;
use Response;
use Illuminate\Foundation\Http\FormRequest;
class PostFormRequest extends FormRequest {
public function rules()
{
return [
'title' => 'required',
'image' => 'mimes:jpeg,bmp,png'
];
}
public function authorize()
{
$image = $this->image;
if(empty($image))
return true;
else
return false;
}
public function forbiddenResponse()
{
return Response::make('Sorry!',403);
}
}
When I try dd($request->input('title')); it's return string(4) "test" what I was wrong?

For files you need to use:
dd($request->file('image'));
and not
dd($request->input('image'));
Reference

Faced with the same problem. One more little thing: if you use native HTML form submit you need also set 'enctype="multipart/form-data"'. Otherwise $request->file('INPUT_NAME') returns string instead the file.

Related

How to upload excel file in Laravel using laravelcollective?

I have a problem when import data xlsx using package fast excel.
I wanna input excel files include the Id from different model like following below
app/http/clustercontroller :
public function Import($id)
{
$model = Cluster::findOrFail($id);
return view('components.Admin.import', compact('model'));
}
public function StoreImport($id, Request $request)
{
//VALIDASI
$this->validate($request, [
'file' => 'required|mimes:xls,xlsx',
]);
if ($request->hasFile('file')) {
$file = $request->file('file'); //Get File
$collection = (new FastExcel)->import($file, function ($line) use ($id) {
return Soal::create([
'soal' => $line['Soal'],
'image' => $line['Image'],
'A' => $line['A'],
'B' => $line['B'],
'C' => $line['C'],
'D' => $line['D'],
'E' => $line['E'],
'kunci' => $line['Kunci'],
'cluster_id' => $id
]); //Import File
});
}
}
resource/admin/import.blade.php :
{!! Form::model($model, [
'route' => $model->exists ? ['cluster.soal.store', $model->id] : 'cluster.soal.create',
'method' => $model->exists ? 'POST' : 'POST',
'files' => true
]) !!}
<div class="form-group">
<label for="" class="control-label">Cluster</label>
{!! Form::text('cluster', null, ['class' => 'form-control', 'id' => 'cluster']) !!}
</div>
<div class="form-group">
<label for="" class="control-label">File .xlsx</label>
{!! Form::file('files') !!}
</div>
{!! Form::close() !!}
the code above displays the form, but when I click submit there is no response
You don't seem to return a response in your Controller method, so that's one reason I can think of. I assume your models are stored?
Things you could do to improve:
Return a view or, even better, redirection after finishing the import
Surround your code with try/catch blocks

Exception with message `No message` on posting data from a form

I got this error when I access to router:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I tried to find out that but I couldn't fix it.
Router(web.php)
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
View(subdomains.account.pages.test.blade.php)
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Controller
namespace Ares\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
The problem is that code always use the GET method instead of POST.
How can I fix it?
EDIT: I just found cause this issue is because I don't use Laravel using php artisan serve, and I only have a virtual host in the public server, how I can solve this without using artisan serve?
FIX: (EDITED)
The issue is by forcing the trailing slash at the end of the URL.
Try This:
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
subdomains/account/pages/test.blade.php
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
web.php
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
Output: When you use your route /test the test button will be displayed on the output page
When you click on the test button, it will be redirect to /testForm page and get the output test
You should try this:
{!! Form::open(['url' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Updated answer
Route::post('/testForm', 'FormController#store')->name('testform.store');
{!! Form::open(['route' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Try to use code below for subdomains.account.pages.test
{!! Form::open(['action' => [ '\Ares\Http\Controllers\FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
You should remove closer from your routes,
Route::get('/test', 'FromController#index');
Route::post('/testForm', 'FormController#store');
then run,
php artisan route:clear
Then submit your form

Laravel: editing two tables

I'm wondering how would it be possible to edit two tables at the same view.
I've got 2 models which are related to eachother.
In my view I'm trying to repopulate my Form select with already inserted value in order to alter it.
Models
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $table = 'products';
public $primaryKey = 'id';
public $timestamps = true;
public function user()
{
return $this->belongsTo('App\User');
}
public function material()
{
return $this->belongsTo('App\Material');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Material extends Model
{
protected $table = 'material';
public $primaryKey = 'id';
public $timestamps = true;
function products(){
return $this->hasMany('App\Products');
}
}
Controller
public function edit($id)
{
$product = Products::find($id);
return view('products.edit')->with('product', $product);
}
View(products.edit)
#extends('layouts.app')
#include('inc.messages')
#section('content')
<div class="container">
<h1>Edit</h1>
{!! Form::open(['action' => ['ProductsController#update', $product->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title', $product->title, ['class' => 'form-control', 'placeholder' => 'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body', $product->body, ['class' => 'form-control', 'placeholder' => 'Body text'])}}
</div>
<div class="form-group">
{!! Form::label('material', 'Material') !!}
{!! Form::select('material',dd($Materials), ['class' => 'form-control']) !!}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Submit',['class' => 'btn btn-primary'] )}}
{!! Form::close() !!}
</div>
#endsection
Error output
Invalid argument supplied for foreach() (View: C:\xampp\htdocs\material\resources\views\products\edit.blade.php)
In order to repopulate a select box in Laravel you need to have possible options that a user can select and then make a select box like this:
<div class="form-group">
{!! Form::label('material', 'Material') !!}
{!! Form::select('material', ['option1', 'option2', 'option3'], null, ['class' => 'form-control']) !!}
</div>
And that option 1,2,3 array can also be called from a database.
P.S. you should use {!! !!} endings instead of {{ }} for {!! Form !!} facade.
update
in order to have the options of select box from database you need to use pluck() to get them.
Make a new function in your controller:
public function materials()
{
return DB::table('your_table')->pluck('name', 'id');
// or this ↓
return ModelName::pluck('name', 'id');
}
and then in your create and edit functions pass it through like this:
public function create($id)
{
$product = Products::find($id);
$theMaterials = $this->materials();
return view('products.create')->with('product', $product, 'materials', $materials);
}
public function edit($id)
{
$product = Products::find($id);
$theMaterials = $this->materials();
return view('products.edit')->with('product', $product, 'materials', $materials);
}
update
{!! Form::model($product, ['method' => 'PATCH', 'action' => ['ProductController#update', $product->id]]) !!}

what is the correct way of form validation in laravel?

I have just created the form and action is PagesController#check and the validation is as follows:
#extends('layout')
#section('content')
<div class = "container">
{!! Form::open(['action' => 'PagesController#check' , 'method' => 'POST']) !!}
<div class = "form-group">
{{ Form::label('country','Country')}}
{{ Form::text('country','', ['class' => 'form-control' , 'placeholder' => ''])}}
</div>
<div class = "form-group">
{{ Form::label('age','Age')}}
{{ Form::number('age','', ['class' => 'form-control' , 'placeholder' => ''])}}
</div>
<div class = "form-group">
{{ Form::label('marks','Marks')}}
{{ Form::number('marks','', ['class' => 'form-control' , 'placeholder' => ''])}}
</div>
<div class = "form-group">
{{ Form::label('description','Description')}}
{{ Form::textarea('description','', ['class' => 'form-control' , 'placeholder' => ''])}}
</div>
{{ Form::submit('Submit' , ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
And the check() method in the PagesController is like this:
public function check(Request $request){
$this->validate($request, [
'country' => 'required',
'age' => 'required',
'marks' => 'required',
'description' => 'required'
]);
return 123;
}
Why is it then it is throwing the following error:
(2/2) ErrorException
Action App\Http\Controllers\PagesController#check not defined. (View: C:\wamp64\bin\apache\apache2.4.23\htdocs\website\resources\views\profiles.blade.php)
Here is the whole PagesController controller:
class PagesController extends Controller
{
public function home() {
return view('welcome');
}
public function about() {
$title = 'This is the about page';
return view('about')->with('title',$title);
}
public function show() {
$yomads = person::all();
return view('show')->with('yomads',$yomads);
}
public function profiles(){
return view('profiles');
}
public function check(Request $request){
$this->validate($request, [
'country' => 'required',
'age' => 'required',
'marks' => 'required',
'description' => 'required'
]);
return 123;
}
}
The error most likely has to do with the route (or lack of it) in app/Http/routes.php - check that it is properly defined there.
Furthermore, it is good practice to create custom request classes. Have a look at Form Request Validation
These can be generated with artisan:
php artisan make:request Profile
Then use it, as you were using the standard request:
public function check(ProfileRequest $request) {
[...]

Video uploading not working in laravel 5

I am working on uploading the videos for my website. But i unable to do it. I have search many documents but i am fail to solve it. Can any one help me out to solve this problem?
I am getting an error "Call to a member function getClientOriginalName() on null"
//Form code for video upload..
{!! Form::open(array('url' => 'video' , 'files' => true)) !!}
<div class="form-group">
{!! Form::label('video_name', 'Video Name : ') !!}
{!! Form::text('name',null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('video_path', 'Select Video : ') !!}
{!! Form::file('path', ['class' => 'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Upload Video') !!}
</div>
{!! Form::close() !!}
//Controller Code
public function store(Request $request){
$data = $request->all();
if ( $request->hasFile( 'path' ) ) {
$file = $request->file( 'path' );
$name = $file->getClientOriginalName();
$data[ 'path' ] = $name;
$destination = '/public/videos';
$request->file( 'path' )->move( base_path() . $destination, $name );
return $name;
}
else {
return '<script>alert("Fail")</script>';
}
}
//The above code is working for images, doucments and audio but not working for videos.
Thanks in advance
If your uploaded video is larger than the upload setting in your php.ini then you get this problem.
I suggest check your php.ini file and increase these directives as needed
php.ini
Example
post_max_size=200M
upload_max_filesize=200M

Resources