How to use Youtube Class on View - php

I am using Youtube package
I added this on app.php
Alaouy\Youtube\YoutubeServiceProvider::class,
&
'Youtube' => Alaouy\Youtube\Facades\Youtube::class,
now i m trying in view like this
{{ Youtube::getVideoInfo('rie-hPVJ7Sw') }}
but getting error
"htmlspecialchars() expects parameter 1 to be string, object given
(View: C:\xampp\htdocs\guru\resources\views\admin\video\index.blade.php)"
how can I fix this?
i Tried
#foreach($videos as $video)
{{ var_dump(Youtube::getVideoInfo($video->videoid)) }}
#endforeach
its showing me everything
Here is Screenshot

As per the docs,
// Return an STD PHP object
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
so you could do,
{{ var_dump(Youtube::getVideoInfo('rie-hPVJ7Sw')) }}
to print out the object or assign it to a variable and access the respective object properties
EDIT
To show only the embed html you could use the following code
#foreach($videos as $video)
{!! Youtube::getVideoInfo($video->videoid)->player->embedHtml !!}
#endforeach

Related

Laravel 5.6 - Grab message from Controller into view

I'm starting with Laravel and i need to show the output of a post request into the view. My controller file returns an array with a message, like this:
return redirect('/myroute')
->with('message', [
'type' => 'success',
'text' => 'It works'
]);
In my view file, i'm trying to grab the message text, but no success. See my code below
#if(Session::has('message'))
{{ $msg = Session::get('message') }}
<h4>{{ $msg->text }}</h4>
#endif
The point is: The condition works, if i changed the {{$msg->text}} to any text it works, but when i try to get the message text, it returns an error:
htmlspecialchars() expects parameter 1 to be string, array given
So, any help is apreciated. If more information is needed, just ask.
PS: i checked this question, but no success at all
EDITED:
PS2: Can't change controller structure
Try accessing the array as follows:
<h4>{{ $msg['text'] }}</h4>
or just pass an array with the items
->with([
'type' => 'success',
'text' => 'It works'
]);
//in the view
#if(session()->has('text'))
<h4> {{ session('text') }} </h4>
#endif
-- EDIT
iterate than over the session like so:
#foreach (Session::get('message') as $msg)
{{$msg['text']}}
#endforeach
you can read more about that here
Do this instead
return redirect('/myroute')->with('success','It worked');
Then on your view
{{session('success')}}

Echo value in Laravel Blade

How can I get the value 'name' under 'rekenplichtige' in my Blade template? If I do this: {{ $vestiging->rekenplichtige->name }} I get the following error: ErrorException Trying to get property of non-object
That syntax is working to get the 'naam' under 'gemeente'. I have no idea about what's going wrong.
{{ $vestiging->rekenplichtige}}
since "name" inside an array, you should array notation to get name. Like below
{{ $vestiging->rekenplichtige['name'] }}
{{ $vestiging->rekenplichtige()->first()->name }}
This is working.

laravel undefined index error while retrieving notification data from database

I have created notification to store order submit event as
return [
'cart_id'=>$this->cart->id,
'text'=>'order is submitted'
];
when i retrieve notifications data from database as
#foreach (Auth::user()->unreadNotifications as $notification )
{{$notification->data['text'] }}
#endforeach
this gives error as undefined index text
but if i try to access text via
#foreach ($notification->data as $key => $data)
{{$data}}
#endforeach
it works
but why cant i can access data via $notification->data['text']
You should get the text attribute directly like :
#foreach (Auth::user()->unreadNotifications as $notification )
{{ $notification->text }}
//Or use
{{ $notification['text'] }}
#endforeach
Hope this helps.

Laravel Passing Variable from Forms

