0

Lets me explain! I have an API with plain php that my app use (returns JSON).

Now I making a web site with Laravel 5.4 to show the same data that the user can see with the mobile app connected with the api.

The problem? I have to make the same logic that I follow in my api (because I need to get the same result). I need to list data but is not directly from one of my table, is from a custom query, then this result of the query I need to do some logic and then create the model that I Want to return to the view.blade.php to loop after all.

Of course, working with Laravel, I don't have to use any api because I'm already on the server side.

It is possible to do what I want? This is an example of my custom model (I put an image from my json of my API, but I need to get the same result with a custom model like I said above) what i gonna have after the query and after make some logic on the query result:

enter image description here

8
  • what i can see is, creating object model that have doors, windows, seats, month, year, dayOfWeek, date and a part model that have id, part name, object id and thing model with name, state, part id something like that. by itself, ORM will map objects that related to each other -- well, i want to say, one-to-many relation will end up the one part hold a collection of the many part. also, don't loop json in blade, use toJson() if possible. Commented Jan 31, 2017 at 2:05
  • Make a model like you said is ok, what I don't know how to make a custom query and then match my results with my custom model. Then i pass to my view the model Commented Jan 31, 2017 at 2:09
  • "I need to list data but is not directly from one of my table, is from a custom query" You mean you're not directly connected to the database? then how do you get those data? through API Call? Anyways, you don't necessarily need to create a custom model for custom query (if it's really complex). consider using Query Objects Commented Jan 31, 2017 at 2:38
  • Query builder now also returns a collection. So you handle the data same as models. And you can pass them to your view. $result = DB::table('table')->get(); Pass to the view return view('view', compact('result'));. In the view get them in a loop same as models foreach($result as $res){ $res->attribute; } Commented Jan 31, 2017 at 2:40
  • Ok but I want to get data from custom query, then make some logic into the data result and after that, set that data into my custom model and return my model to the view Commented Jan 31, 2017 at 2:45

1 Answer 1

2

As per you comment you have a custom query and from that you need to make a model. You can follow following steps.

Create migration

public function up()
{
    DB::statement("
    CREATE VIEW your_view_name
    AS
    SELECT 
        -- your query goes here
    ");
}

Run migration with php artisan migrate

Then create a model using the created mysql view.

use Illuminate\Database\Eloquent\Model;

class YourClassName extends Model
{
    protected $table = 'your_view_name';
}

Then you can use your model as you would use normally.

Sign up to request clarification or add additional context in comments.

1 Comment

@Faustino Gagneten is this answer help you out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.