Laravel | Get value of a column with

laravel eloquent get column value
orwhere laravel
laravel distinct
laravel get single row by id
laravel count rows
laravel pluck
laravel insert get id
update get id laravel

i am having a problem with laravel, all the details are given below.

I Have 2 tables in mysql database : books & pens.

books & pens have columns : bookid,bookname & penid,penname, respectively.

Users have a transactions table with : bookid or penid.

I am retrieving all the transactions of a specific user with this:

$usertransactions = $user->usertransactions;

Not to worry about the relationships between user and usertransaction model, its all setup.

the response of the function above is:

"data": {

    "Id": "1",
    "Bookid": "27",



"data": {

    "Id": "2",
    "Penid": "42",


I want the result to be like below:

"data": {

    "Id": "1",
    "Bookid": "27",
    "Bookname" : "bookname from books table using the Bookid from response"


I Would appreciate the help.


first of all, the common practice of naming columns is snake_case. so it is better to name bookid as book_id. and also for your relations and function names, it is good to keep it CamelCase. so instead of usertransactions it is better to write userTransactions.

Let's get back to your question. Since you have book_id in your response, you should be able to build a relationship between UserTransaction and Book models. in Book model:

public function userTransactions() {
    return $this->belongsToMany(UserTransaction::class); //it can be one to many or many to many base on your logic. I assume Many to many

then you should define the reverse relationship in UserTransaction model:

public function books(){
    return $this->belongsToMany(Book::class);

now you can load all books object using load method like $usertransactions = $user->usertransactions->load('books')

if you want your response match exactly then use as @Maraboc has mentioned.

Good luck!

Query Builder - Laravel, Hello, I am fetching value from database to generate user profile. In the Title I want to display firstname of user who has role=main user. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings. PDO does not support binding column names. Therefore, you should never allow user input to dictate the column names referenced by your queries, including "order by" columns

You are working with a many to many relation.

Check out this:

So in your code it could be:

return $this->belongsToMany('App\Book', 'transactions', 'user_id', 'book_id');

Use select or pluck to format your data if required.

Database: Query Builder - Laravel, how do I select a single field and return a string with no id know for the column in question since Database::Find(1) takes a id as its parameter w  Column Filter for Laravel Nova. A Laravel Nova column querier filter for Nova > v1.1.8. Prior to Nova v.1.3.2, use the 0.2.1 version.. Prior to Nova v.1.1.8, use the 0.1.1 version.

You can get what you want using the Eloquent: API Resources :

You have to create a Usertransaction resource, assuming you have a one to one relationship between Usertransaction and Book models :


namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Usertransaction extends JsonResource
     * Transform the resource into an array.
     * @param  \Illuminate\Http\Request
     * @return array
    public function toArray($request)
        return [
            'id' => $this->id,
            'Bookid' => $this->book->bookid,
            'Bookname' => $this->book->bookname 

Finally in the controller you can do this :

use App\Usertransaction;
use App\Http\Resources\Usertransaction as UsertransactionResource;

function YourMethod() {
    $usertransactions = $user->usertransactions;
    return UsertransactionResource::collection($usertransactions);

PS : you can do the same for pens :)

How to get single column value from DB, mysql_fetch_field — Get column information from a result and return as an table on live server (unless user is restricted or replace the $_GET with fixed value.) The Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We'll use the collect helper to create a new collection instance from the array, run the strtoupper function on each element, and then remove all empty elements:

how to select single field and get a single result with eloquent no id , This function cannot be used to Return values into a cell or column nested in a formula, to get a list of distinct values that can be counted or  i am having some trouble getting results from a query. Let me explain. I have two tables, tasks and task_interactions. the table tasks stores the tasks, and the table task_interactions stores the history of the tasks, like a change of status, comments

mysql_fetch_field - Manual, Get column of last names from a recordset: <?php and Usage. The array_column() function returns the values from a single column in the input array​. Get model column value in Laravel and new whereKey method in Laravel 5.4 January 22, 2017 by Marcin Nabiałek / 4 Comments In some cases we want to get Eloquent model single column value based on other column value.

VALUES function (DAX), The third argument is the column in that range of cells that contains the value that If it didn't, you'd have to enter one of the values in columns C or D to get a  The get method is responsible for transforming a raw value from the database into a cast value, while the set method should transform a cast value into a raw value that can be stored in the database. As an example, we will re-implement the built-in json cast type as a custom cast type:

  • have you a relationship between usertransaction and book models ?
  • yes , all the relationship of models is already setup everything is working fine, i can not find a way to get the bookname using bookid from response from books table
  • i don’t think you understood my question mate. theres nothing to be changed in the model files , its in the controller file. i need a statement which can get the transaction and then get the id from the response it got and using that id it gets the name of the book from books table.
  • I don't think you understood the MVC framework. Check Peyman's answer. We both are telling you to use belongsToMany method in your model. It's because not everything needs to go into the controller's file. Some methods must go into the model's file. And also, you should show your code of $user->usertransactions;
  • in some cases more than 1 transactions are received , will this work with them too? & do you mind telling me how does the resource class you made determines book as a model without importing it using use command at top? , "$this->book->bookid" , i am sorry i am not familiar with resources.
  • For the first question yes it will, because we assume it's a collection Usertransaction::collection, for the second question it's not a model that need import because i's just a relationship so no need for import, but you need to name your relation in the Usertransaction model book or rename the the book in this statment $this->book->bookname with the name of your relationship :)
  • BadMethodCallException
  • witch method ??
  • Illuminate\Database\Query\Builder::collection()