hi all i want to do upload and download files.am uploading files successfully but when i want to download file i am getting error like
Missing argument 1 for stockTransferController::getfile()
below is my code
Routes
Route::get('stock/file','stockTransferController#getfile');
Controller
public function uploadfile($id) {
$stfk =Stocks::find($id);
$file = Input::file("file");
$fileName = $file->getClientOriginalName() ;
$path=storage_path();
$stfk->file = $fileName;
$stfk->st_status ='1';
$stfk->path_to_file =$path;
$stfk->name_on_disk = basename($path);
$stfk->save();
Session::flash('message', 'Successfully updated.');
return View::make('stock.index');
}
public function getfile($file)
{
$file_path = storage_path() . "/" . $file;
return Response::download($file_path);
}
view
<a class="btn btn-danger" title="" data-toggle="tooltip" href="'.url('stock/file',$row->file).$row->fileName.'" target="_blank"><i class="fa fa-download"> </i></a>
please help me to get rid of this issue am using laravel 4.2
It should be like this on your routes file.
Route::get('stock/{file}','stockTransferController#getfile');
Use this
Route::get('stock/{file}','stockTransferController#getfile');
<a class="btn btn-danger" title="" data-toggle="tooltip" href="'.url('stock/file/{$row->file}').'" target="_blank"><i class="fa fa-download"> </i></a>
Related
I made a button to update a user's access to the website and I get an error. I searched here but I did not find the answer to help me, can you give me some advice?
This is the error: Creating default object from empty value
Controller:
public function suspendUser(Request $request, $id) {
$user = User::find($id);
$user->userAcces = 0;
$user->save();
return back();
}
View::
<div class="col-md-3 col-sm-3">
<form>
<i class="fa fa-eye" aria-hidden="true"></i>
<i class="fa fa-ban" aria-hidden="true"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-user-friends"></i>
<form>
</div>
Route:
Route::get('/updateUser/{id}', 'UserController#suspendUser');
I tried other things but failed, maybe something went wrong in the controller?
It means this user is not found with this $id and $user is null , so check the $id which sent to the suspendUser method, and it's better to use User::findOrFail($id); in order to return 404 if model not found ,and not face these kinds of errors.
I'm trying to upload images to my website but it does not show up
this is the HTML
<div class="col-md-10">
<div class="post-image">
<canvas style="display: none" id="canvas-preview" style="overflow:hidden;"></canvas>
<img id="img-preview" src="{{ url('backend/'). ($posts->preview_image?'/'. $posts->preview_image:'/post_preview/post-image.png')}}"> </div>
<div class="m-b-10">
<a id="upload-submit" class="btn btn-warning btn-block btn-sm" onclick="file.click();"><i class="fa fa-upload"></i> Upload Picture </a>
{{ Form::file('preview_image', ['class'=> 'btn btn-warning btn-block btn-sm','id'=>'file','style'=>'visibility:hidden']) }}
</div>
</div>
the controller:
public function create()
{ $this->authorize('create', Post::class);
return \View::make('posts.edit')
->with('posts', new \App\Models\Post);
}
and the model:
class Post extends Model
{
const DEFAULT_PREVIEW_IMAGE = "post_preview/post-image.png";
/**
* get preview image
* #return mixed|string
*/
public function getPreviewImage()
{
return $this->show_preview_image?$this->preview_image:static::DEFAULT_PREVIEW_IMAGE;
//return $this->preview_image;
}
}
I tried to add multiple extensions like jpg, jpeg or gif in the model but the problem persists
For laravel images should be stored in storage folder or you can create a folder inside storage folder. You also need to create symbolic link
php artisan storage:link
Here is how to upload the file to the storage folder
if ($request->hasFile('file_name')) {
$file = $request->file('file_name');
$fileName = str_replace(' ', '-', $file->getClientOriginalName());
$uploadPath = storage_path('app/public/folder_name');
$path = $request->file('file_name')->move($uploadPath, $fileName);
$uploadPathFile = $fileName;
}
Then you save the path to your DB.
For showing the image in the blade
<img src="/storage/folder_name/{{$data->image}}" alt="No image available" class="img-thumbnail">
Here $data->image is only the path
is your image stored in a public directory - try accessing the URL in your browser to see if the response is the expected image.
If not please provide more details how are you trying to retrieve the image (via binary response or stored as a public asset)?
I'm working with role based permission using Zizaco Entrust package with yajra datatables.
when i'm giving permission to some users i have to touch datatables also.
This is my code ,
Controller.php
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', '#permission('user-edit')
View#endrole
Edit')
->make(true);
when i use permission inside datatables it is getting error , any one having idea to solve this ??same question in
yajra datatables and entrust role permission laravel
Entrust::can() checks if the user is logged in, and then if user has the permission. If the user is not logged the return will also be false.
Check below code:
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', function($company){
$action = '';
if (!Entrust::can('user-edit')) {
$action = 'View';
}
$action .= 'Edit';
return $action;
})
->make(true);
Made correction into code for {{}} and quotes issue.
You can also follow this technique
->addColumn('actions', function($admin) {
return view('admin.user.partials.admin_action', compact('admin'))->render();
})
And in your blade admin_action, you can write your view code as follows
#permission("admin-edit")
<a href="#" title="Edit"
class="btn-sm btn-primary editData"
data-id="{{ $admin->id }}"
data-fname="{{ $admin->f_name }}"
data-lname="{{ $admin->l_name }}"
data-email="{{ $admin->email }}"
data-redeem_manager="{{ $admin->redeem_manager }}"
data-mobile="{{ $admin->mobile }}"
data-role_id="{{ $admin->role_id }}"
><i class="fa fa-edit"></i> </a>
#endpermission
Entrust::can() not working with me causes an error.
but \Auth::user()->can('user-edit') works well with me
return datatables()->of($data)
->addColumn('actions', function ($data) {
$button = '<a class="btn btn-sm btn-info" href="' . route('users.show', $data->id) . '" >Show <i class="fa fa-eye"></i></a>';
if (\Auth::user()->can('user-edit')) {
$button .= ' <a class="btn btn-sm btn-primary" href="' . route('users.edit', $data->id) . '" >Edit <i class="fa fa-edit"></i></a>';
}
if (\Auth::user()->can('user-delete')) {
$button .= ' <a id="' . $data->id . '" class="delete btn btn-sm btn-danger" href="#" >Delete <i class="fa fa-trash"></i></a>';
}
return $button;
})
->rawColumns(['actions'])
->make(true);
I have the following function
public function getTasks()
{
$users = User::select(['id','users_full_names', 'email', 'users_telephone_number', 'users_credits', 'users_last_seen_carbon_object','users_profile_picture','updated_at']);
return Datatables::of($users)
->addColumn('action', function ($user) {
return '<i class="glyphicon glyphicon-edit"></i> Edit <i class="glyphicon glyphicon-trash"></i> Delete <i class="glyphicon glyphicon-ban-circle"></i> Moderate <i class="glyphicon glyphicon-search"></i> View <i class="glyphicon glyphicon-user"></i> Impersonate';
})->editColumn('updated_at', function(User $user) {
$dt = $user->updated_at->toDateTimeString();
$datetime = Carbon::parse($dt);;
return $datetime->diffForHumans();
})->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->make(true);
}
that i am using to get the pictures stored in the database. This is the particular section that is creating the link
->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->make(true);
but the link generated looks like this
View Image
instead of a clickable html link.
How can i fix this?.
Please use rawColumns as used below,
->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->rawColumns(['users_profile_picture'])->make(true);
Please refer, https://github.com/yajra/laravel-datatables/issues/949
This is my Controller:
public function anyData()
{
$myID = Auth::guard('cashier')->user()->id;
$players = User::where('parent_id','=', $myID)
->where('delete', '=', 1)
->orderBy(DB::raw('LENGTH(name),name'))
->get(['id', 'name', 'score', 'alarm', 'bonus_from_percentage']);
return Datatables::of($players)
->addColumn('actions4', function ($players) {
return
'
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#actions-modal" class="open-playerID btn btn-warning" data-toggle="modal"
data-target="#actions-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.SETTINGS').'"><span class="glyphicon glyphicon-cog"></span></a>
';
})
->addColumn('actions3', function ($players) {
return
'
<button type="button" class="'.changeClass($players->alarm).'"
value="OK" onclick="Enable('.$players->id.','.$players->alarm.')" id="enable"/><span class="glyphicon glyphicon-ok-circle"></span></button>
';
})
->addColumn('actions2', function ($players) {
return
'
<a href="#" data-toggle="modal" data-id="'.$players->id.'" data-target="#changeplayerpassword-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.CHANGEPLAYERPASS').'" class="playeridchangepass btn btn-warning">
<span class="glyphicon glyphicon-edit"></span></a>
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#delete-modal" class="open-delete btn btn-warning" data-toggle="modal"
data-target="#delete-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.DELETE').'" ><span class="glyphicon glyphicon-remove"></span></a>
';
})
->addColumn('actions', function ($players) {
return
'<div class="btn-group text-center">
<button onclick="changeValueIn(this);" class="btn btn-primary btn-sm" style="width: 50px;" data-score-id="' . AppHelper::ToEuroC($players->score) . '" data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#In">IN</button>
<button onclick="changeValue(this)"class="btn btn-danger btn-sm" data-score-id="' . AppHelper::ToEuroC($players->score) . '"data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#Out">OUT</button>
</div>
';
})
->addColumn('score', function ($players) {
return AppHelper::ToEuroC($players->score);
})
->addColumn('bonus_from_percentage', function ($players) {
return AppHelper::ToEuroC($players->bonus_from_percentage);
})
->addColumn('players', function ($players) {
return ($players->name) . ' ' .dikOn($players->id);
})
->make(true);
}
This script i run to refresh table each 5 seconds:
// REFRESH TABLE
function refreshptable() {
if (table != null)
table.ajax.reload(null, false);
setTimeout(refreshplayers
, 5000);
}
wen i upload on server my laravel project (i have active around 300 Users online)
CPU load going 200+
On my old PHP code (No Laravel) CPU load is 10-15
its something wrong with datatables or is laravel ?
i check the request is same no loops and all querys is same the only difference is laravel..
on TOP -C laravel use 3% my old code use 0.3% ..