/    Sign up×
Community /Pin to ProfileBookmark

added more then one variable and query to the controller code in laravel .

Hey guys !

I have a very specific question about generating a query in laravel , Below is my question .

Suppose i have the following table :

[ATTACH]16623[/ATTACH]

now suppose i have the following code in my controller :

[CODE]public function getIndex(){
$this->layout->content = View::make(‘test.index’)
->with(‘snippets’ , Testing::take(2)->orderBy(‘id’,’desc’)->get());
}[/CODE]

see how it is passing a variable snippets to the view , with i cn iterate over by using a for Each loop .

now want i want to do is , i want to write a query that fetches only Rakesh from the coloumb name .

I need help with the following :

  • Firstly ,can somebody tell me roughtly what the query would look like

  • Secoundly , can somebody tell me how can i prepend/append a new variable and query to the existing controller code .
  • Thanks .

    Gautam .

    [canned-message]attachments-removed-during-migration[/canned-message]

    to post a comment
    PHP

    12 Comments(s)

    Copy linkTweet thisAlerts:
    @iBeZiMar 18.2015 — Try using the "where" function on your model, you can pass more than one variable to a view by using an array instead of "with"

    [code=php]
    public function getIndex(){
    $rakesh = Testing::where('name','=','Rakesh')->get();
    $snippets = Testing::take(2)->orderBy('id','desc')->get();
    $this->layout->content = View::make('test.index',array(
    'snippets'=>$snippets,
    'rakesh'=>$rakesh
    ));
    }
    [/code]
    Copy linkTweet thisAlerts:
    @NogDogMar 19.2015 — Yeah, I'd try to keep DB logic out of your controller, instead creating a method in your model.
    Copy linkTweet thisAlerts:
    @Vikas_PatelMar 19.2015 — Queries in Laravel are damn easy. With Eloquent and the Laravel Query Builder you can build your queries directly in PHP without writing any SQL yourself. With your applications growing your requirements will grow too. In some cases you will need to chain your sql parts based on conditions. This article will show you how to work with conditional queries in Laravel.

    The scenario

    Recently I was working on a Laravel project where I was implementing a search. It was a product search with a lot of different options to filter results. On the backend I got all the necessary information to build the database query. Of course with all these different options the query needs to be build different for every search. Let's take a look at a simple example.

    $category = 'food';

    $color = 'red';

    $searchResults = DB::table('awesome_products')

    ->where('category', $category)

    ->where('color', $color)

    ->get();

    We are using the Laravel query builder to select all products from the category "food" with the color "red". Of course in a real application this would look different, but as I mentioned this is a simple example to show the scenario. But what If only the category is defined and not the color? A first attempt would probably look like this.

    if(isset($category) && isset($color)) {

    $searchResults = DB::table('awesome_products')
    ->where('category', $category)
    ->where('color', $color)
    ->get();


    } eleseif(isset($category)) {

    $searchResults = DB::table('awesome_products')
    ->where('category', $category)
    ->get();


    }
    Copy linkTweet thisAlerts:
    @iBeZiMar 19.2015 — @NogDog

    I'd argue that the database logic is [I]already[/I] abstracted out of the controller via the modelling system, all you're doing in the controller is accessing the models instead of running direct queries on the database. If you needed to make some really complicated query that's specific to a particular model, and might be used in more than one part of your application, [I]then[/I] you would add it as a function in the model.

    @Vikas

    Remember that using the query builder you can build up your selection slowly, you don't have to do everything at once.

    [code=php]
    $selection = DB::table('awesome_products');

    if(isset($category)) {
    $selection->where('category', '=', $category);
    }

    if(isset($color)) {
    $selection->where('color', '=', $color);
    }

    $searchResults = $selection->get();
    [/code]
    Copy linkTweet thisAlerts:
    @NogDogMar 19.2015 — Well, I'd argue that business logic does not belong in the controller. It's not just the fact that it's data logic. The Model in MVC is, theoretically, not just about database abstraction, it's about modeling all the business logic. However, MVC implementation and common usage seems to have moved to thinking of Models only as database objects. In our (and my) first Laravel application where I work, we're using Laravel commands as the place for business logic that then interfaces with the models, and those commands actually reference repository methods -- though all that is probably overkill for relatively straight-forward and not too complex web apps.

    The world will not end if you make a few Eloquent queries in your controllers -- but if you find yourself doing it a lot and the controller methods becoming longer and longer and more complex, it's probably time to think about re-factoring and moving the business logic out of your controllers. ?
    Copy linkTweet thisAlerts:
    @gautamz07authorMar 19.2015 — @NogDog , You make a very interesting point .

    @Vikas , Thx for your input .

    @Ibizi , Thanks for ur answer and inputs .. ? ?
    Copy linkTweet thisAlerts:
    @iBeZiMar 19.2015 — @NogDog

    I agree that complex tasks specific to a model should definitely be abstracted out of the controllers and into the model, but using simple eloquent queries in the controller is fine as far as I'm concerned, otherwise you're going to end up with functions in your model for every little thing...
    Copy linkTweet thisAlerts:
    @NogDogMar 19.2015 — @NogDog

    I agree that complex tasks specific to a model should definitely be abstracted out of the controllers and into the model, but using simple eloquent queries in the controller is fine as far as I'm concerned, otherwise you're going to end up with functions in your model for every little thing...[/QUOTE]


    Actually, it looks like I'll be ending up with functions in my repository files for every little thing. ?
    Copy linkTweet thisAlerts:
    @iBeZiMar 19.2015 — Huh, fair enough, I'd never actually looked into repository files before but I can definitely see why you'd want to use them if there was a chance you might want to switch away from Eloquent to a different ORM at some point.
    Copy linkTweet thisAlerts:
    @gautamz07authorMar 19.2015 — @everyone ! Stop confusing me ? ?
    Copy linkTweet thisAlerts:
    @NogDogMar 19.2015 — Huh, fair enough, I'd never actually looked into repository files before but I can definitely see why you'd want to use them if there was a chance you might want to switch away from Eloquent to a different ORM at some point.[/QUOTE]

    Yeah, there's a good chance at least some of the data will get indexed into some no-sql solution such as Solr, so we want to make that as easy as possible on the application code should we decide to. Plus, it's a new toy for us, and we want to to use it all. ?
    Copy linkTweet thisAlerts:
    @gautamz07authorMar 20.2015 — if i have the following query in my controller :

    [CODE]$rakesh = Testing::where('name', '=' , 'Rakesh')->get();[/CODE]

    in my view if i say :

    <p>{{ $rakesh }}</p>


    i get the following output in my browser :

    [CODE][{"id":1,"name":"Rakesh","location":"Jantar mantar ","city":"Delhi "}][/CODE]

    now what i wanted was only the name , so i can solve this problem by using the ->pluck('name') instead . but what if i want to iterate over all the values of $rakesh in my views only and display the name only , how could i do that ?
    ×

    Success!

    Help @gautamz07 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...