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.
Related
I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here
I need to use diffForHumans() function in laravel blade file
I have use a data array to echo the data in blade view {{$data['time']}}
{{$data['time']}}
i expect the out is 2 minutes ago .
You can do the following:
// in your view add this
{{ Carbon\Carbon::parse($data['time'])->diffForHumans() }}
// or when you prepare your $data array in the controller or wherever
$data['time'] = $model->created_at->diffForHumans();
if the $data['time'] is an instance of carbon u can basically do {{$data['time']->diffForHumans()}}
I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')
How to show TeamController#index and ProductController#index both show list of team and product inside one view main.blade.php
Looks like you want to show two datasets on one page. Basically, it means you have to execute two controller methods but it's not necessary to follow each and everything that official documentation says.
For example, if Products belong to a team, you can execute only TeamController#index and show products as given below.
#foreach($teams as $team)
#foreach($team->products as $product)
{{ $product->name }}
#endforeach
#endforeach
If no teams and products are two different entities and does not have any relation, you can just pass teams and products like this:
TeamController.php
public function index()
{
$teams = Team::all();
$products = Product::all(); // Don't forget to include 'use App\Product'
return view('index',compact(['teams','products']);
}
and then you can show teams and products like this:
index.blade.php
#foreach($teams as $team)
{{ $team->name }}
#endforeach
#foreach($products as $product)
{{ $product->name }}
#endforeach
Getting information from two different models does not mean you have to execute two different controller functions.
Still, if you want to get data from two different controllers, you can setup index.blade.php and create two ajax requests that will get data from two different URLs (two different controller methods).
Let me know if you have any more questions.
You can't show results from two controllers like that. Create a view that includes both the view that TeamController#index and ProductController#index return. be aware that both might be extending a layout which will probably try to load your page twice, so keep in mind to split the views into smaller components and include only those.
More info here
https://laravel.com/docs/5.6/views#creating-views
I'm passing a $emailtemplate variable into a view.
This $emailtemplate is a model with the following properties
(string) 'from_name', (string) 'from_email', (string) 'subject', (string) 'body', (string) 'email_type', (integer) 'status'
User's can create new instances of the $emailtemplate via a FORM on the site, where they can populate each of the fields above.
An example of the body as populated by users into the FORM would be exactly the following line (yes users will be writing HTML code into the form to be stored in the body property of the $emailtemplate):
<strong>Dear {{$user->first_name}} </strong>
I have created a view which will allow users to 'Preview' the email. The variables $emailtemplate and $user are both passed to the view (for the case of the preview the $user = Auth::user())
resources/views/emails/preview.blade.php:
{!! $emailtemplate->body !}}
This 'preview' view correctly renders the strong styling however does not identify the blade variable reference as it is stored within a literal string.
i.e. the HTML that is rendered is exactly
Dear {{$user->first_name}}
What is going wrong here? I want to allow Users to develop their own email templates via a form. I am passing both the $emailtemplate and $user into the view however because the $emailtemplate->body is a string datatype Laravel does not recognise the use of the blade variable between the parenthesis.
How can I fix this?
Thanks for your help
You can't do this since Laravel wont Re-render the blade output, to see and replace any variables that is injected by another variable!
Take a look at https://github.com/TerrePorter/StringBladeCompiler
So, you should evaluate the user defined template first, e.g.
<strong>Dear {{$user->first_name}} </strong>
will become:
<strong>Dear Foo </strong>
This evaluated <strong>Dear Foo </strong> will be stored in $evaluatedTemplate
Then you can use this in your main blade template:
{!! $evaluatedTemplate !!}
Also see:
https://github.com/Flynsarmy/laravel-db-blade-compiler