for example I have model Message
with these columns:
messages
- id
- body
- type
- created_at
- updated_at
And such a controller:
class MessageController extends Controller
{
/**
* Display the specified resource.
*
* @param \App\Message $message
* @return \Illuminate\Http\Response
*/
public function show(Message $message)
{
switch ($message->type) {
case 'info':
$color = 'blue';
// updated - lots of code here!
break;
case 'warning':
$color = 'yellow';
// updated - lots of code here!
break;
.
.
.
case 'danger':
$color = 'red';
// updated - lots of code here!
break;
default:
$color = 'gray';
// updated - lots of code here!
}
return view('messages.show',compact(['message','color']));
}
}
and as you see my problem is this long switch
. I tried searching and I found that Polymorphism
could help me to avoid this long switch, but I did not find out how to implement it how much I went and all my searches ended in Polymorphic Relations in Laravel
that was not my answer.
would you help me to do something like this:
class MessageController extends Controller
{
/**
* Display the specified resource.
*
* @param \App\Message $message
* @return \Illuminate\Http\Response
*/
public function show(Message $message)
{
$color = $message->[something?]->color();
return view('messages.show',compact(['message','color']));
}
}
Update: As I wrote at beginning, It's just an example and switch case block will contain more lines of code.