mercredi 29 mars 2017

Laravel Model - All ID's return 1

I want to get all the projects through a group. I got that working now, but I can't seem to get the right ID's. I keep getting 1 as the ID for all my posts. Am I doing something wrong?

ProjectController.php

public function index()
{
    $projects = \Auth::user()->projects;
    dd($projects);
    return view('projects.home', compact('projects'));
}

User.php

public function projects()
{
    return $this->hasManyThrough(
        'App\Project', 'App\Group',
        'id', 'group_id', 'group_id'
    );
}

Dump (all items have the same attribute > id, all the other attributes are good)

Collection {#224 ▼
  #items: array:8 [▼
    0 => Project {#225 ▼
      #table: "projects"
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "name" => "project.com"
        "display_name" => "Project 1"
        "group_id" => 1
        "active" => 0
        "created_at" => null
        "updated_at" => "2017-03-29 12:21:47"
      ]
      #original: array:7 [▶]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    1 => Project {#226 ▶}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2o7dLRq
via IFTTT

How to insert array fields data in Laravel5

How can i insert multiple text fields data with laravel. I have two fields.

<input type="text" name="title[]" />
<input type="text" name="email[]" />

Now in controller i can get these values like this.

$name =  $request->title;
$description =  $request->email;

How can i add this with eloquent.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nLuo4C
via IFTTT

Return Collection after update()?

Using Raw, how to return collection of updated row?

For example:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

I was expecting dd($updated) to return updated row of collection but it returned 1.

 should return 123



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nuTNgJ
via IFTTT

Add custom atrribute to json response

I have model something like that with custom attribute

class MyModel extends Model
{
    public function getExtraAttribute(){
        return 'some string'; //etc.
    }
}

And for controller method i have this

return MyModel::where('user_id', Auth::user()->id)->get();

But i don't see 'extra' attribute on json response

P.s. extra isn't column from database.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nLDNJN
via IFTTT

withInput() doesn't work

And when I use it with withError(), it does not work either.

This works:

if ($validation->fails()) {
    //////////// Works and return the $errors array
    return redirect()->back()->withErrors($validation);                         
}

This doesn't work:

if ($validation->fails()) {
    return redirect()->back()->withInput();                         
}

This doesn't work too (either for withInput() and for withErrors())

if ($validation->fails()) {
    return redirect()->back()->withInput()->withErrors($validacion);
}

This is symptom of ... what problem?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2o6SPdA
via IFTTT

Returning javascript view in Laravel

In Ruby on Rails In my controller I can do something like this:

def jstest
    respond_to do |format|
        format.js
    end
end

This would return jstest.js.erb from the relevant views directory

jstest.js.erb

alert("Hello World")

The result being I get hello world alert.

I want to do the same thing in Laravel

In my controller I have:

public function jstest(  )
{
    return view('jstest');
}

in my view (which I have tried with both jstest.blade.js and just jstest.js)

but I get the error, view cannot be found.

The only way I get this to work is by calling the view jstest.blade.php and including my js in a <script> tag within this php file. but this feels a bit wrong...?

Is this even possible in Laravel? If so, where am I going wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ngCiz0
via IFTTT

How to get all month record count in laravel

I have applied group by with with created_at column using DB query but like to do it by laravel eloquent

Output should be like this:-

Array
(
  [1] => 2
  [2] => 3
  [3] => 7
  [4] => 4
  [5] => 5
  [6] => 7
  [7] => 2
  [8] => 9
  [9] => 0
  [10] => 4
  [11] => 0
  [12] => 0
)

Please provide any help me to do this.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2o6LXNf
via IFTTT