In my routes I have this route defined as:
// app/routes.php
Route::resource('CharacterController');
The corresponding method in the controller is:
// app/controllers/CharacterController.php
public function index(){
$characters = Character::all();
$houses = House::all();
return View::make('characters.index')->with(array('characters'=>$characters, 'houses' => $houses));
}
Finally, in the view:
// app/views/characters/index.blade.php
#this fires an error:
{{ $houses[$characters->house_id]->name }}
# at the same time this gives correct result:
{{ $houses[1]->name }}
# and this IS equal to 1:
{{ $characters->house_id }}
You can't use the id as index of the array to access the object with given id.
Since you have an Eloquent Collection you can use its various functions. One of them being find() for retrieving one item by id
{{ $houses->find($characters->house_id)->name }}
Related
Taking the following code as an example, how can I use the param paramenter inside the index view?
Route::get('foo/{param}', [FooController::class, 'index']);
Have you index method of the controller take a parameter, $param, and pass it to your view:
public function index($param)
{
return view('index', ['param' => $param]);
}
Then in the Blade view:
{{ $param }}
Or, you could get the route parameter from the request directly in the view if needed:
{{ request()->route('param') }}
I have Post model which has user function returning User model - creator of the post.
class Post extends Model
{
/**
* Get the user that owns the post.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
In blade view I achieved how to print author name
{{ $post->user->name or 'Anonymous' }}
Above code works, but it is very hmm sensitive?
For example, I tried change this code to:
{{ $post->user->name, 'Anonymous' }}
<?php $post->user->name or 'Anonymous' ?>
Results? Trying to get property of non-object error on line with this code.
Maybe I am skipping something simple but important. How to print URL or default value in blade view. Pseudo code what I mean:
{{ '' or 'Anonymous' }}
Try
{{ isset($post->user->name) ? '' : 'Anonymous' }}
If this doesn't work (I haven't check it) try something like this:
#if (isset($post->user->name))
#else
Anonymous
#endif
Actually the error is not because the code is sensitive, it is because you are actually trying to access non objects values.
To achieve what you are looking for:
{{ isset($post->user)?'' : 'Anonymous' }}
I use ::find to find the business but business_position I need to use WHERE business_id = and WHERE id to find the correct position, and example is avalible below...
<?php
$s = Stats::find(Auth::user()->id);
$myJob = \App\Businesses::find($s->business_id);
$matchedValues = ['business_id' => $s->business_id, 'id' => $s->business_position];
$myPosition = \App\BusinessPositions::where($matchedValues)->get();
?>
And using $myPosition I use
{{ $myPosition->pay_amount }}
And on that line it throws and error...
Undefined property:
Illuminate\Database\Eloquent\Collection::$pay_amount (View:
C:\Users\Darren\MyLavProject\resources\views\pages\business\business.blade.php)
Laravel's get() returns a collection of results that match your query (even if there are none or just one, it'll be an array of Laravel model instances).
You can use first() instead of get() to grab the first result, or you can foreach through each of the results get() provides. It depends if your application permits more than one permission per business_id/id combination.
$myPosition This is an array of objects.
So if you need the result:
{{ $myPosition[0]->pay_amount }}
Or
#foreach($myPosition as $position)
{{ $position->pay_amout }}
#endforeach
Of if you don't want to use any of these:
Change your query to :
$myPosition = \App\BusinessPositions::where($matchedValues)->first();
Now you can use: {{ $myPosition->pay_amount }}
in MySql database i have systemSetting table and this have any data.
in this table i have this fields :
id - site_copyright - date
i want to fill form with this single row.
{{ Form::model($siteSettings) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::close() }}
UPDATE:
in CONTROLLER of that i have this:
public function getIndex()
{
$siteSettings = SystemSetting::all();
return View::make('back_end.layouts.manageHeaders')->with('siteSettings', $siteSettings);
}
in Result, form could not parse data to input fields.
i get error for this :
$siteSettings->site_copyright
This is a duplicate of many questions, such as this: Laravel display table record using query
The short answer is: use first() method to get a single entry.
You're using form model binding, so change your controller to:
public function getIndex()
{
$siteSettings = new SystemSetting;
return View::make('back_end.layouts.manageHeaders', compact('siteSettings'));
}
Query work on 5.1 or more
return DB::table('users')->where('id', $id)->value('field_name');
//if lower version of laravel like 4.2
DB::table('users')->where('id', $id)->pluck('field_name');
I am trying to return a specific value in my Laravel view. I define a variable in my controller and pass it on to my view. This is what I get when my blade code looks like this:
<h1>{{ $sport }}</h1>
However, since this is returned in my view and I only want to have the "sport" itself I did this:
<h1>{{ $sport->sport }}</h1>
Undefined property: Illuminate\Database\Eloquent\Collection::$sport
Any help would be much appreciated.
Thanks
You are referencing an Array Collection not an object itself
Try
<h1>{{ $sport->first()->sport }}</h1>
Or modify your controller like this
// Controller
return View::make('sports', array('user' => $sport->first()));
// View
{{ $sport->sport }}