0

I have written a member function like ::

public function details($article_seo){
        $articleDet = Articles::where(function($query) {
            $query->where('article-seo', '=', $article_seo);
            })
        ->get();
        dd($articleDet);
    }

and have used these at the beginning of laravel controller::

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\articles;  /** this is the eloquent model **/

The problem is :: when ever i try to pass the $article_seo that I am receiving from the url data, it gives me an eror of ::: Undefined variable: article_seo

Being new to Laravel i have checked out lot of ways but unable to find one.Please help!!!!

2 Answers 2

2

When you want to use an outside variable inside of a closure, you have pass the variable into the closure using the use keyword.

public function details($article_seo){
        $articleDet = Articles::where(function($query) use ($article_seo) {
            $query->where('article-seo', '=', $article_seo);
            })
        ->get();
        dd($articleDet);
    }

You can get Articles match with your article-seo easily.

    public function details($article_seo){
        $articleDet = Articles::where('article-seo', '=', $article_seo)
        ->get();
        dd($articleDet);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Closures aren't necessary here, you can simple use the model's where method to do what you are looking for.

$details = Articles::where('article-seo', '=', $article_seo)->get();

Cleaning that up a little bit, the where expression will use an equal sign by default, so we can omit that.

$details = Articles::where('article-seo', $article_seo)->get();

If your database values were snake cased (article_seo instead of article-seo), then you can simplify that expression even more

$details = Articles::whereArticleSeo($article_seo)->get();

That is a whole lot easier to read than the original closure based method!

1 Comment

thank you too for the help, both the method were helpful, being new to Laravel it helped me explore both way functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.