I have a question regarding showing data from my database in Laravel.
I get the following error:
Trying to get property 'first_name' of non-object
It refers to this line of code:
#foreach ($contact as $c)
<h1 class="display-4">Bekijk details voor contact: {{ $c->first_name }} {{ $c->last_name }}</h1>
#endforeach
I get this data from my database by using Laravel's 'show' function as described below:
public function show($id)
{
$contact = Contact::find($id);
return view('contacts.show', compact('contact'));
}
My routing looks like this:
Route::resource('contacts', 'ContactController');
The reason I can't get my head wrapped around this error is because it seems to work just fine for other functions like Laravel's 'edit' function as described below:
public function edit($id)
{
$contact = Contact::find($id);
return view('contacts.edit', compact('contact'));
}
Any help would be appreciated, I would like to know why it is not working for my 'show' function whilst it is working for my 'edit' function, are there any differences I am not aware of?
Thanks in advance!
Kind regards,
Geert-Jan Knapen
Very likely, $contact is the object rather than collection, so you do not need to loop through it. you can access it directly.
<h1 class="display-4">Bekijk details voor contact: {{ $contact ->first_name }} {{ $contact ->last_name }}</h1>
Update it's better to use route model binding, so it handles if the contact does not exist.
public function show(Contact $contact)
{
return view('contacts.show', compact('contact'));
}
Related
I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}
Stuck on an annoying problem and I feel like I need another set of eyes.
For some reason I can't get this formbuilder generated field to be found by the controller in Symfony.
Here's the relevant piece of the controller
$barcode = $request->get('barcode');
$em = $this->getDoctrine()->getManager();
//this checks for whether an item still exists
$items = $em->getRepository('Bundlename:Items\Item')
->itemsNotDisposedByBarcode($barcode);
if ($items) { .... do stuff ...}
else { $this->get('bundle.flashbag')
->addError('Item not found.');
Nothing ever seems to be found.
I tested this by running the conditional off of $barcode so it's pretty certainly the twig or the form (in other words, I'm pretty certain that it's not itemsNotDisposedByBarcode, there appears to be nothing wrong with the query)
Here's what the formbuilder looks like:
class DisposeItemBarcodeType extends AbstractType
{
protected $editors = array();
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('barcode','text',
array('required' => false));
}
public function getName()
{
return 'bundle_items_item';
}
}
I keep thinking it might be the twig somehow, however it looks fine to my eyes; not sure if it's the formbuilder, or if I need to hack this apart and just use a regular form. Here's what I have for this:
{{ form_start(form, {'action': path('bundle_item_dispose_post'),'attr': {'id': 'dispose-item-form','novalidate': 'novalidate'}}) }}
{{ form_errors(form) }}
<section>
<div class="panel-body" id="barcode">
{{ form_row(form.barcode, {'attr': {'name':'barcode','class': 'barcode-field', 'autofocus': true}})}}
</div>
I have a feeling I'm missing something obvious but my eyes are not seeing it.
===
Edit: solved with help from below
Turns out formbuilder does some tokening, so I just needed to do this:
$barcode = $postData['bundle_items_item']['barcode'];
I think that the problem is that you wanna retrieve a POST variable not a GET, to get this try this please:
$postData = $request->request->all();
$barcode = $postData['barcode'];
i am new to larvel.I tried to pass the variable from controller toview but it did not worked.
i am getting an error:
"Whoops, looks like something went wrong."
code used in controller:
public function showWelcome()
{
return View::make('hello', array('theLocation' => 'NYC'));
}
Code in hello.blade.php:
<h1 class="highlight">Blade has arrived in {{ $theLocation }} .</h1>
can you tell me is there any syntax error in the above code and is there any possibility of debugging the error??
There are many ways to pass data from Controller to View like:
return view('hello')->with(['key' => 'value']);
or
return view('hello', ['key' => 'value']);
And you can use it on view like:
<p>{{ $key }}</p>
Controller
return view('hello')->with(['theLocation' => 'NYC']);
View
<h1 class="highlight">Blade has arrived in {{ $theLocation }} .</h1>
This is a weird error. I can add a new record to the database fine with a new UserEdit method, but if I try to update a record. Nothing. It doesn't even file a blank value, or issue an error.
What am I missing? To try to eliminate issues, I tried running the core update method in the documentation like so:
protected function create()
{
$save = UserEdit::find(715);
$save->First_Name = 'Jeffrey';
$save->Last_Name = 'Way';
$save->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
This problem is the same as my earlier question here, but I didn't think it appropriate to double up the questions and this is technically a different problem. My blade just prints an array of the user data at the moment so I know if it's working or not:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="col-md-6">
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
</div>
#stop
The Routes:
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
I'm assuming I'm missing some crucial minor detail, but for the life of me, can't find what. Anybody got an idea where to look?
public function store(Request $request)
{
$user = new UserEdit;
//$user->name = $request->name;
$user->First_Name = "foo";
$user->Last_Name = "bar";
$user->save();
return $user;
}
Try this function, maybe you had a mistake in your code.