I am brand new to Laravel, and following a super basic tutorial.
However the tutorial did not come with an edit record section, which I am attempting to extend myself.
Route:
Route::controller('admin/products', 'ProductsController');
Controller:
class ProductsController extends BaseController
{
public function getUpdate($id)
{
$product = Product::find($id);
if ($product) {
$product->title = Input::get('title');
$product->save();
return Redirect::to('admin/products/index')->with('message', 'Product Updated');
}
return Redirect::to('admin/products/index')->with('message', 'Invalid Product');
}
..ECT...
I realise the controller is requesting an ID to use, but I cannot figure out how to pass it a product ID when the form is posted/get.
Form:
{{Form::open(array("url"=>"admin/products/update",'method' => 'get', 'files'=>true))}}
<ul>
<li>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title') }}
{{ Form::hidden('id', $product->id) }}
..ECT...
{{ Form::close() }}
my initial idea was to pass the product id within the form URL like:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
But no luck with that either.
The error I get is:
Missing argument 1 for ProductsController::postUpdate()
Interestingly if I type directly into the URL:
http://localhost/laravel/public/admin/products/update/3
It works and the item with id 3 is altered fine.
So can anyone help and inform me how to pass the id with a form?
Thanks very much
The first Problem here ist the following:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
the {{product->id}} is wrong in two ways:
it should be {{$product->id}}
BUT it wouldn't work anyway because the inner {{..}} inside of the {{Form::...}} won't be recognized since it is inside a string and therefore part of the string itself.
You either have to write it this way:
{{Form::open(array("url"=>"admin/products/update/".$product->id, 'files'=>true))}}
or you give your route a name in your routes.php file and do it this way:
{{Form::open(array('route' => array('route.name', $product->id, 'files'=>true)))}}
I prefer the second way.
You also might want to look into Form Model Bingin

laravel passing database array from controller to view

hello people here is my code below for controller
$siteinformation = DB::table('siteinformation')->where('Site',$myarr[0]['site'])->select()->get();
print_R($siteinformation); will display
Array ( [0] => stdClass Object ( [Site] => site1 [Nickname] => friend [ProgramID] => 1 [CreateDateTime] => 2014-06-03 18:05:39 ) )
I am trying to pass it to view
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
In my view i have...
#if(Session::has('siteinformation'))
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif
Iam gettng an error Undefined variable: siteinformation
my route file contains... Route::post('sbs_site', array('uses' => 'myController#sys_config'));
what could be the problem??
I think you don't need to redirect; probably you are doing it wrong, instead you should pass the data to the view directly using something like this:
$siteinformation = DB::table('siteinformation')
->where('Site',$myarr[0]['site'])
->get();
// View Name: "sbs_site.blade.php"
return View::make('sbs_site')->with('siteinformation', $siteinformation);
Then in the view:
#foreach ($siteinformation as $value)
{{ $value->Site }}
{{ $value->Nickname }}
#endforeach
But if your really need to use a redirect then you may do it like this:
// Route Is: "sbs_site"
return Redirect::to('sbs_site')->with('siteinformation', $siteinformation);
Then in your view:
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation') as $value)
{{ $value->Site }}
{{ $value->Nickname}}
#endforeach
#endif
This worked for me...
in view
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation')as $key => $value)
{{ $value->Nickname}}
#endforeach
#endif
In controller
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. You set in your controller 'siteinformation' as a normal variable. If you want to set session, instead of this.
Redirect::to('sbs_site')->with('siteinformation',$urserializedobj);
Do this
Session::put('siteinformation',$urserializedobj);
Redirect::to('sbs_site');
OR
Simply don't check session in your view instead of this
if(Session::has('siteinformation'))
Just do this
if (!empty($siteinformation)) :
I don't know why u are redirecting to pass it to the view
return View::make('yourview',array('siteinformation'=>$siteinformation));
This will do the trick if you can make the view directly from the controller
BUT
if u need to redirect than i suppose you can serialize your array or change it to json first because the with function passes the data as flash so only strings
$urserializedobj = $siteinformation.toJson();//if this doesn't work try serealizing
return Redirect::to('sbs_site')->with('siteinformation',$urserializedobj );
now in your view you can do this
#if(Session::has('siteinformation'))
$siteInformation = json_decode(Session::get('siteinformation'));
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif

Categories