Get movie title from TMDb with Laravel - php

I'm trying to get a movie title from TMDb API but:
ErrorException
Trying to access array offset on value of type int
Controller
$getPopuler = Http::withToken(config('services.tmdb.token'))
-> get('http://api.themoviedb.org/3/movie/popular')
-> json();
dump($getPopuler);
return view('pages.index', [
'getPopuler' => $getPopuler
]);
The result of dump($getPopuler) gives this.
Blade
#foreach ($getPopuler as $populer)
<div class="card text-center">
<div class="container">
<img class="img-fluid img-thumbnail" src="{{ asset('assets/image/poster01.jpg') }}" alt="Poster-01">
</div>
<div class="card-body">
<h6 class="card-title">{{ $populer['title'] }}</h6>
<i class="fas fa-star"><span class="ml-1">85%</span></i>
<p class="tahun"><small>Mar 20, 2020</small></p>
Read More
</div>
</div>
#endforeach
Thanks in advance.

Assuming your API call is working, I think you need to access the results array of the API response, as per the documentation.
#foreach ($getPopuler['results'] as $populer)
Side note: 'populer' is spelled 'popular'.

Related

Why do I get Trying to get property 'username' of non-object while "owner_user" has information in it?

HTML Code
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
#endforeach
Laravel code
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it.
relations work, but somehow I get "Null" as a result in {{ $post->owner_user->username }} when I return it in HTML code.
because that is not a single object try
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">#(foreach $post->owner_user as $user){{$user->username }}#endforeach</p>
</div>
</div>
#endforeach

Get all images from the database and display the images in a bootstrap 4 carousel Laravel 5.8

I've been trying to get all the images from the database and trying to display them in a bootstrap carousel but it is only returning one image.
<div class="carousel-item active">
#if($posts->active == 1)
<img src="{{ Storage::url("image/{$posts->image}") }}" alt="..." class="d-block img-fluid">
#endif
<div class="carousel-caption d-none d-md-block">
<h3 class="text-white">{{ $posts->title }}</h3>
<p>{{ $posts->h1 }}</p>
</div>
</div>
And here is the code in the controller index function
$posts = Post::latest()->first();
When I try to change the method to get() instead of first() an error appears. I'm not sure if I need to setup a new variable for the images, or include a foreach in the view which I'm not sure how to do. Really appreciate the help.
<div class="carousel-item active">
#foreach($posts as $post)
#if($post->active == 1)
<img src="{{ Storage::url("image/{$post->image}") }}" alt="..." class="d-block img-fluid">
#endif
<div class="carousel-caption d-none d-md-block">
<h3 class="text-white">{{ $post->title }}</h3>
<p>{{ $post->h1 }}</p>
</div>
#endforeach
</div>
You can access your posts like this if you are using get() method.
Since first() method directly returns you the model you can access its properties but for get() you will have to loop through it since it returns "Eloquent Collection' (not array, you can use toArray() if you need one).

Displaying data on view blade from database Laravel Notifications

I am new with working laravel. I have faced with a problem, when I am trying to display data on website using laravel notification.
This is my code:
GameBiddedNotification.php:
public function toDatabase($notifiable)
{
return[
'title' => $this->details['title'],
'text' => $this->details['text']
];
}
This is database template, in data column:
{"title":"hg","text":"\u10db\u10dd\u10d7\u10d0\u10db\u10d0\u10e8\u10d4 \u10e8\u10d4\u10db\u10dd\u10d5\u10d8\u10d3\u10d0"}
And this is my blade:
#foreach(auth()->user()->unreadNotifications as $notification)
#php
$data = json_decode($notification,true);
$test = $data['title'] ['text'];
#endphp
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
{{-- <p class="preview-subject mb-1">{{ $notification->data['title'] }}</p> --}}
<p class="text-muted ellipsis mb-0">{{ $test }}</p>
</div>
</a>
#endforeach
I have tried multiple methods for example:
{{ $notification->data['title'] }}
, but result is the same. I am getting always an error saying
ErrorException (E_ERROR)
Undefined index: title
Based on your question, title and text is stored in 'data' column,
#foreach(auth()->user()->unreadNotifications as $notification)
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
<p class="preview-subject mb-1">{{ $notification->data['title'] }}</p>
<p class="text-muted ellipsis mb-0">{{ $notification->data['text'] }}</p>
</div>
</a>
#endforeach
If you set up $casts properly on Notification-model to cast data-column as array, there is no need to use json_decode on blade.
$casts = [ 'data' => 'array' ];
What does your model look like?
btw: You should generally avoid #php — it usually points out code that belongs in the controller. I suppose this is test-only code? Instead, better dd($notification); to check data-attribute.

Variable from a controller to a view without a foreach loop in Laravel

I want to show the content of an article created, I have a show method in my controller
public function show_capitulos($id)
{
$data=Capitulo::select('capitulos.titulo as capitulo','capitulos.descripcion','capitulo_secciones.contenido','capitulo_videos.video')
->join('capitulo_secciones','capitulos.id','=','capitulo_secciones.capitulo_id')
->join('capitulo_videos','capitulos.id','=','capitulo_secciones.capitulo_id')
->where('capitulos.id',$id);
return view('administrador.capitulos.show')->with(['data'=>$data]);
}
And I want to pass that data to my view in Laravel, without using a foreach loop, but it keeps showing the error of undefined variable
I've read that you use the get() function to retrieve a collection of data and that with first() you get only one.
**UPDATE:**After adding the get() in my code a new error is showing
error
This is what I have in my view
<div class="row" id="contenido-cursos">
<div class="justify-content-between flex-wrap align-items-center pb-2 mb-3 ">
<h2 class="title">Capítulo 1</h2>
<h1 class="nombre-capitulo"> {{ $data->capitulo }}</h1>
<div class="linea-capitulo"></div>
<p id="titulo-capitulo">{{$data->descripcion}}</p>
<h2 class="title sub">Contenido</h2>
<p class="contenidocap"><br>{{$data->contenido}}</p>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/recursos.png')}}"> {{ __('Descargar Cap1.pdf') }}</a>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/test.png')}}"> {{ __('Hacer Test 1') }}</a>
</div>
</div>
You are defining the query but not executing it.
After concatenating where() and join() you have to call get() to retrieve the results for the query.

Laravel 5.2 Undefined variable theme

I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.

Categories