Laravel & Blade PHP - How would I call a Laravel Blade directive dynamically? - php

Is it possible to use a variable as the call to a Laravel Blade directive?
For my menu system, I've defined a component and I would like to be able to set the visibility of a link in the component. I have created numerous Blade directives (staff, admin, client, etc.) which check a user's role.
My component definition looks like this:
#component('components.primaryMenu', [
'items' => [
[
'route' => route('some.uri'),
'visibility' => ['staff', 'client'],
'label' => 'Item 1',
],
[
'route' => route('another.uri'),
'visibility' => ['everyone'],
'label' => 'Item 2',
],
]
])#endcomponent
What I would like to do is:
<ul class="nav">
#foreach($items as $item)
#foreach($item['visibility'] as $visibility)
#{{ $visibility }} // Should interpolate to #staff / #client
// Link stuff in here
#end{{ $visibility }} // Should interpolate to #endstaff / #endclient
#endforeach
#endforeach
</ul>
When I run this code I get "Invalid argument supplied for foreach()". I'm guessing because the #{{ $visibility }} declarations are throwing off the parser.
My Blade directives are defined in a service provider and look like this:
Blade::if('staff', function () use ($user) {
return $user->isType('staff');
});
Blade::if('client', function () use ($user) {
return $user->isType('client');
});

Related

Why the changes function does not return the changes?

652/5000
I am using in my project of Laravel 5 the package https://github.com/spatie/laravel-activitylog in its version 2.3. I have a page where I go through a list of activities and try to get the changes but it returns an empty array.
This is my Controller
public function history($id) {
$incidence = Incidence::find($id);
$activities = Activit::where('subject_id', $incidence->id)->get ();
return view('incidence.history', compact('activities'));
}
This is my html page
#foreach ($activities as $activity)
<p> {{ $activity->created_at }} </p>
<p> {{ $activity->changes() }} </p>
#endforeach
And this is the output in the browser
Clarify that I have done 4 update to the same record which I see reflected in the activity_log table of the database that uses the package. But I do not understand why the arrangement of the changes is not shown as indicated by the site's documentation:
Calling $ activity-> changes will return this array:
[
'attributes' => [
'name' => 'updated name',
'text' => 'Lorum',
],
'old' => [
'name' => 'original name',
'text' => 'Lorum',
],
];
Since it uses a database table I'm certain this has to do with the way Laravel allows methods to be chained. When you do $activity->changes() it expects you to continue the query. IE: $activity->changes()->where('etc', 1)->get() or something similar. So by calling $activity->changes() you are just sending a partial query to eloquent.

backpack laravel crud custom column

