Oscar John
Oscar John

Reputation: 41

How to solve this error: join() expects at most 2 arguments, 4 given Laravel

I am trying to display some data on a view from the database using 2 tables. Its a details view which shows data for a particular id. But I want to match a column with another column from another table and show data from the other table on the same view.

Here is how I tried it, but I am getting an error join() expects at most 2 arguments, 4 given

function details ($id) {
        $fruit = Fruit::find($id);
                        join('fruit', 'fruit.id', '=', $id);
        $page = "Fruit Details";
        return view('admin.fruits.details', compact('fruit'), ['page'=>$page]);
    }

Please Help

Upvotes: 0

Views: 592

Answers (1)

waterloomatt
waterloomatt

Reputation: 3742

As the comments point out you closed the query with the semi-colon so now, join is just the standard PHP join that accepts 2 parameters.

$fruit = Fruit::find($id);
join('fruit', 'fruit.id', '=', $id);

There are actually quite a few ways to do joins in Laravel but (IMHO) the most straightforward way is to use Laravel's Query Builder.

Your table/model names are a bit confusing but it sounds like you have 2 tables: a parent (I can't see the name from the code you provided) and a child table called fruit. I made the assumption that your model Fruit points to a table named fruit_main

// Notice the join uses the names of the tables to distinguish the ID columns
$fruit = DB::table('fruit_main')
    ->join('fruit', 'fruit.id', '=', 'fruit_main.id')
    ->where('fruit_main.id', $id)
    ->first();

Upvotes: 1

Related Questions