Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

newbie360's avatar

query and show all movies info with aka name

ok i follow the Laravel doc setup the OneToMany Relationships

// Movie model
class Movie extends Model
{
    protected $table = 'movies';
    protected $guarded = [];

    public function movieAka()
    {
        return $this->hasMany('App\MovieAka', 'movie_id');
    }
}
// movie_aka table
id | movie_id | aka_name

// MovieAka model
class MovieAka extends Model
{
    protected $table = 'movie_aka';
    protected $guarded = [];

    public function movie()
    {
        return $this->belongsTo('App\Movie', 'movie_id');
    }
}
// MovieController
class MovieController extends Controller
{
    public function listMovieWithAkaName(Request $request)
    {
        $movieAkaName = Movie::find(5)->movieAka;
        return $movieAkaName;
    }
}

all is worked fine, but what about i want to show all the movies info with aka name in the same page look like imdb.com , after u search a movie, on the result page will show the aka another name so i want the result look like this:

movie name: abcdef (aka ccc, aaa, bbb)

movie name: xyz (this may be empty)

Thx

0 likes
7 replies
RamjithAp's avatar
Level 10

Try $movies= Movie::with('MovieAka')->get();

Snapey's avatar
// MovieController
class MovieController extends Controller
{
    public function listMovieWithAkaName(Request $request)
    {
        $movieAkaName = Movie::with('movieAka')->find(5);
        return $movieAkaName;
    }
}

view

{{ $movie->name }} (
    @foreach($movie->movieAka as $aka)
        {{ $aka->name }},
    @endforeach 
    )

this would get you going.

bonus for working out how to not end the list with a comma, and not show the braces if there are none

newbie360's avatar

@Snapey because i want show all movies, not only 1 movie

@RamjithAp is the answer $moviesWithAkaName = Movie::with('movieAka')->get();

Thankyou so much =)

newbie360's avatar

@Snapey use Laravel implode() ?

Movie Name: {{ $movie->name }}
@if ( count($movie->movieAka) )
    ( {{ $movie->movieAka->implode('aka_name', ', ') }} )
@endif

// aka_name is the column in the movie_aka table
2 likes
Telesteini's avatar

You can use following query

select M.name
from movies M
  join actor_role AR on AR.MID = M.MID
  join actors A on A.AID = AR.AID
where A.name = 'Charlie Chaplin'

You can also use inner joint as follows

SELECT movie.name
FROM movies, actors, actor_role
WHERE actors.name = 'Charlie Chaplin' AND
    movies.MID = actor_role.MID AND
    actors.AID = actor_role.AID

You can also actors name query which allows users to fetch movies by its actors. You can use following query. Many apps like SilentGhost HD are using this application to fetch movies according its actors.

SELECT movie.name
    FROM actor_role as ar
         inner join actors as a
         on ar.AID = a.AID
         INNER JOIN movies as m 
         ON ar.MID = 
    WHERE a.name = 'Charlie Chaplin'

Please or to participate in this conversation.