I asked this question before but there was no response on it. So apologies. I'm asking it again
I have foreach loop here and I'm passing values from database in view to controller via GET route function. At first I click and send key from database to controller, then correct key is passed. By clicking second time on <a> tag previous key(value) is passed. I do not want to do this. I want to pass current key(value) from database. I spend 2 days in resolving this issue but failed. Do anyone know how can I resolve this issue?
Here is my code:
shopReq.blade.php:
#foreach($products as $key => $value)
<div href="{{route('special',$value->toCity)}}" id="tayyab"class="recent-container" data-toggle="modal" data-target="#shopping-request-modal">
<h1>I'm opening modal and sending value to Controller.<h1>
</div>
#endforeach
#include('modal')
web.php:
Route::get('special/{id}', [
'uses' => 'UserController#shopReq1',
'as' => 'special'
]);
UserController.php:
public function shopReq1($id){
echo $id;
$products = DB::table('shippingitems')->get();
return view('modal',compact('products'));
}
#foreach($products as $key => $value)
<div href="{{route('special',[ 'id' => $value->toCity ])}}" id="tayyab"class="recent-container" data-toggle="modal" data-target="#shopping-request-modal">
<h1>I'm opening modal and sending value to Controller.<h1>
</div>
#endforeach
Related
I have an anchor element that has a taget="_blank" attribute.
<span class="cell">
<a href="{{ route('contact.files.read', $file->uuid) }}" target="_blank">
{{ $file->name }}
</a>
</span>
Accessing the route via this element hides the request from telescope; request is not show in telescope entries.
Refreshes made in the newly opened tab are shown.
Removing the target attribute and using the element, produces normal results ( request is shown in telescope entries ).
Also the log method is not ran. It almost seems like the method is not called at all.
Anyone has any idea why?
This is the controller method used:
public function read(Request $request, MyFile $file)
{
Log::notice('read contact files',
[
'who' => $request->user()->id,
]);
return response()->file(storage_path('app/' . $file->path), [
'Cache-Control' => 'max-age=31536000',
]);
}
Route definition:
Route::get('/contact/file/{file}', 'MyController#read')
->name('contact.files.read')
my view page:
#if(empty($data))
<p>No response have been attached to this entities.</p>
#else
<p>By default, it will respond with the predefined phrases. Use the form below to customize responses.</p>
#endif
controller:
public function queries($companyID, $entityType, $entityValue)
{
$data = [];
$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach ($details AS $datum)
{
if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []];
$data[$datum->intent]['question'][$datum->queries] = $datum->id;
}
$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach ($detailsAns AS $datum)
{
if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []];
$data[$datum->intent]['answer'][$datum->reply] = $datum->id;
}
ksort($data);
return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
}
I made the controller and view shown above, but I can't seem to figure out what the problem is when there is no data it still shows like this:
I am trying to have it show the data but when there isn't data then for it to show something else.
I have two examples of with and without data when I dd();
first with data:
second without data:
so the one without data should shows something else like an error message.
$data is not empty in both cases, you need to be checking the answer index:
#if(empty($data['answer']))
<p>No response have been attached to this entities.</p>
#else
<p>By default, it will respond with the predefined phrases. Use the form below to customize responses.</p>
#endif
edit
You've also got an empty string index wrapping both answer and question so
#if(empty($data['']['answer']))
because you use empty() function to check data,but in controller $data is array, so It always not empty.
You can change empty() function to count() function. If $data is empty or null, it will be equal zero.
#if(count($data)==0)
<p>No response have been attached to this entities.</p>
#else
<p>By default, it will respond with the predefined phrases. Use the form below to customize responses.</p>
#endif
see this apple icons, these are rendering from foreach loop in view, defined variable in controller . i can change each icons but not able to assign link to each icon.
Hello guys i am stuck in linking using laravel controller.
my code is something like that: (i have a service page which has sub services page there is only one view which is using controller variable to showing data. i want each page button have different links. links can be static so i can putt it from controller. dont have any database attached to this solution. i want each href as a separate link)
This is view:
<?php $index=-1; ?>
#foreach ($third_fold_3_technologies_list as $item)
<?php $index++; ?>
<span class="{{ $item }}"></span>
#endforeach
This is controller:
$third_fold_3_technologies_list = [
'icon-appleinc',
'icon-appleinc',
'icon-appleinc',
];
You can use associative array.
$third_fold_3_technologies_list = [
'http://<link1>' => 'icon-appleinc',
'/<link2>' => 'icon-appleinc',
'<link3>' => 'icon-appleinc',
];
Then in your view
#foreach ($third_fold_3_technologies_list as $link => $class)
<span class="{{ $class }}"></span>
#endforeach
I've got a problem in Laravel. I have passed my whole table to my view like this from my controller:
$usersTable = DB::table('users')->get();
return view('users')->with('users', $usersTable);
In a foreach loop I can perfectly get each of the values like this in my view:
#foreach($users as $user => $value)
<div class="projectBox">
<br><span class="projectBoxName">{{ $value->name }}</span>
#php
echo Form::image('/images/edit.png', "",['class' => "editUserBtn", 'userId' => $value->id]);
#endphp
<br><span class="projectBoxSmallText projectBoxEmail">{{ $value->email }}</span>
<br><span class="projectBoxSmallText projectBoxId">ID: {{ $value->id }}</span>
<br><span class="projectBoxSmallText projectBoxProjects">Currently no projects</span>
</div>
#endforeach
But I also need to access these values outside my foreach loop, how can I do that?
echo Form::text('email', "$users->$value->email", array('placeholder' => "Email"));
Ain't working...
This gives my the whole object in this form
[{"id":"1","name":"Administrator","email":"admin#mail.com","password":"$2y$10$Re3Ahf.SwU5vj4UvtU5Dy.jxaZMsUNC2WhuJMwsNy9gu6TST4PuRG","remember_token":null}]
How to get only the email? I also tried using indexes, but those weren't working.
Thanks!
Edit:
Full situation:
I have a list of users with their extra information (mail, tel,...). In those user-boxes there is a button which says 'edit user' when I click that a modal opens giving the current mail and tel. So I can't say in my controller WHO's mailaddress to return because I only know that at the moment the user clicks a client-side button.
Images: http://imgur.com/a/krDrY
(Edit button is that small circle with three dots).
To access a collection without using loop, you should use collection methods:
$users = User::get();
$users->where('name', 'John Smith')->first()->email
This will not create any additional queries since you've already eager loaded data.
If you want to load just one user, use first() instead of get():
$users = User::first();
If you'll use get() and then [0] or first() like some guys recommend here, you'll load all users data into the memory for each request which will overload your server.
Also, using indexes to access data (like $users[4]['email']) is a bad practice. Avoid it if possible.
You need to add as first element by adding [0]
echo Form::text('email', $users[0]->email, array('placeholder' => "Email"));
I suggest use ->first() instead of ->get() to get single object. And remove loop and use anywhere you want.
You are using a collection of users to get the first user's email, you do
$users->first()->email;
Looks like it's coming in as JSON. Try json_decode($data) and $data->email to get that attribute.
I just starting learning laravel and was wondering how to pass data unrelated to the route to a controller. What I'm trying to accomplish is create a todo item that is able to have nested items.
View
<a class="btn btn-success" href="{{route('lists.items.create',4)}}">Create New Item</a>
The 4 is just a hard-coded example to see if it was working.
Controller
public function create(TodoList $list, $item_id = null)
{
dd($item_id);
return view('items.create', compact('list'));
}
So if your creating an item and don't pass in a parameter for id, it will default to null otherwise set it to whatever was passed in. However I'm getting a NotFoundHttpException. How would I be able to accomplish this.
Any help Welcome :)
You need to define the route, for example:
Route::get('create-item/{id}', [
'as' => 'lists.items.create',
'uses' => 'MyController#create'
])
Now, call the route like:
<a class="btn btn-success" href="{{route('lists.items.create', ['id' => 4])}}">Create New Item</a>