eloquent

Clone an Eloquent object including all relationships?

天涯浪子 提交于 2019-12-28 03:31:14
问题 Is there any way to easily clone an Eloquent object, including all of its relationships? For example, if I had these tables: users ( id, name, email ) roles ( id, name ) user_roles ( user_id, role_id ) In addition to creating a new row in the users table, with all columns being the same except id , it should also create a new row in the user_roles table, assigning the same role to the new user. Something like this: $user = User::find(1); $new_user = $user->clone(); Where the User model has

Laravel 4: how to “order by” using Eloquent ORM [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-28 02:23:48
问题 This question already has answers here : Laravel Eloquent: Ordering results of all() (8 answers) Closed 4 years ago . Simple question - how do I order by 'id' descending in Laravel 4. The relevant part of my controller looks like this: $posts = $this->post->all() As I understand you use this line: ->orderBy('id', 'DESC'); But how does that fit in with my above code? 回答1: If you are using post as a model (without dependency injection), you can also do: $posts = Post::orderBy('id', 'DESC')->get

Add a custom attribute to a Laravel / Eloquent model on load?

爷,独闯天下 提交于 2019-12-27 11:33:30
问题 I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open() method. For instance, at the moment, in my controller I have: public function index() { $sessions = EventSession::all(); foreach ($sessions as $i => $session) { $sessions[$i]->available = $session->getAvailability(); } return $sessions; } It would be nice to be able to omit the loop and have the 'available' attribute already

Why PHP considers MySQL INT columns as strings?

痴心易碎 提交于 2019-12-25 18:39:05
问题 I am using a Laravel query builder to search for categories. Here it is: $array[] = $categories->where('pk_i_id', $category_id)->first(); I have to manually convert the category id to string, even though in database it is Integer type. Why do I have to do this? Actually - I have to do this on Linux machine with Lamp stack installed. On Windows machine with Xampp it considers the column as integer as it should. 回答1: The issue sounds more like that $categories is already a Collection , not a

Laravel “ErrorException (E_NOTICE) Undefined variable: class” [duplicate]

谁说我不能喝 提交于 2019-12-25 17:21:51
问题 This question already has answers here : “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP (28 answers) Closed 9 days ago . Hello everyone can help me, I have a problem "ErrorException (E_NOTICE) Undefined variable: class" class ControllerTraining extends Controller { public function index() { $title = "Data Training"; foreach(Sentimen::all() as $stm){ $class['class'][] = $stm->kategori; $data_training[$stm->kategori] = WordFrequency::where('id

Laravel 5 eloquent with whereHas more than 3. How to?

我怕爱的太早我们不能终老 提交于 2019-12-25 17:04:10
问题 it seems that it does not work, when I'm using "with" and function inside it and later trying to use "has" function. Here's the code, it should be easier to understand: $users = Users::where("active","=", "1")->with(['photos' => function($q){ $q->where('type','!=', 2); }])->has('photos', '>', 3)->paginate(8); } It seems, that it should return user ONLY if he has 3 photos which are NOT of type 2. But it doesn't care about "where" clause and return it without checking to the type. How can I

Laravel 5 eloquent with whereHas more than 3. How to?

谁说我不能喝 提交于 2019-12-25 17:03:48
问题 it seems that it does not work, when I'm using "with" and function inside it and later trying to use "has" function. Here's the code, it should be easier to understand: $users = Users::where("active","=", "1")->with(['photos' => function($q){ $q->where('type','!=', 2); }])->has('photos', '>', 3)->paginate(8); } It seems, that it should return user ONLY if he has 3 photos which are NOT of type 2. But it doesn't care about "where" clause and return it without checking to the type. How can I

Laravel 4 Eloquent assign BIT values

会有一股神秘感。 提交于 2019-12-25 16:56:06
问题 I want to set a filed with BIT data type in mySql using Eloquent. $i = new Step; $i->active = 'b0'; $i->save(); But the active filed is 1 , I also tried: $i->active = "b'0'"; $i->active = '0'; $i->active = false; ... Simply I want to run something like this: INSERT INTO `steps` (`active`) VALUES (b'0') 回答1: Talking about active field: If you want to use active and inactive for flagging active and inactive state of any record (i.e. user model) then you may use tinyint data type. Bool, Boolean:

laravel blade foreach fetching related tasks to projects 'Trying to get property of non-object'

最后都变了- 提交于 2019-12-25 16:48:17
问题 Hi I am trying to output all the related tasks to a Project but I am getting 'Trying to get property of non-object' in my error log so I'm not sure what I am doing wrong, does anyone have any ideas? This is my view which has the foreach {{ $project->project_name }} {{ $project->project_brief }} {{ $project->clients->client_name }} @foreach ($project->tasks as $project) {{ $project->tasks->task_name }} @endforeach This is my repository which performs the query public function getProjectTasks(

Laravel 4.2 execute query without using elequent query builder

荒凉一梦 提交于 2019-12-25 16:47:45
问题 I have a query with sub-query .. and i want to write an explicite SQL request and execute it Query : select count(*) as count from ( SELECT DISTINCT t.article_id FROM `articles` as a left join `tags` as t on a.`id` = t.`article_id` where a.`flag` = 1 ) as sub i tried this to execute the request without building the query : DB::connection()->getPdo()->exec( $sql ); But it always return 0 ! 回答1: You can use sub query with DB::raw . A method DB::raw() (don’t forget to use Illuminate\Support