eloquent

Fetch specific fields on a hasMany relation

拜拜、爱过 提交于 2020-01-22 19:38:48
问题 I have a hasMany relation function like this: public function articles() { return $this->hasMany('App\Article'); } And use it like this: $data = \App\User::with('articles')->get(); I don't have any problems with it, since it's returning the expected data. Something like this: { "id": 1, "name": "Jhon", "lastname": "Doe", "articles": [ { "id": 1, "title": "Article 1", "status": "published", "published_at": "2015-04-30" }, { "id": 2, "title": "Article 2", "status": "draft", "published_at": null

WhereNotExists Laravel Eloquent

≯℡__Kan透↙ 提交于 2020-01-22 19:31:09
问题 Little bit of trouble with the eloquent framework for laravel. I need to replicate a query like this : SELECT * FROM RepairJob WHERE NOT EXISTS (SELECT repair_job_id FROM DismissedRequest WHERE RepairJob.id = DismissedRequest.repair_job_id); Right now I have $repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')->where('active', '=', 'Y')->whereNotExists('id', [DismissedRequest::all('repair_job_id')])->get(); Anyone an idea? I need to get all the repairjobs where there is no

Laravel Eloquent: is SQL injection prevention done automatically?

核能气质少年 提交于 2020-01-22 13:50:14
问题 Given the example code ( Message is an Eloquent model.): public function submit(Request $request){ $this->validate($request, [ 'name' => "required", "email" => "required" ]); //database connection $message = new Message; $message->name = $request->input("name"); $message->email = $request->input("email"); $message->save(); } Does Eloquent use parameterized queries (like PDO) or any other mechanisms to prevent SQL injection? 回答1: Yes, but... Yes, it does SQL injection prevention when you rely

Does Laravel's toSql() method mask ids? (column value being replaced by question mark)

别说谁变了你拦得住时间么 提交于 2020-01-22 11:17:40
问题 I'm trying to debug some SQL queries that I'm doing in a testing suite. Using the following debugging code: \Log::debug(User::first()->jobs()->toSql()); The SQL that prints out is: `select * from `jobs` where `jobs`.`deleted_at` is null and `jobs`.`managed_by_id` = ? and `jobs`.`managed_by_id` is not null` What is that question mark doing there? I've tested the query, and it works as expected. Is it because i'm selecting that first() user that this is happening? 回答1: Laravel uses Prepared

Do I need to sanitize the user input Laravel

不打扰是莪最后的温柔 提交于 2020-01-22 09:39:08
问题 I am using Laravel 4 with Eloquent. When I get the user input I just use $name=Input::get('name') and then I do $a->name=$name; I don't know if the function Input::get protect me from SQL Injection and XSS. If it does not, what do I have to do to sanitize the input? And, when I show the value in my view, shall I use {{$a}} or {{{$a}}} Greetings and thanks. 回答1: Laravel uses PDO's parameter binding, so SQL injection is not something you should worry about. You should read this though. Input:

Do I need to sanitize the user input Laravel

两盒软妹~` 提交于 2020-01-22 09:38:07
问题 I am using Laravel 4 with Eloquent. When I get the user input I just use $name=Input::get('name') and then I do $a->name=$name; I don't know if the function Input::get protect me from SQL Injection and XSS. If it does not, what do I have to do to sanitize the input? And, when I show the value in my view, shall I use {{$a}} or {{{$a}}} Greetings and thanks. 回答1: Laravel uses PDO's parameter binding, so SQL injection is not something you should worry about. You should read this though. Input:

How to load Laravel with nested models and limit the nested models?

纵饮孤独 提交于 2020-01-21 10:18:08
问题 Hello guys I am using Laravel 5.6 I have three tables in the database posts,comments and replies and three models Post , Comment , Reply the relations are as follow one post has many comments and one comment has many replies. I created a route that when hit will return some data ;however, I want this data to be in a specific way read this example: Lets say I have 6 posts in my database and each post has 6 comments also each comment has 6 replies I want to return only the first 3 posts along

Laravel many to many relationship using Eloquent

泪湿孤枕 提交于 2020-01-20 05:19:04
问题 I'm trying to create a many to many relationship using Laravel, but I am stuck. Here's my current table model: album album_id name created_at user_image user_image_id value albumxuser_image (junction table) albumxuser_image_id (primary key & auto increment) album_id (FK from album) user_image_id (FK from user_image) I want to be able to get the album name from albumxuser_image table. Here's what I've done so far. Album.php model namespace App\Models; use Illuminate\Database\Eloquent\Model;

Laravel Relationships

≯℡__Kan透↙ 提交于 2020-01-20 04:34:04
问题 I've been looking over relationships in Laravel 4 in the documentation and I'm trying to work out the following. I have a table in my database called 'events'. This table has various fields that mainly contain ID's that relate to other tables. For example, I have a 'courses' table. The events table contains a field called 'course_id' which relates to the ID of the 'id' field in the courses table. So basically, I'm after some advice on how you go about relating the two (belongsTo()?) and then

Laravel filter reset after next page click in paginate

霸气de小男生 提交于 2020-01-19 18:05:06
问题 I have create table where showing data. I also add few filters to filter data. Using paginate i show 20 records per page. After select filter and click search records in table filter with paginate in first page but as soon as i click next page filters getting reset. How to stop filters getting reset? Below is my code, public function index() { $agos = DB::table('orders') ->leftJoin('companies', 'orders.company_id', '=', 'companies.id') ->select(DB::raw('orders.id, companies.name, orders.type,