I am facing an issue related to multiple files uploads in laravel version 5.7.*
This is what i have in web.php
Route::get('add/news',[
'uses' => 'Admin\NewsController@create',
'as' => 'add.news'
]);
Route::post('add/news',[
'uses' => 'Admin\NewsController@store',
'as' => 'add.newsProcess'
]);
In blade file form code
<form id="addNewsForm" action = "" method="POST" enctype="multipart/form-data" autocomplete="off">
<fieldset>
<legend>News Management Area</legend>
<div class="form-group">
<label class="col-form-label" for="news_title">Title</label>
<input type="text" name="news_title" class="form-control news_title placeholder-shown" placeholder="News Title" id="news_title">
</div>
<div class="form-group">
<label class="col-form-label" for="newsBody">Body</label>
<textarea name="newsBody" class="form-control newsBody placeholder-shown" id="newsBody"></textarea>
</div>
<div class="form-group">
<label>Attachments <span class="optional">(Optional)</span></label>
<div class="custom-file">
<input type="file" name="newsAttachments[]" class="custom-file-input newsAttachments" id="tf3" multiple>
<label class="custom-file-label persist" for="tf3">Choose Attachment</label>
</div>
</div>
<div class="form-group">
<div class="list-group-item d-flex justify-content-between align-items-center">
<span style="margin-left: -15px;">Status</span>
<label class="switcher-control" style="margin-right: -12px;">
<input type="checkbox" name="is_active" class="switcher-input" checked="checked">
<span class="switcher-indicator"></span>
</label>
</div>
</div>
<div class="form-group btn-toggle">
<button type="submit" id="BtnSubmit" class="btn btn-primary">Submit</button>
</div>
</fieldset>
<div id="loader"></div>
</form>
Model Code
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class NewsTb extends Model { protected $primaryKey = 'news_id'; protected $fillable = ['title','body','attachment','published_on','is_active','user_id'];
public function user(){
return $this->belongsTo('App\User');
}
}
In my controller i have this code
public function store(Request $request) { dd($request->all());
$user_id = Auth::id();
$news_title = $request->news_title;
$newsBody = $request->newsBody;
$is_active = $request->is_active;
$is_active = $request->is_active;
if ($is_active === "on") {
$is_active = 1;
}
else{
$is_active = 0;
}
if($request->hasFile('newsAttachments'))
{
foreach($request->file('newsAttachments') as $key => $image)
{
print_r($image);
$name=$image->getClientOriginalName();
$image_encod = base64_encode(file_get_contents($request->file('newsAttachments')[$key]));
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
/*$data = new Image();
$data->image_name = $image_encod;
$data->save();*/
}
}
}
**I am facing this issue with multiple files uploads but getting this error.**
ErrorException (E_WARNING)
trim() expects parameter 1 to be string, array given
Please help me thanks in advance
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire