Laravel Auth Return - laravel

I created a function in users model to return permission and return as obj. but when i type {{ Auth::user()->permission()->outlineAgreements }} it said "htmlspecialchars() expects parameter 1 to be string, object given". How can i fix it ?
PS: inside test value is an array
"{"outlineAgreements":["view"],"purchaseOrder":["view"],"pwra":["view","create","update","delete"]}"
public function permission()
{
$permissions = auth()->user()->getAllPermissions()->pluck('name');
foreach ($permissions as $key => $value) {
$module = last(explode(" ", $value));
$action = current(explode(" ", $value));
$result[$module] = $result[$module] ?? [];
array_push($result[$module], $action);
}
return json_decode(json_encode($result));
}

Php is complaining about an object print. It expects that the data you are instructing it to print is a string.
Use dd to print out the return of the permissions method for debugging. This way you can see more clearly what data you are about to print out.
{{ dd(Auth::user()->permission()) }}
{{ dd(Auth::user()->permission()->outlineAgreements) }}
If your first box represents that payload data, and you need to access and print all outlineAgreements permissions, and it is an array, you can use implode:
{{ implode(', ', Auth::user()->permission()->outlineAgreements) }}
You can loop through that array too:
#foreach(Auth::user()->permission()->outlineAgreements as $permission)
{{ $permission }}
#endforeach
Hope it helped!

Related

Laravel: How to solve my custom translation's fallback problem

I know we can use {{ __(messages.welcome) }}. I think that it's troblesome to call by file path and word each and every time.
{{ __(sales.orders.id) }}
{{ __(sales.orders.code) }}
{{ __(sales.orders.first_name) }}
{{ __(sales.orders.last_name) }}
And it's too long.
So I invented a method.
use Lang;
class OrderController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// Language
$lang = (object)[];
foreach (Lang::get('common/common') as $key => $value) {
$lang->$key = $value;
}
foreach (Lang::get('sales/order') as $key => $value) {
$lang->$key = $value;
}
$data['lang'] = $lang;
In blade:
{{ $lang->code }}
{{ $lang->first_name }}
{{ $lang->last_name }}
Now I found that this has no fallback function. If a string is translated in English, but not in other language, and I visit that language, error shows
Undefined property: stdClass::$first_name ...
Is there any other way to implement fallback translation?

Attempt to read property "degree" on null

i have created a profile where users can add their education fields.
when there is no value in the database it throws an error. how can i get rid of this ? Attempt to read property "degree" on null.
public function myEducations()
{
return $this->hasMany('App\Models\Education','user_id')->orderByDesc('endDate');
}
controller
public function myProfile(\App\Models\User $user)
{
$user = Auth::user();
$education = $user->myEducations->first();
return view('candidate.profile',compact('user','education'));
blade
{{ $education->degree }} - {{ $education->fieldOfStudy }}
If the users of you application are filling in their education details later only. You should ideally catch this condition when rendering your view. For example you could try the following:
#if ($education)
{{ $education->degree }} - {{ $education->fieldOfStudy }}
#else
<p>Education details not available</p>
#endif
You have to inspect in an if statement $education->degree is not null.
If not null, degree has value, this part will render.
Else, it doesn't have value so else block appears in template.
#if(null !== $education->degree)
{{ $education->degree }} - {{ $education->fieldOfStudy }}
#else
// there is no degree
#endif

Laravel 5.5 - Save multiple data continiously from blade

I want to make a PHP method using laravel. I want to do the comparison of criteria and criteria. Here is the controller code :
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
return view('kriteria_kriterias.create')->with('kriteria1', $kriteria1)->with('kriteria2', $kriteria2)->with('data', $data);
}
and this is the blade code :
It will make the form appear as total of criteria#
The problem is, I can't save it all to database. How do I get it to do this?
Updated method in the controller to the following:
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
How to output in the blade file:
{{ $kriteria1 }}
{{ $kriteria2 }}
Or you update the controller to pass the complete results:
public function create($id1, $id2)
{
$kriteria1 = Model\Kriteria::find($id1);
$kriteria2 = Model\Kriteria::find($id2);
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
And the in the blade you can accss the data in various ways, one way is a foreach loop using blade in the blade template:
#foreach($kriteria1 as $k1)
{{ $k1 }}
#endforeach
#foreach($kriteria2 as $k2)
{{ $k2 }}
#endforeach'
To accept multiple values dynamicaly in the controller you can try something like this:
public function create($ids)
{
$results = collect([]);
foreach($ids as $id) {
$kriteria = Model\Kriteria::findOrFail($id);
if($kriteria) {
$results->put('kriteria' . $id, $kriteria);
}
}
return view('kriteria_kriterias.create')->with($results);
}
Then use the same looping method mentioned above to display them in the blade or a for loop that gets the count and displays accordingly.
maybe you forgot to add the opening tag ;)
{!! Form::open(array('url' => 'foo/bar')) !!}
//put your code in here (line 1-34)
{!! Form::close() !!}

Eloquent: get related model with aliases

I have a view where a related model property gets displayed.
{{ $product->category->title_en }}
I'd rather put this like
{{ $product->category->title }}
And make the locale selection in my controller.
For example with the main model:
View:
{{ $product->title }}
Controller:
if ($locale === 'en') {
$product = Product::where(id of something)->get([
'title_en AS title'
])
}
How can I set aliases for the related?
Or is there a better option?
I've created a helper function for this for my own project. As the locale is the first in all urls, I explode it and use it like this.
function translate($model, $column)
{
$url = explode('/', Request::path());
return $model[$column . '_' . $url[0]];
}
In the view file, I use:
{{ translate($product, 'title') }}
Alternatively you can use App::getLocale() to get the current locale.
you can use select() but you have to write names of other columns too in select()
if ($locale === 'en') {
$product = Product::where(id of something)
->select('title_en as title','col2','col3'....)
->get()
}

laravel 5.2 entrust - show roles foreach user

My controller is
public function index()
{
$users = User::paginate(15);
return view('dash.users.index')->with(array('users' => $users));
}
in view i pass: {{ $item->roles }} and return all column as array, if i put {{ $item->roles->get('name') }}the table is blank, why?
I want to show user->roles->name foreach user
If it's an array, you should only simply use this:
{{ $item->roles['name'] }}
solved with nested foreach! #foreach($item->roles as $item) {{ $item['name'] }} #endforeach
inside my #foreach user
Thank you Mojtaba for the help!

Resources