i have the following route:
Route::get('notes/main', function(){
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
return View::make('notes.main')
->with('destinations', $destinations);
});
//the relationship models:
<?php
class Destination extends Eloquent {
public function notes()
{
return $this->has_many('Note');
}
}
<?php
class Note extends Eloquent
{
public function destination()
{
return $this->belongs_to('Destination');
}
}
//View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{ $notes->destination->text }} // this isn't echoed
#endforeach
what's the correct way to filter this and define $destination->id
thanks
How would i Filter the notes in an if statement inside the loop ?
#if (isset($note->title) != 'shorttext')
#else
<p> {{ $note->text }} </p>
#endif
#endforeach
You use $destination->id in your Route but it seems to be not defined yet. The question is, what do you want to achieve with your code?
With:
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
you are getting only the Notes of one specific destination (so far, $destination is not defined. You could use Destination::all()->first() to get the first, or Destination::find(id), with ID being replaced with the primary key value of the destination you need).
But I guess you don't want it that way. From your Output it seems like you want to have an output with each destination and below each destination the corresponding Notes.
Controller:
//gets all the destinations
$destinations = Destination::where('show', '=','1')->get();
return View::make('notes.main')
->with('destinations', $destinations);
View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
#foreach($destination->notes as $note)
{{ $note->text }} <br>
#endforeach
<hr>
#endforeach
Didn't test this, but this way your View would show you all your Destinations and for each Destination all the Notes.
In your route, your not sending $notes variable to the view.
How I would do it:
$destinations = Destination::where('show', '=','1')->get();
$destinations->notes = Destination::find($destination->id)->notes()->get();
Then in view:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{{ $destination->notes->text }}
#endforeach
So first you asked a question, I gave you a correct answer.
You marked my answer as accepted, but later on you edit your initial question to add another question (for which you should create a new thread instead).
Then you create your own answer which is only answering part of your initial question and mark that as the good solution?
I could tell you why your isset and empty did not work, but why should I if you don't value my help at all?
OK
for the last part this worked out at the end
function isset and empty didn't work , strange:
#if ($note->title == 'shorttext')
Related
I have a table with many columns and some of those columns have spaces in their names (i.e. 'Provider First Name'). I know the syntax to use these in Blade helpers and am using this in other parts of my app: {{$provider->{'Provider First Name'} }}. This works fine in other parts.
I have the following in my ProviderController:
public function show($id)
{
$provider = NPIData::where('NPI', $id)->first();
$providers = NPIData::all();
return view ('profiles.provider', compact('provider', 'providers'));
}
I have brought in the NPIData Model to the controller.
I have the following in my provide.blade.php file:
#extends('layouts.profiles')
#section('content')
<div>
{{ $provider->NPI }}
{{$provider->{'Provider First Name'} }}
</div>
#endsection
Oddly, the NPI will pull in, but the 'Provider First Name' does not. I have tried many other columns with spaces and none of them work. I even copied and pasted from other parts of my app where the syntax to pull these in works and it does not work here.
Instead of:
{{$provider->{'Provider First Name'} }}
Try this:
#php
$providerFirstName = $provider->{'Provider First Name'};
#endphp
{{ $providerFirstName }}
UPDATE
If not you can always go with array access:
{{ $provider['Provider First Name'] }}
In blade, If we want to check that the current route matches with a route or not, we can simply use:
#if(Route::currentRouteName() == 'parameter')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
But what if we want to match it with a wildcard like:
#if(Route::currentRouteName() == 'parameter.*')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
Is there any solution for that?
I have tried "*" and ":any", but it didn't work.
Note: I want to check route, not URL.
Any help would be appreciated.
Thanks,
Parth Vora
Use Laravel's string helper function
str_is('parameter*', Route::currentRouteName())
It'll return true for any string that starts with parameter
I had the same problem. I wanted to toggle an active class based on a URI.
In blade (Laravel 6x), I did:
(request()->is('projects/*')) ? 'active' : ''
You can also make use of Blades Custom If Statements and write something like this in your AppServiceProvider.php:
public function boot()
{
Blade::if('route', function ($route) {
return Str::is($route, Route::currentRouteName());
});
}
then you can use it in a blade view like this:
<li #route('admin.users*') class="active" #endroute>
Users
</li>
I have a blog and a quotationfamous sayings repository on one site.
The quotations are tagged and the entries are tagged too.
I use this rtconner/laravel-tagging package.
Now, what I want to do is to display ALL Quotation models which share the same tags as article.
The Eloquent syntax is simple, as the original docs provide an example:
Article::withAnyTag(['Gardening','Cooking'])->get();
possible solution
Optional routing parameters. The asker-picked answer in this question gives a solution:
//in routes.php
Route::get('/{book?}/{chapter?}/{topic?}/{article?}', 'controller#func');
//in your controller
public function func($book = null, $chapter = null, $topic = null, $article = null) {
...
}
my problem
In my app the shared tags might count more than 3 or 5. I will soon get an example with even 10 tags. Possibly more
My question
Does it mean that I have to construct an URL with 10 optional routing parameters? Do I really need sth like this:
Route::get('quotations/tags/{tag1?}/{tag2?}/{tag3?}/{tag4?}/{tag5?}/{tag6?}/{tag7?}', 'controller#func');
my question rephrased
I could create a form with only a button visible, and in a hidden select field I could put all the tags. The route would be a POST type then and it would work. But this solution is not URL-based.
I think you could process the slashes, as data:
Route::get('quotations/tags/{tagsData?}', 'controller#func')
->where('tagsData', '(.*)');
Controller:
public function controller($tagsData = null)
{
if($tagsData)
{
//process
}
}
Ok, this is my solution. As I have a tagged model, I dont't need to iterate through tags in url to get the whole list of tags.
The enough is this:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}', 'QuotationsController#all_tagged_in_model');
Then in my controller:
public function all_tagged_in_topic($itemtype, $id) {
if($itemtype == 'topic') {
$tags = Topic::find($id)->tags->pluck('name')->all();
$topic = Topic::find($id);
}
if($itemtype == 'quotation') {
$tags = Quotation::find($id)->tags->pluck('name')->all();
$quotation = Quotation::find($id);
}
// dd($tags);
$object = Quotation::withAnyTag($tags)->paginate(100);;
And it is done.
Now, the last issue is to show tags in the URL.
TO do that, the URL should have an extra OPTIONAL parameter tags:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}/{tags?}', 'QuotationsController#all_tagged_in_model');
And in the {url?} part you can just write anything which won't break the pattern accepted by route definition.
In your view you might generate an URL like this:
// A button to show quotes with the same set of tags as the article
// generated by iteration through `$o->tags`
<?php
$manual_slug = 'tag1-tag2-tag3-tag4`;
?>
<a href="{{ URL::to('quotations/all-tags-in/article/'.$o->id.'/'.$manual_slug) }}" class="btn btn-danger btn-sm" target="_blank">
<i class="fa fa-tags icon"></i> Tagi:
</a>
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 !!}
I have a relationship between two models which is define by :
class Appartement extends Eloquent {
public function lang()
{
return $this->hasOne('AppartementLang')->where('language_id', '=', '2');
}
}
So nothing big.
When I try to do something like :
#foreach ($appartements as $appartement)
{{$appartement->lang->title}}<br/>
{{$appartement->lang->subtitle}}<br/>
{{link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover'))}}<br/>
#endforeach
And that the lang does not exists, then laravel throws an error, which is pretty logical.
I wanted to know if there is some magical way which would allow me not to automate the verification that the relation exists. Basically not turn it into a big "if exists then use" for each property of the class like that :
#foreach ($appartements as $appartement)
#if($appartement->lang){{$appartement->lang->title}}<br/>#endif
#if($appartement->lang){{$appartement->lang->subtitle}}<br/>#endif
{{link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover'))}}<br/>
#endforeach
Thanks for the advice on that.
I am sure there are lots of ways to do it (or at least try)
There is this option to check for existing and supply default if none, that I think is new in 4.1 (or at least it is new to me):
#foreach ($appartements as $appartement)
{{ $appartement->lang->title or "default if doesn't exist" }}<br/>
{{ $appartement->lang->subtitle or "default text if doesn't exist" }}<br/>
{{ link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover')) }}<br/>
#endforeach
or you could try one of these three if statements if you don't want anything to display for that particular record depending on existance of lang...
#foreach ($appartements as $appartement)
#if ($appartement->lang)
// or maybe:
// #if ($appartement::has('lang'))
// or also maybe:
// #if ($appartement->lang->count())
{{ $appartement->lang->title or "default if doesn't exist" }}<br/>
{{ $appartement->lang->subtitle or "default text if doesn't exist" }}<br/>
{{ link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover')) }}
<br/>
#endif
#endforeach