Currently, after images are loaded onto the page, if the user deletes an image the page is refreshed and if they end up deleting all images then an error will appear on the screen saying that the $count variable is undefined. How can this be if the page is refreshed? I know that the form at the bottom needs to have the count variable but currently, that variable is coming from inside the first form so I can't put the form in the other.
What should I do?
{!! Form::open() !!}
... Rest of form inputs
#if (count($project->getImages()) > 0)
#foreach ( $project->getImages() as $count => $asset)
<div class="delete-toggle-container">
<i class="fa fa-times-circle-o"></i>
</div>
#endforeach
#endif
{!! Form::close() !!}
{!! Form::open(['url' => '/route/resource/' . $project->id . '/images', 'method' => 'DELETE', 'id' => 'image-delete-' . $count]) !!}
{!! Form::hidden('image', $asset, ['readonly' => true] ) !!}
{!! Form::close() !!}
Maybe you could define $count if there are no images:
#if (count($project->getImages()) > 0)
#foreach ( $project->getImages() as $count => $asset)
// Some content here
#endforeach
#else
<?php $count = 0; ?>
#endif
It's not the prettiest but it might work.
You probably should not be relying on a variable that is set via a foreach loop to exist outside of the loop. If the loop doesn't run, there is nothing to iterator over, then that variable won't be set.
Also that is a loop, do you always want the last 'key' from what was iterated as the value you need to open that form? Is that the intent or are you trying to just get an actual count to use?
Side Note, to replace the if then foreach on the same value ...:
#forelse (...)
iterating
#empty
nothing to iterate
#endforelse
If you are inside a loop you have the $loop variable available to get current index, iteration, etc ...
Laravel - Docs - Blade - Loops
Laravel - Docs - Blade - The Loop Variable
Related
I have abstracts table in my database with the Abstract_Status_ID column, I am trying to create a button that changes every Status from '2' to '3'
I have tried to do this:-
My controller
public function publish($A_ID)
{
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
my route
Route::put( '/projects', 'AbstractsController#publish');
Route::post( '/projects', 'AbstractsController#publish');
my view (projects)
Tried with the csrf token and without as eloquent docs says any post/put will be restricted without it.
#if (count ($abstracts)> 0)
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{!! Form::open(['action' => ['AbstractsController#publish' , $abstract->A_ID], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
#endif
#endforeach
The error I am getting when clicking the button
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\AbstractsController::publish()
Also, the code above will show more than one button, any suggestions to make one button change all of them ?
A: If you want to have a button for each ABSTRACTS just change your route to :
Route::put( '/projects/{A_ID}', 'AbstractsController#publish');
B: If you want to have only one button that change all , you can echo their ID in hidden input then send form
so your view would be:
#if (count ($abstracts)> 0)
{!! Form::open(['action' => ['AbstractsController#publish'], 'method' => 'post' ]) !!}
{{ csrf_field() }}
{{Form::hidden('_method', 'PUT') }}
#foreach ($abstracts as $abstract)
#if($abstract->Abstract_Status_ID == 2)
{{Form::hidden('abstract_ids[]', $abstract->A_ID) }}
#endif
#endforeach
{{Form::Submit('Submit Changes',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
And Your Controller :
public function publish()
{
foreach(request('abstract_ids') as $A_ID){
$abstracts = Project::find($A_ID);
$abstracts->Abstract_Status_ID= 3;
$abstracts->save();
}
return redirect('/AdvertisedProjects')->with ('success', 'Abstracts Published' );
}
You are using form method="POST". I think it should be method="PUT".
The main problem in your code is the definition of your routes, You are passing an argument ($A_ID) to the controllers publish method,
But in your routes, you did never mention that you are passing any argument through your route. So for passing the argument, you can use {$A_ID} in your route URL.
One thing you should always remember is that you should always give your route a name and call it by using its name and route() Laravel helper method.
The benefit of using route name is that if you decide to change the URL-Structure of your route later in the development process, you'll never have to change it everywhere in your application because you did not call it by its URL.
And route name is less error prone
In total, You should define your routes like this
Route::put('projects/{A_ID}/update', 'AbstractsController#publish')->name('projects.update');
Route::post('projects/{A_ID}/post', 'AbstractsController#publish')->name('projects.store');
Then in you blade files now you can call it like
{!! Form::open(['action' => route('projects.store', $abstract->A_ID), 'method' => 'post' ]) !!}
Hope this will clarify and resolve your problem.
I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);
I just started learning laravel and I have the following situation:
I have a table which is called 'cities' and a table 'users'. To connect them I have a table users_cities.
I am trying to do a form using laravel
#extends('welcome')
#section('content')
<h1>Cities</h1>
#foreach ($cities as $city)
{!! Form::checkbox($city->name, $city->id, null, ['class' => 'field']) !!}{!! $city->name !!}
#endforeach
#foreach ($citiesSelect as $citySelect)
{!! $citySelect->id !!}
#endforeach
#stop
And it`s bringing the name of the cities on the first foreach, and on the second is bringing the ids of the cities that should be checked.
Basically is bringing the entries on the database which has the id_user selected.
But I can't figure out how to bring the checkboxes on the first foreach selected using the result of the second foreach.
first create array of city ids like this
$selectedCitiesIds = array_map(function($city) {
return $city->id;
}, $citiesSelect);
then
{!! Form::checkbox($city->name, $city->id, (in_array($city->id, $selectedCitiesIds)), ['class' => 'field']) !!}{!! $city->name !!}
simply add a 'true' argument to your statement like such:
Form::checkbox($city->name, $city->id, true, ['class' => 'field'])
You can do this too.
In this case you have an array called cmbbolsas and in the checkbox input you can use it
#foreach ($cmbbolsas as $key=>$value)
<input type="checkbox" name="bolsas" value={{$key}}> {{$value}}</br>
#endforeach
I hope it works for you :3
hello people here is my code below for controller
$siteinformation = DB::table('siteinformation')->where('Site',$myarr[0]['site'])->select()->get();
print_R($siteinformation); will display
Array ( [0] => stdClass Object ( [Site] => site1 [Nickname] => friend [ProgramID] => 1 [CreateDateTime] => 2014-06-03 18:05:39 ) )
I am trying to pass it to view
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
In my view i have...
#if(Session::has('siteinformation'))
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif
Iam gettng an error Undefined variable: siteinformation
my route file contains... Route::post('sbs_site', array('uses' => 'myController#sys_config'));
what could be the problem??
I think you don't need to redirect; probably you are doing it wrong, instead you should pass the data to the view directly using something like this:
$siteinformation = DB::table('siteinformation')
->where('Site',$myarr[0]['site'])
->get();
// View Name: "sbs_site.blade.php"
return View::make('sbs_site')->with('siteinformation', $siteinformation);
Then in the view:
#foreach ($siteinformation as $value)
{{ $value->Site }}
{{ $value->Nickname }}
#endforeach
But if your really need to use a redirect then you may do it like this:
// Route Is: "sbs_site"
return Redirect::to('sbs_site')->with('siteinformation', $siteinformation);
Then in your view:
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation') as $value)
{{ $value->Site }}
{{ $value->Nickname}}
#endforeach
#endif
This worked for me...
in view
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation')as $key => $value)
{{ $value->Nickname}}
#endforeach
#endif
In controller
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. You set in your controller 'siteinformation' as a normal variable. If you want to set session, instead of this.
Redirect::to('sbs_site')->with('siteinformation',$urserializedobj);
Do this
Session::put('siteinformation',$urserializedobj);
Redirect::to('sbs_site');
OR
Simply don't check session in your view instead of this
if(Session::has('siteinformation'))
Just do this
if (!empty($siteinformation)) :
I don't know why u are redirecting to pass it to the view
return View::make('yourview',array('siteinformation'=>$siteinformation));
This will do the trick if you can make the view directly from the controller
BUT
if u need to redirect than i suppose you can serialize your array or change it to json first because the with function passes the data as flash so only strings
$urserializedobj = $siteinformation.toJson();//if this doesn't work try serealizing
return Redirect::to('sbs_site')->with('siteinformation',$urserializedobj );
now in your view you can do this
#if(Session::has('siteinformation'))
$siteInformation = json_decode(Session::get('siteinformation'));
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif
I'm having a hard time figuring out how to repopulate a form for edit that has check boxes in it. I think the most confusing part is because they are coming from a pivot table.
I have users, permissions, and users_permissions tables.
For a quick demonstration of what the tables look like, I ran this query and included a screen clip of the results.
return $userPermissions = User::with('permissions')->find($id);
In my form I just have two permissions check boxes for now until I get the concept working, then I will add a foreach loop and grab them all from the database, but now I have the following:
<div class="form-group">
<label>
{{ Form::hidden('permissions[4]', '0', ['class' => 'checkbox-inline']) }}
{{ Form::checkbox('permissions[4]', '1', ['class' => 'checkbox-inline']) }}
Manage Content
</label>
</div>
<div class="form-group">
<label>
{{ Form::hidden('permissions[3]', '0', ['class' => 'checkbox-inline']) }}
{{ Form::checkbox('permissions[3]', '1', ['class' => 'checkbox-inline']) }}
Manage Users
</label>
</div>
I'm not sure if this is useful information, but when I first create the user, I create a permissions array to attach to the new user. Here is the code for that,
public function createUserPermissionsArray($input)
{
$permissionsArray = [];
$permissions = $input['permissions'];
foreach ($permissions as $id => $value)
{
if ($value == 1)
{
array_push($permissionsArray, $id);
}
}
$this->saveNewUserToDatabase($input, $permissionsArray);
}
I really need some direction here about how to solve this problem. Thanks
I recently worked on something similar, except I had groups with permissions assigned to them, which users were then assigned to. For example, for GroupController::edit() I had this code:
public function edit($id)
{
if(!permitted('group.edit')) {
return Redirect::route('user.dashboard');
}
$group = Group::find($id);
$permissions = Permission::all();
return View::make('admin.group.edit', [
'group' => $group,
'permissions' => $permissions,
'assigned' => $group->permissions->lists('id')
]);
}
Then within the form partial for the view (admin.group.partials.form), I had the following code to handle permissions:
#foreach($permissions as $permission)
<div class="row">
#if(isset($assigned))
{{ Form::checkbox(
'permissions[' . $permission->ident .']',
$permission->id,
in_array($permission->id, $assigned))
}}<label for="permissions">{{ $permission->ident }} - {{ $permission->description }}</label>
#else
{{ Form::checkbox(
'permissions[' . $permission->ident .']',
$permission->id)
}}<label for="group">{{ $permission->ident }} - {{ $permission->description }}</label>
#endif
</div>
#endforeach
I've formatted it the best I can, so that it's easy to read. Basically I had a create.blade.php and edit.blade.php which both include the form partial, but each file handles the opening and closing of the form (model binding for edit, normal open for create), hence the if statement.
Unfortunately I couldn't find a better method to achieve this. I hope this helps you.
P.S: As a side note, I've actually written a tutorial about creating a robust and simple ACL with Laravel, I can link if you'd like.