I want to have model property containing array of files from specific directory
public function getGalleryAttribute($value) {
return Storage::disk('storage')->files($value);
}
then I output on the page
#foreach ($item->gallery as $img)
<img src="/storage/{{ $img }}" >
#endforeach
and I see error
Attempt to read property "gallery" on array (View: /var/www/site/resources/views/components/postpartials/gallery.blade.php)
I cannot understand what is the problem
if ($item->gallery->isNotEmpty()) {
}
try checking if the gallery property exists in the array and if there is some sort of data stored in that
just use dd($item);
it seems that it was mistype. Issue is closed.
Related
I am learning how to use vue with laravel. I have basic loops working well to pull direct model relationships, but I can't figure out how to access model methods in a loop. Many of my Larvel models have basic information formulated with a method pulling data from related models. I've tried to research it and think the answer might be some combination of eager loading, preformating the answer as a json response or maybe something with axios, but the snipits I've found aren't clear on what goes where, or what needs to be in place for them to work correctly. I've tried both eager loading and using a json response and neither has worked. I can access methods in simple vue components that are just text, but not in a loop where the variable isn't part of the page.
Example: I want to use Vue to display a list of ingredients on a recipe's page. The ingredient "title" is a method pulling the information from a related model.
RecipeController.php
public function show(Recipe $recipe)
{
$ingredients = $recipe->ingredients;
$view = $this->view('recipes.show');
//(variable in the view, variable defined in current function)
$view->with('recipe', $recipe);
$view->with('ingredients', $ingredients);
return $view;
}
Recipe.php
public function ingredients()
{
return $this->hasMany('App\Models\Ingredient', 'recipe_id', 'recipe_id');
}
Ingredient.php
public function title()
{
$title = $this->item->title();
return $title;
}
public function vueTitle()
{
$title = Ingredient::title()->get();
return response()->json($title );
}
Recipes/show.php
<div>
<ul>
<li
is="test-li"
v-for="ingredient in {{ $ingredients }}"
v-bind:key="ingredient.ingredient_id"
v-bind:title= "ingredient.vueTitle"
v-bind:id="ingredient.ingredient_id"
></li>
</ul>
</div>
I'd prefer to reuse the same methods, but created a new one to try converting to json first but that didn't work (or I'm doing it wrong). I tried eager loading, but it either did nothing, or generated an error (Call to a member function on null) if I tried to eager load the specific method. I've tried various combinations of binding and not binding the title component. I've also tried title= "{{ingredient->title()}}" but that syntax errors.
How can I get the result of the Laravel method in a Vue loop?
After more searching, I found this post which described how to add an accessor to a model. Doing so allowed me to access my custom method as if it were a standard direct relationship. It was a straightforward modification and will reduce complexity in a number of places. I made the following modifications:
Ingredient.php
Added the get..Attribute() function and appended the array
...
protected $table = 'ingredients';
...
protected $appends = array('title');
// Access methods as direct relationships
public function getTitleAttribute()
{
return $this->title();
}
Recipes/show.php
Bound the title prop to the ingredient title as if it were a direct relationship.
<div>
<ul>
<li
is="test-li"
v-for="ingredient in {{ $ingredients }}"
v-bind:key="ingredient.ingredient_id"
v-bind:title= "ingredient.title"
v-bind:id="ingredient.ingredient_id"
></li>
</ul>
</div>
Another example, hope one may find it helpful:
Model.php
/**
* Accessor for Age.
*/
protected $appends = ['age'];
public function getAgeAttribute()
{
return Carbon::parse($this->attributes['dob'])->age;
}
VueFile.vue
<td>
<span v-bind:age="user.age"> {{user.age}} </span>
</td>
I am trying to display certain content in my blade files based on if variables are set. But variables are being set that I'm not setting.
I have GameController.php:
use App\Models\Game;
class GameController extends Controller
{
public function index() {
$games = Game::latest()->get();
return view('game.index', [
'games' => $games
]);
}
...
and then in the layout blade file I do this:
#isset($game->name)
<h1>{{ $game->name }}</h1>
#endisset
So I'm setting $games, but I am NOT setting $game. however, $game is set. I want this header to show if I do a show() and $game would be set, but it shouldn't be showing in the index() where $game is not set. Is this normal behavior, and if so what's a better way for me to handle this?
$games = Game::latest()->get();
in code above get() returns collection (may be called array) of games, and you will need #foreach directive in blade template to loop through the games collection.
if you want only one latest game then you should use $games = Game::latest()->first();
I have found the issue. It was lack of understanding of how #extend and #section work.
index.blade.php extends layout.blade.php
#extends ('layout')
it appears all of index processes before layout. Because of that a foreach in index, despite technically being below the header if you were to view source on the web page, was processing before the header and setting $game. I just refactored the variable used in the foreach and that resolved my issue.
Thank you kerbh0lz, you technically helped me find it.
I am trying to retrieve data from database and check if the data is empty or not. What problem I am facing is that html is showing even if the data is empty. I want to ignore the html tag like example ul li. Here how i tried is like
#if(!empty($jobseekers->skill_1))
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
#endif
I want to ignore "My Skill is " if the data is empty. I don't want to show anything.
When using ->get() you can't simply use any of the below:
if (empty($jobseekers->skill_1)) { }
if (!$jobseekers->skill_1) { }
if ($jobseekers->skill_1) { }
But, When you are getting data with first() method, You can simply use all above methods.
Because if you dd($jobseekers->skill_1); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results.
I think you are using !empty() on data with ->get() method which will always return true even data is empty. You need to use other way.
To determine if there are any results you can do any of the following:
if (!$jobseekers->skill_1->isEmpty()) { }
if ($jobseekers->skill_1->count()) { }
if (count($jobseekers->skill_1)) { }
If you get $jobseekers with get() method you can not use empty($jobseekers )
instead of empty you can use other conditions :
#if($jobseekers->skill_1 != '')
in this condition you check skill_1 as empty string
also
#if($jobseekers->skill_1)
and etc
replace your code with below code and check it:
#if($jobseekers->skill_1 != '')
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
#endif
you should use isset()
#if(isset($jobseekers->skill_1))
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
#endif
you can use count method
#if(count($jobseekers->skill_1)>0)
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
#endif
#if(count($data)>0)
//write your code which you want to show if data is available
#endif
Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}
In my view I output data from my database in the view via:
{{ $data->id }}
In one particular view I need do not get the data from a database but need to keep the view the same and manually set the id.
I've tried setting the id in my controller like:
$data['id'=>1];
But this fails to output in the view with:
{{ $data->id }}
Where am I going wrong?
You would want:
$data = new stdClass();
$data->id = 1;
Creating something like that will create an object (like an array, but accessed with $data->id instead of $data['id'].
To make your code simpler, Laravel has magic getters which means you can use $data['id'] in your view for data from the database. Then, when not using the database, you could do $data['id'] = 1; in your code rather than the two lines above.