Laravel 5 session()->keep method not working - laravel

I have a view with an input date field and a table beneath that. The table is populated based on the date entered. When the date is entered I use a POST method on the form which handles the DB request and returns the same view with the data. I'd like to also return the original date that was entered. I tried session()->keep and flashOnly methods. None return the input date to the view.
My controller:
public function groupTestAthletes(Request $request)
{
$inputDate = null;
$tests = null;
if ($request['tgroupdate']){
$inputDate = Carbon::createFromFormat('d/m/Y', $request['tgroupdate']);
$tests = Test::where('test_date', '=', $inputDate->format('Y-m-d'))
->orderBy('athlete_id', 'desc')
->get();
}
$request->session()->keep(['tgroupdate']);
//$request->flashOnly(['tgroupdate']);
return view('npr.test_athletes', ['tests' => $tests]);
My view:
<form class="form-inline" role="form" method="POST" action="{{ route('admin.search_tgroup') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="tgroupdate" class="control-label">Test Date</label>
<div class="input-group date" id="testgroupdatepicker">
<input name="tgroupdate" type="text" style="background-color:#ffffff" readonly=""
value="{{ Session::get('tgroupdate') }}" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search Athletes
</button>
<input type="hidden" name="_token" value="{{ Session::token()}}">
</div>
</form>

You dont need to save the date in session. You can save the date in a variable,send it to the view and in the view you can check if variable exist using isset php function.
In Controller
public function groupTestAthletes(Request $request)
{
$inputDate = null;
$tests = null;
if ($request['tgroupdate']){
$inputDate = Carbon::createFromFormat('d/m/Y', $request['tgroupdate']);
$tests = Test::where('test_date', '=', $inputDate->format('Y- m-d'))
->orderBy('athlete_id', 'desc')
->get();
}
return view('npr.test_athletes', ['tests' => $tests,'selected_date' => $request['tgroupdate']]);
And in the view,
<form class="form-inline" role="form" method="POST" action="{{ route('admin.search_tgroup') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="tgroupdate" class="control-label">Test Date</label>
<div class="input-group date" id="testgroupdatepicker">
<input name="tgroupdate" type="text" style="background-color:#ffffff" readonly=""
value="#if(isset($selected_date)) $selected_date #endif" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search Athletes
</button>
<input type="hidden" name="_token" value="{{ Session::token()}}">
</div>
</form>
Edit: This minor change in the view gave the optimal solution.
value="#if(isset($selected_date)){{$selected_date}}#endif"

Related

How to update or create at same time in laravel

``I had one page on that I'm Showing data from two table with left join and also edit button in front of every column when user click to edit button ,it goes a different page where user can update input fields but at the same time i want if some field is not showing data we can create data only when both the id of tables are matching
By this i can easily update but i can't create it shows error
Attempt to assign property "SchemeId" on null
Controller
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::find($SchemeId);
$scheme->SchemeId = $request->input('SchemeId');
$scheme->MerchantCode = $request->input('MerchantCode');
$scheme->BankAccountNumber = $request->input('BankAccountNumber');
$scheme->BankAccountIFSC = $request->input('BankAccountIFSC');
$scheme->save();
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}
**view**
<form action="{{ url('SchConfigration-update/' . $user->SchemeId) }}" method="post"
enctype="multipart/form-data">
#csrf
#method('PUT')
<input type="hidden" name="SchemeId" value="{{ $user->SchemeId }}">
<div class="form_row">
<div class="form_item">
<label>Scheme Name</label>
<input type="text" id="SchemeId" name="SchemeId"
placeholder="Enter scheme name" class="form-control" required
value="{{ $user->SchemeId }}">
{{-- value="{{ request()->SchemeId }}" --}}
{{-- if we want to pass scheme name just pass $user->SchemeName --}}
</div>
<div class="form_item">
<label>Merchant Code</label>
<input type="text" id="MerchantCode" name="MerchantCode"
placeholder="Enter Merchant Code" class="form-control" required
value="{{ $user->MerchantCode }}">
</div>
</div>
<div class="form_row">
<div class="form_item">
<label>Bank Account Number</label>
<input type="text" id="BankAccountNumber" name="BankAccountNumber"
placeholder="Bank account Number" maxlength="17" class="form-control" required
value="{{ $user->BankAccountNumber }}">
</div>
<div class="form_item">
<label>Bank Account IFSC</label>
<input type="text" id="BankAccountIFSC" name="BankAccountIFSC"
placeholder="Bank account IFSC" maxlength="11" class="form-control" required
value="{{ $user->BankAccountIFSC }}">
</div>
</div>
<div class="btn_row">
<input type="submit" value="submit" class="primary_btn">
<a class="btn btn-primary" href="{{ url('SchConfigration') }}"
role="button">Back</a>
</div>
</form>
`
Try This
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::updateOrCreate(
['SchemeId' => $request->input('SchemeId')],
[
'MerchantCode' => $request->input('MerchantCode'),
'BankAccountNumber' => $request->input('BankAccountNumber'),
'BankAccountIFSC' => $request->input('BankAccountIFSC')
]
);
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}

Laravel | Delete function - how to delete photo from calendar's event

How can I remove photo from calendar's event in edit calendar's event view? In list of events I did delete method and it works. Now when I try to do the same in edit.blade.php it gives error:
Call to a member function photos() on null
I have two tables in relationship one calendar to many photos, file upload works, but I stucked on edit part.
Look at my controller function:
public function deletePhoto(CalendarRepository $calRepo, $id)
{
$calendars = $calRepo->find($id);
$calendars->photos($id)->delete();
return redirect()->action('CalendarController#edit');
}
and here is fragment of edit.blade.php:
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}" style="width:100px; height:auto;"/>
</div>
<i class="fas fa-times"></i>Remove
</div>
#endforeach
</div>
</div>
I need to remove photo from Photo table and redirect to edit.blade.php (about the specific event id of the calendar)
Thanks for any help.
EDIT:
<div class="card-body">
<form action="{{ action ('CalendarController#editStore')}}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $calendar->id }}"/>
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}"/>
</div>
<form method="POST" action="{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
</div>
#endforeach
</div>
</div>
<div class="form-group">
<label for="header">Header</label>
<input type="text" class="form-control" name="header" value="{{ $calendar->header }}"/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{ $calendar->description }}"/>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" name="date" value="{{ $calendar->date }}"/>
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
</form>
</div>
You use the same $id to find the photo and the calendar instance.
GET request is not recommended for deleting a resource, so a better approach would be in your routes you can have something like this:
Route::delete('photo/{photo}', 'PhotosController#delete')->name('photo.delete');
Then in your view, you should surround the button with a Form, for example:
<form method="POST" action="{{ route('photo.delete', $photo) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
Then your confirm function in JS should submit the form if the user accepts to delete the photo. And also remember to return false as default in the confirm function so it does not submits the form by default.
Your controller will then be:
public function delete(Photo $photo)
{
$photo->delete();
return redirect()->back();
}
--- EDIT
Route::delete('calendar/{calendar}/photo/{photo}', 'CalendarController#deletePhoto')->name('photo.delete');
and the action in the form can be:
{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}
The method in the controller:
public function deletePhoto(Calendar $calendar, Photo $photo)
{
$calendar->photos()->where('id', $photo->id)->delete();
return redirect()->action('CalendarController#edit');
}

Error About Laravel Blade Foreach in Loop if else

There are problem my code about foreach and if condition. When I pulled else code block out of foreach loop there are no problem it is work. But I put in else code in loop. Else dont work and dont show anything.
My purpose, programme check to used user_id in total_bonuses datatable if ok I want to display only update form if no records user_id in total_bonuses I want to show store forum and user add to info. It seems to complicated.
My controller code is below
public function show($lang=null, $id)
{
$bonuses = DB::table('reservations')
->select(DB::raw('SUM(bonus) as total_bonus, user_id'))
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$dolars = DB::table('reservations')
->select(DB::raw('SUM(dolar) as dolar, user_id'))
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$confirmeds = DB::table('reservations')
->select(DB::raw('SUM(confirmation) as confirmed, user_id'))
->where('confirmation', '=', 1)
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$user = DB::table('users')->find($id);
$totals = DB::table('total_bonuses')->where('user_id', '=', $id)->get();
return view('wallet.show', compact('totals', 'user', 'bonuses', 'dolars', 'confirmeds'));
}
View Code is below
#foreach ($totals as $total)
#if ($total->new_dollar !== NULL)
<div class="col-12"><a href="javascript:void(0)" class="link"><i class="mdi mdi-check-circle text-success"></i> <font class="font-medium"> {!! $total->new_dollar !!}
Remaining Dollar</font></a></div>
#endif
</div>
</center>
</div>
<div>
</div>
</div>
</div>
<!-- Column -->
<!-- Column -->
<div class="col-lg-8 col-xlg-9 col-md-7">
<div class="card">
#if (count($total->user_id) === 1)
<br>
<br>
<label class="col-md-12"><h4> Paid Dollar</h4></label>
<form class="form-horizontal form-material" method="POST" action="{{ route ('total.update', ['lang' => App::getLocale(), 'id' => $total->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name='id' value='{!! $total->id !!}'>
<input type="hidden" name='user_id' value='{!! $user->id !!}'>
<div class="form-group">
<label class="col-md-12">Total Dollar</label>
<div class="col-md-12">
<input name="old_dollar" type="text" value="{!! $total->new_dollar !!}" class="form-control form-control-line" readonly>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Paid Dollar</label>
<div class="col-md-12">
<input name="paid_dollar" type="text" class="form-control form-control-line">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Note (Optionally! Not Required)</label>
<div class="col-md-12">
<input name="bonus_note" type="text" class="form-control form-control-line">
</div>
</div>
<input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
</div>
</div>
</form>
#else
<form class="form-horizontal form-material" method="POST" action="{{ route ('total.store', ['lang' => App::getLocale()]) }}">
{{ csrf_field() }}
<input type="hidden" name='user_id' value='{!! $user->id !!}'>
<div class="form-group">
<label class="col-md-12">Total Dollar</label>
<div class="col-md-12">
<input name="old_dollar" type="text" value="#foreach ($dolars as $dolar)
{{ $dolar->dolar }}
#endforeach" class="form-control form-control-line" readonly>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Paid Dollar</label>
<div class="col-md-12">
<input name="paid_dollar" type="text" class="form-control form-control-line">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Note (Optionally! Not Required)</label>
<div class="col-md-12">
<input name="bonus_note" type="text" class="form-control form-control-line">
</div>
</div>
<input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
</div>
</div>
</form>
#endif
#endforeach
Best wishes.
The problem in your provided view is with the if condition. Here you are trying to count the user id which is not possible:-
#if (count($total->user_id) === 1)
Change the above code to
#if (count($total) === 1)

How to autofill Edit form with old Information from two different tables?

I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}

Hidden type input value not being passed in Laravel gallery Project

I have created an album. I am trying to upload photos to the album and storing the album_id through the 'hidden' type input. When i check the source code, album_id is shown in 'value' attribute but unfortunately the value is not being passed to the query during form submission.
My create method of PhotoController which shows the form
public function create($id)
{
$albums = Album::where('id',$id)->first();
return view('admin.pages.photos',compact('albums', 'id'));
}
Here is the form.
<div class="container">
<div class="row">
<a class="btn btn-success" href="/gallery/{{$albums->slug}}">Back to Gallery</a>
<h4>Upload Photos to <strong>{{$albums-> name}}</strong> Gallery</h4>
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<img class="thumbnail" src="/images/gallery/{{$albums->cover_pic}}" alt="{{$albums->name}}">
</div>
<div class="col-md-8">
<form class="form-horizontal" action="/photo" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-8">Photo Title:</label>
<input type="text" name="photo_name" class="form-control" placeholder="Name of the Photo" value="{{ old('photo_name') }}" >
</div>
<div class="form-group">
<label class="col-md-8">Description</label>
<input type="text" name="desc" class="form-control" placeholder="Write Description" value="{{ old('desc') }}">
</div>
<div class="form-group">
<label class="col-md-8">Upload Pic</label>
<input type="file" name="photo" class="form-control" value="{{old('photo')}}" >
</div>
<input type="hidden" name="album_id" value="{{$albums->id}}">
<button type="submit" name="submit" class="btn btn-success waves-effect waves-light m-r-10">Submit</button>
</form>
and the store method
public function store(Request $request)
{
$this->validate($request, [
'photo_name'=>'required|min:3',
'desc'=>'required',
'photo'=>'required'
]);
$photo = new Photo;
$photo->album_id = $request->album_id;
$photo->photo_name = $request->photo_name;
$str = strtolower($request->photo_name);
$photo->slug = preg_replace('/\s+/', '-', $str);
if($file=$request->file('photo')){
$name = time().'.'.$file->getClientOriginalName();
$file->move('images/gallery', $name);
$photo['photo'] = $name;
}
$photo->desc = $request->desc;
$photo->save();
return redirect()->back()->with('status', 'Photo Successfully Added!');
}

Resources