I have a task - insert a custom column in one of our admin pages, where I can to call method from Model for each entry, generated by backpack CRUD generator.
I have found in official documentations statement that looks like what I need here:
https://laravel-backpack.readme.io/v3.0/docs/crud-columns-types#section-roll-your-own
But there is nothing about how to implement this in the controller right way.
So I have tried to do just like this
Controller:
$status = [
'label' => 'Status',
'name' => 'status',
'type' => 'text'
];
$this->crud->addColumn($status);
and as mentinoned in documentation, I have create my own blade file in
resources\views\vendor\backpack\crud\columns
Here it is:
{{-- status --}}
<td>{{ $entry->isBlocked }}</td>
Where isBlocked is method in my Model. I have an error about database and nothing is working.
Please say is it even possible to do what I wnat and if it is - please say how to do it right way both in view and controller
Thankyou!
Let's check your code
$status = [
'label' => 'Status',
'name' => 'isBlocked', // your column name
'type' => 'status' // your blade name, e.g status.blade.php
];
$this->crud->addColumn($status);
and inside status.blade.php
{{-- status --}}
<td>{{ $entry->{$column['name'] }}</td>
Any question, please comment

tagging a new model - preloaded array produces an error - Laravel

I use this package as an egine for my tagging functionality:
https://github.com/rtconner/laravel-tagging
Now, when creating new model, in my controller I declare a variable with all available tags:
$tags = array('' => 'Enter your tags...') + Tag::orderBy('name', 'asc')->lists('name', 'name')->all();
Then, in my create view I define this field:
{!! Form::select('Tags[]', $tags, Input::old('Tags') ,['multiple' => 'multiple', 'data-tags' => 'true', 'data-token-separators' => "[',', ' ']", 'data-placeholder' => "{$tags['']}", 'class' => '']) !!}
Problem
when I write a first tag, say politics, two tags appear in the window: the politics and the empty tag defined in the controller '' => 'Enter your tags...'
I need to remove the tag manually each time I use the form.
When I remove the array('' => 'Enter your tags...') + part of my variable,
I get error
`Undefined index: (View: resources/views/events/create.blade.php)`
My question
how to make the select box work properly.
Thx.

Cannot upload picture in laravel 4

I have this basic form:
{{ Form::open(array('url' => URL::route('post-account-changeProfilePic'), 'files' => true, ))}}
{{ Form::file('photo') }}
<br />
{{ Form::submit('Regístrarme', array("class" => "button expand round")) }}
{{ Form::close() }}
My Route is inside of 2 groups: before=>Auth and before=>csrf
Route::post('/accont/changeProfilePic', array(
'as' => 'post-account-changeProfilePic',
'uses' => 'CallCenterController#postChangeProfilePic'
));
In my controller, I just dump my variable to see what I got:
public function postChangeProfilePic(){
$input = Input::all();
var_dump($input);
}
These are the errors I am getting:
1- Illuminate \ Session \ TokenMismatchException.
This because of the csrf filter, but since I am using blade, the token is actually there. Also, if I remove the name attribute from the file input, this error will not be displayed.
So far, I decided to place the route outside of the csrf filter until I understand what is going on.
2- After placing the route out of the csrf filter, and try to display all the inputs, I get a null array.
I decided to add a new text field and if I don't select a photo/pic and only send the form like that, it'll dump on screen all the inputs, but of course, the file is empty/null.
Any idea about what I am doing wrong?
In your form try changing this to
array('before' => 'csrf'), function()
{{ Form::open(array('url' => URL::route('post-account-changeProfilePic'), 'before' => 'csrf'), 'files' => true, ))}}
The csrf token should be there by default since you are doing a POST request, so not sure where there is an issue there.
http://laravel.com/docs/4.2/html#csrf-protection
for the input try
public function postChangeProfilePic(){
if (Input::hasFile('photo'))
{
$input= Input::file('photo');
var_dump($input);
}
}
http://laravel.com/docs/4.2/requests#files
Okay I think it could be this
change
{{ Form::open(array('url' => URL::route('post-account-changeProfilePic'),
'files' => true, ))}}
to
{{ Form::open(array('route' => 'post-account-changeProfilePic',
'files' => true )) }}
I don't think you need the ',' at the end of the true either.

Laravel nested resource route parameters in html forms & urls

Im essentially trying to see if there is a more efficient or proper way of accessing route parameters in the views of a nested resource. The code below demonstrates what I'm doing, catching all parameters from the route: /schools/1/classes/2/teachers/4/assignments
into the controller index method, and then making a view and passing it all of those parameters so that within the view I can make forms and links that use the same route format & parameters. Is there a better way? Laravel Paste
//
// app/routes.php
//------------------------------------------------------
Route::resource('schools.classes.teachers.assignments', 'AssignmentsController');
//
// app/controllers/AssignmentsController.php
//-------------------------------------------------------
public function index($school_id,$class_id,$teacher_id)
{
$routes = array($school_id,$class_id,$teacher_id);
$assignments = $this->assignment->all();
return View::make('assignments.index', compact('assignments'))
->with('routes', $routes);
}
//
// app/views/assignments/index.blade.php
// ------------------------------------------------------------
<p>{{ link_to_route('schools.classes.teachers.assignments.index', 'All Assignments', array($routes[0],$routes[1],$routes[2])) }}</p>
//
// app/views/assignments/edit.blade.php
// -------------------------------------------------------------
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes[0],$routes[1],$routes[2],$route[3]))) }}
-
You always need to pass the parameters and this is simple but I think it would be better if you use an associative array like this instead:
$routes = compact('school_id', 'class_id', 'teacher_id');
So it'll become:
$routes = array(
'school_id' => $school_id,
'class_id' => $class_id,
'teacher_id' => $teacher_id
);
So, you can use:
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes['school_id'], ['class_id'], ['teacher_id']))) }}
Looks more readable and easy to understand.

Categories