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
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.
Below is the code to show current exam to student.If he already applied then he cannot apply for the same.When user logged in I stored there value in Session.put('Student.id',$user[0]->id).Now when i run below code I am getting the error:
undefiend variable:$admission_id in alert(I used YAJRA DATATABLE).
**$admission_id** = Session::get('student.id');
$exam=DB::table('exam')->where('id_exam_category','2')->get();
return Datatables::of($exam)
->addIndexColumn()
->addColumn('exam_name', function($row){
return $row->exam_name;
})
->addColumn('action',function($row){
$stu=DB::table('exam_student')
->where('id_exam',$row->id)
->where('admission_id',**$admission_id**)
->get();
if(count($stu) > 0) {
$ret='<button class="btn btn-block btn-default" disabled>Appered <i class="fa fa-caret-right"></i></a>';
}else{
$ret='Start <i class="fa fa-caret-right"></i>';
}
return $ret;
})
->rawColumns(['action'])
->make(true);
Even if i print $admission_id...It is printing. Session.id has student id and not empty..
Any Suggesstion ?
Thank You
I have an HTML form defined as follows:
<form action="{{route('save.checkout')}}" method="POST">
{{csrf_field()}}
<input name="amount" type="hidden" value="{{session()->get('cart')->totalprice}}">
<div class="cart_navigation">
<a class="continue-btn" href="#">
<i class="fa fa-arrow-left"> </i> خرید را ادامه دهید
</a>
<a class="checkout-btn" href="{{route('save.checkout')}}">
<i class="fa fa-check"></i> ادامه به پرداخت
</a>
</div>
</form>
The following error is being thrown when submitting the form:
The GET method is not supported for this route. Supported methods: POST.
my route is:
route::post('/savecheckout','BasketController#checkout')->name('save.checkout');
and the checkout function:
public function checkout(Request $request){
$user = auth()->user()->id;
$order = new order();
$order->user_id = $user;
$order->amount = $request->input('amount');
$order->status = 0;
$order->save();
$order = order::where('status' ,0)->where('user_id', $user)->first();
return view('checkout.index', compact('order'));
}
i solved the problem . it was a silly mistake i changed my button type to submit
if you are beginning with Laravel so i would like to contribute something to make your programming better & safer for future projects.
always validate your input
use of try & catch blocks
show appropriate error message to your users so that you can also
debug it in future.
so the modified code will be look like:
public function checkout(Request $request){
// use Validator; (add this after namespace to import validator)
$validator = Validator::make($request->all(),[
'user_id' => 'required|integer|max:11',
'amount' => 'required|numeric',
'status' => 'sometimes|integer|max:1',
]);
if($validator->fails()) {
return back()->withErrors($validator);
}
try {
$user = auth()->user()->id;
$order = new order();
$order->user_id = $user;
$order->amount = $request->input('amount');
$order->status = 0;
$order->save();
$request->session()->flash('message', 'Order Successfully Created');
$order = order::where('status' ,0)->where('user_id', $user)->first();
return view('checkout.index', compact('order'));
} catch (\Exception $e){
dd($e->getMessage()); // it will show the error message with, you can replace this block with redirect code or anything else..
}
}
for showing error & success message in front-end use below code in your checkout > index.blade.php template (just a sample code, you can make it more better by using your own CSS & styles)
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
#if(count($errors) > 0)
<div class="note note-error">
<h4>Error..!!</h4>
<p>
#foreach($errors->all() as $error)
<div class="alert alert-danger fade in m-b-15">
<i class="fa fa-chevron-right"></i>
{{ $error }}
<span class="close" data-dismiss="alert">×</span></div>
#endforeach
</p>
</div>
#endif
I am using datatables from https://datatables.net/examples/server_side/simple.html and I am able to display all the information to the tables.
Now I have a column which stores foreign key as shown below.
The other table which is being pointed by source_of_member is
so here i have these kind of relationship and in frontend datatables my data are visualized as
Howeber here instead of foreign keys in Member Source column I want those Name to appear from another table which matches the id
my member model is as:
class Member extends Model
{
protected $fillable = ['name','mobile_number','organization_name','source_of_member','relationship_manager','referred_by'];
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'id');
}
}
and my memberSource model is as:
class MemberSource extends Model
{
protected $fillable = ['name'];
public function member()
{
return $this->hasOne('App\Member', 'id');
}
}
what i tried is:
public function getdata()
{
$members = Member::all();
return Datatables::of($members)
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
}
i dont know how to do this any help would be greatly appreciated.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In Controller:
$members = Member::with('memberSource')->get();
return Datatables::of($members)
->addColumn('source_of_member', function ($member) {
$member_name = '';
if(!empty($member->memberSource->name)){
$member_name = $member->memberSource->name;
}
return $member_name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
With the relationships working properly, you can simply edit the column in the datatable:
$members = Member::with('memberSource');
return Datatables::of($members)
->editColumn('source_of_member', function ($member) {
return $member->memberSource->name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
However, your relationship configuration seems wrong, because your foreign key is called source_of_member and therefore laravel can't automatically detect it.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In your MemberSource model:
public function member()
{
return $this->belongsTo('App\Member', 'source_of_member');
}
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>