#13 CRUD - Read functionality | Laravel 5 6

Display Data

# Show all data from a table
DB::table('tablename')->get(); // This is query builder method
\App\modelclassname::all(); // This is Eloquent method Just replace DB::table('tablename')-> with \App\modelclassname. Here modelclassname is the model created for that database. e.g if database name is tasks model name will be task.

#Show a specific data from a table
DB::table('songs')->where('id',2)->get(); // Get all the data from a table songs where id=2.
or \App\Song::where('id',2)->get();
DB::table('songs')->where('id','>',2)->get(); // Get all the data from a table songs where id is greater than 2.

#Show all data from the table in ascending order
or \App\Task::latest()->get()

#Find the data for a specific id 
DB::table('tasks')->find(2) // here 2 is the id
or or \App\Task::find(2)

# Get all data from table 'tasks' but only form a specific colum 'body'
\App\task::pluck('body'); // notice we have use task and not tasks when we use this method

# Show latest 50 items
$all_jobs = PostJob::latest()->take(50)->get();

# Get all the values from city column of post_jobs table which are unique meaning exclude similar/repetitive values
$cities = PostJob::pluck( 'city' )->unique();

DB::table('tablename')->update(['votes' => 1]);

# NOT Equal to so and so 
// Example 1:
$profile_users = User::where( 'category', '!=', 'Recruiter' )->get();
//Example 2
$profile_users = User::where( 'category', '!=', 'Recruiter' )->
		                        where( 'category', '!=', 'Admin' )->get();

Add your review

Leave a Reply